Unhook While Switched

In a comment on this Sitewide Tags post, Kevin left a comment indicating that they had experienced a conflict between Sitewide Tags and the Subscribe2 plugin. Others had reported that as well, including the CUNY Academic Commons.

How the conflict occurs is that Subscribe2 is hooked into several post related hooks & Sitewide Tags uses built in WP functions to save the post to the tags blog. To save in the tags blog, SWT calls switch_to_blog, saves the post & then restores the current blog. Calling the built in WP functions while switched, causes the post hooks to be called against the tags blog. But, the active plugins & theme are those of the original blog and not the tags blog. Due to limitations in PHP, it is not possible for WP core to completely switch context from one blog/site to another: constants cannot be redefined, functions cannot be unloaded from memory, etc.

However, nearly all plugins rely on hooks in WordPress to run their functions at an appropriate time. Subscribe2 is not an exception. So, while we can’t unload Subscribe2 from memory, what we can do is unhook it while WordPress is switched. There are now over 12,000 plugins available for download on wordpress.org. Rather than write some code that could be copied into any plugins that might want to unhook while switched, I wrote a framework plugin that any plugin can hook into, to provide a function callback for when the site/blog context is switched by WordPress and when the context is restored to the original site/blog. The two hooks can be accessed with

// call my_remove_hooks function when context is switched
add_action( 'ra_switched_remove', 'my_remove_hooks' );
// call my_restore_hooks function when context is restored
add_action( 'ra_switched_restore', 'my_restore_hooks' );

For Subscribe2, I searched the plugin for add_action. The relevant hooks that I found were

add_action('save_post', array(&$this, 's2_meta_handler'));
add_action('create_category', array(&$this, 'new_category'));
add_action('delete_category', array(&$this, 'delete_category'));
add_action('wp_meta', array(&$this, 'add_minimeta'), 0);
add_action('new_to_publish', array(&$this, 'publish'));
add_action('draft_to_publish', array(&$this, 'publish'));
add_action('pending_to_publish', array(&$this, 'publish'));
add_action('private_to_publish', array(&$this, 'publish'));
add_action('future_to_publish', array(&$this, 'publish'));

$this refers to the Subscribe2 object which is named $mysubscribe2. The Unhook while switched framework plugin includes the sample functions needed to unhook and rehook the hooked functions above.

About Ron

Homeschooling dad of 4 (ages 27 - 14), grampy to 3, WordPress core contributor, former farmboy & software developer by profession.

Comments

  1. Nice! (See, I noticed) :D