Lightweight WordPress environments

I had been running local development servers since before I started with WordPress. As share of WordPress stuff, bouncing around my computer, increased it became shaky code environment situation.

  • There are (now there are) tools to spin up whole WordPress sites or servers.
  • There is (now there is) WordPress multisite for many sites in single installation.

Either of these tends to complicate workflow and routinely makes you manage the tools for managing.

These days I do bulk of local development in single WordPress installation and have worked out workflow for fast on–the–fly configuration.

Configuration

When on development task the last thing you want to do is wander through WordPress admin. It can still be sluggish and poorly organized monster under the coat of paint.

For clients it can be simplified for practical use, for development there isn’t such luxury.

This leaves the one interface element which is fast to access and use, on both admin and front sides — the toolbar.

Toolbar enables us to do admin tasks faster. With right plugins it can do many more of those.

I use the following plugins, dedicated to configuration changes through toolbar:

 Indication

Another solid function for the toolbar is indication of what is going on.

I use the following plugins, dedicated to providing information in toolbar:

Also sometimes I use Admin Color Schemes to distinguish between sites.

Since Uniform Server lets me change between configuration on its level, I also made a snippet for a quick summary of PHP version, opcode cache and WP object cache status:

add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {

	$title       = PHP_VERSION;
	$zend_status = null;

	if ( function_exists( 'opcache_get_status' ) ) {
		$zend_status = opcache_get_status();
		$title .= empty( $zend_status['opcache_enabled'] ) ? '' : ', Zend';
	}

	if ( empty( $zend_status ) && function_exists( 'apc_add' ) ) {
		$title .= ', APC';
	}

	if ( wp_using_ext_object_cache() ) {
		$title .= ', OC';
	}

	/** @var WP_Admin_Bar $wp_admin_bar */
	$wp_admin_bar->add_node( array(
		'id'    => 'php-info',
		'title' => esc_html( $title ),
	) );
}, 200 );

Project context

The one remaining issue is that being able to toggle plugins from toolbar is still inconvenient enough for sets of them. I tend to work on site–specific themes, which need and/or assume certain set of plugins enabled. These sets are obviously different between themes too.

After a while it hit me that if it is conditional on theme (selected via Toolbar Theme Switcher) then I can make it conditional via code in the must use plugin:

$cookie_name = 'wordpress_tts_theme_' . md5( home_url( '', 'http' ) );
$theme_name  = isset( $_COOKIE[ $cookie_name ] ) ? $_COOKIE[ $cookie_name ] : false;
$plugins     = [
	'trident'      => [
		'meta-box/meta-box.php'
	],
	'canvas-child' => [
		'advanced-custom-fields/acf.php',
		'custom-digital-downloads/custom-digital-downloads.php',
		'woocommerce/woocommerce.php',
		'woocommerce-custom-collections-taxonomy/collections-taxonomy.php',
		'woofakepay/woofakepay.php',
	],
	'madame'       => [
		'advanced-custom-fields-pro/acf.php',
	],
];

if ( is_string( $theme_name ) && array_key_exists( $theme_name, $plugins ) ) {

	$plugins = $plugins[ $_COOKIE[ $cookie_name ] ];

	add_filter( 'option_active_plugins',
		function ( $active ) use ( $plugins ) {
			return array_merge( $active, $plugins );
		}
	);

	add_filter( 'pre_update_option_active_plugins',
		function ( $active ) use ( $plugins ) {
			return array_diff( $active, $plugins );
		}
	);
}

It is somewhat unreliable (Plugin Toggle gets confused by it), but automagical sync of plugin state with theme is well worth it in development.

Overall

In combination this setup allows me to switch between contexts very fast. It is less suitable for data–heavy projects, but as far as code side goes it makes for a fast and enjoyable workflow.

Related Posts

1 Comment

  • I will try these as I put my hands on WordPress. If I`ll face any issue, i will come to you for help.

Comments are closed.