WordPress Network (Multisite): Always Specify Full Path for Includes

Recently I’ve been working a lot on network installations of WordPress. During development I won’t always adhere to best practices from the go-get. I’ll use little short-cuts. They let me know if something is going to work or not.

One error that I have come across, is a weird little thing that only happens under certain circumstances:

This happens if you include a plugin file that requires() a file in the same directory, and then browse to network admin. So, it works in normal admin, and it works if you require() a file from a different directory. But not in the same directory. It’s simple to fix, and is something that should be done for any beta or production code anyway..






What use is the WordPress register_activation_hook() function?

Having been playing around with WordPress Network (Multisite) plugins, mostly internal, not doing much that anyone else would be interested in having. During a plugin’s lifetime it goes through some critical points. The one you can’t avoid is activation. Many also experience deactivation or even deletion. Most will, at some point, be updated. for these critical points the plugin author may want to have certain critical events happen. Setting up options, creating databases, pre-populating meta data. And of course the reverse – deleting said litter.

A quick summary of theĀ register_activation_hook() function:

  • It doesn’t work on multisite. Network activation does not trigger this function
  • Updating the plugin does not trigger it
  • It is triggered every time a plugin is manually re-activated

None of these are my desired behaviour. I usually write my own hook that checks if the plugin version has changed, and fire off my actions.

Which makes me wonder whether the function is out dated, and should be replaced a set of alternative functions. They could perhaps even be actions?

  • register_plugin_activation_hook
  • register_plugin_network_action_hook
  • register_plugin_update_hook
  • register_plugin_update_network_hook

How To Remove ‘www’ From Your WordPress Network (multisite) Primary Domain

When setting up a WordPress Network (multisite) using subdomains you should avoid using the default ‘www’ sub domain prefix. You are told this when activating the network. There is a good reason for it – mostly that all the good and useful plugins assume that you have done.

I can think of two reasons why you haven’t done this: either you are a little lazy, or you have a well established website (with ‘www’ in it) that you are converting into a network.

Whatever the reason, some time later on, you are looking at your hundreds (or thousands!) of sites and wishing that you could somehow get rid of that pesky ‘www’.

There are 5 database tables and 1 config file for you to edit:

  1. Update wp-config.php where DOMAIN_CURRENT_SITE is defined.
  2. Update wp_site.domain (where id=1)
  3. Update wp_blogs.domain (where site_id=1 blog_id=1)
  4. Update wp_sitemeta.meta_value (where site_id=1 meta_key=siteurl)
  5. Update wp_options.option_value (where option_name=siteurl)
  6. Update wp_options.option_value (where option_name=home)

Depending on your set up, you may need to look further into the options table and post content, but I have successfully updated three networks using the above 6 changes.

SEO friendly WordPress Network domain mapping URL filters

When running a WordPress network with domain mapping, some clients and visitors find it less than re-assuring when the network’s domain appears in the page content (as opposed to the pretty mapped domain).

The domain mapping plugin WordPress MU Domain Mapping hasn’t been updated for a while, but it still works perfectly well (WordPress 3.4.2). It filters most content extremely well. However, with the constant advance of WordPress core, a few things are slipping the net.

Headers and Background

 * Fix header image url
add_filter( 'theme_mod_background_image', 'tcb_domap_url_fix' );
add_filter( 'theme_mod_header_image',     'tcb_domap_url_fix' );
function tcb_domap_url_fix( $content ){
  if( is_multisite() ) :
    if( function_exists('domain_mapping_post_content') ) :
      $content = domain_mapping_post_content( $content );
  return $content;

I don’t know if it ever did these (I’ve not been using it long enough to know), but currently these are missed. Easily fixed with the above snippet.

Image Widget Plugin

Some of our sites use the Image Widget plugin. The author has kindly added some filters so we can fix it in exactly the same way.

 * Fix image widget.
add_filter( 'image_widget_image_url',  'tcb_domap_image_widget_url_link_mapping', 1, 3 );
add_filter( 'image_widget_image_link', 'tcb_domap_image_widget_url_link_mapping', 1, 3 );
function tcb_domap_image_widget_url_link_mapping( $content, $args, $instance ){
  if( is_multisite() ) :
    if( function_exists('domain_mapping_post_content') ) :
      $content = domain_mapping_post_content( $content );
  return $content;

May all your URLs be pretty!