Categories
WordPress

Theming WordPress in 2022

Historically I’ve used the Underscores starter theme with Sass when building a custom theme. Now I’m interested in incorporating Blocks, Patterns, and also being able to use Tailwind.

I did just build a site with _tw which is a Tailwind focused fork of Underscores. It has potential but was a bit difficult to override some of the default behaviours and styling.

The default WordPress Twenty Twenty Two theme doesn’t strike me too well. If you want to build a site that looks like it, it’s not bad, but I found the customization got awkward very quickly.

I think I’m still keen on the ACF approach, but could certainly use Blocks in standard page area. I just don’t want to build the outer pages with them. The ACF PHP API for creating custom blocks is also very handy unless I ever get some in-depth React knowledge to do it with JS.

So if I were to build a new website now I’d probably consider:

  • Another test of _tw with the latest version, using ACF for custom sections and mostly limiting myself to non-layout blocks.
  • Investigating other Tailwind starters
  • Just regular old Underscores once more. Forget the last six years?
Categories
WordPress

Updating SVN Repo

You may update without changing the code. This happens when I test a plugin with a new version of WordPress and don’t need to make any code changes.

Update the readme.txt with new info, then:

svn ci -m "My cool message"

This is the same command to push new changes that do involve updated code.

If SVN is unauthenticated use your wordpress.org account username (which is not the email address) and password. For me that is mikehealy.

Categories
WordPress

WooCommerce PayPal Checkout is slow

When active it sends an HTTP API request on every page load to PayPal. Adds >1 second to every frontend page render.

How does this shit pass QA?

Categories
WordPress

Create WordPress user programatically

if (! defined('ADDED_THE_USER')) {
    add_action('init', 'mh_add_user');
}

function mh_add_user() {
    $username = 'myusername';
    $password = 'cool_password';
    $email = 'theuser@example.com';

    // Create the new user
    $user_id = wp_create_user( $username, $password, $email );

    // Get current user object
    $user = get_user_by( 'id', $user_id );

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
    
    
    define('ADDED_THE_USER', true);
}
Categories
WordPress

WordPress Plugins of Note

Envira Gallery Lite

Categories
WordPress

WordPress Break Point

Via Ryan McCue.

[php title="functions.php"]
register_shutdown_function(function () {
  var_dump($GLOBALS['wp_current_filter']);
});[/php]
Categories
WordPress

Quickly Protect Whole WordPress Site

Some WordPress sites aren’t for public consumption.
Chuck this in your functions.php

add_action('init', function() {

  $uri = $_SERVER['REQUEST_URI'];
  if(!preg_match('/wp\-(admin|login)/', $uri) AND !is_user_logged_in()) {
    header("Location:" . get_bloginfo('url').'/wp-admin/');
    exit;
  }

});
Categories
WordPress

Don’t track internal visits to a WordPress site

Here’s a quick way to reduce the number of internal site visits showing up in Google Analytics from logged in internal users.

This code was for a WooThemes based site using the woo_head hook. It won’t be available in other themes.
[php]<?php
add_action(‘woo_top’, ‘my_analytics’, 5);
function my_analytics() {

//Don’t track admins (editors, or publishers…)
if( current_user_can(‘edit_posts’)) {
echo "\n<!– Internal User. Not counted –>\n";
return;
}

echo <<<EOD
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-YOUR-TRACKING-ID-HERE’, ‘example.com’);
ga(‘send’, ‘pageview’);

EOD;
}[/php]

Categories
WordPress

WordPress Menus with URLs That Don’t Break

WordPress has a handy Menus feature in the Appearance area of the admin. You can link to Pages and Post Categories and these will migrate fine as you move your site from a development to production environments and the site URL changes.

Sometimes though you may need to refer to internal URLs not supported by Menus natively  using the Custom URL feature. The problem is that specifying the absolute URL here will break when the site is deployed.

A simple solution is to add a filter and use a placeholder value in Custom URLs added to a menu.

wordpress-menu-custom-urls

Then add a filter in your themes functions.php file, or as a must-use plugin as a more robust solution:

<?php 
/**
* Replace (site_url) placeholder in navigation menus
* Helps keep menus portable between dev/staging/production environments
*/
add_filter(
	'wp_nav_menu', 
	function($menu) {
		$menu = str_replace('(site_url)', home_url(), $menu);
		return preg_replace('~https?\:\/\/(https?\:\/\/)~', '$1', $menu);
	},
20
);

Note: this code uses an Anonymous function which requires PHP5.3+.
For older versions define this as a regular function and pass in its name as a string to add_filter()