By Lisa Sabin-Wilson

The WordPress API provides a great solution for hooking in and extending or modifying functionality. Sometimes, however, you may find it useful to hook into another plugin rather than WordPress itself and to extend or modify that plugin’s functionality instead.

Here’s an example of making your plugin pluggable: interaction (or lack of) between WooCommerce and the All in One SEO Pack plugin. All in One SEO Pack, among other things, adds a custom document title, description, keywords, and canonical URL to each page of a WordPress site. People often ask how to have their plugins modify this information before it appears onscreen. Forum plugins and shopping-cart plugins particularly need this function, which All in One SEO Pack could easily satisfy if it would just provide a method of accessing its functionality. It has. Read on.

WooCommerce (and plugins with similar functionality) creates its own virtual pages for things like product listings, all contained on a single WordPress page. If you define http://mywebstore.com/shop as the WordPress page WooCommerce uses, for example, http://mywebstore.com/shop/product-name and all other product pages are created dynamically by the WooCommerce plugin and are outside the reach of other WordPress plugins. So WordPress and the All in One SEO Pack plugin don’t know about them.

You can see the problem for purposes of search engine optimization (SEO): WordPress and All in One SEO Pack think that all these product pages are the same as the shop page, with the same titles, canonical URL, and so on. Code could have been written into the plugin to compensate for the needs of the WooCommerce plugin, but that coding would be never-ending; an infinite number of other plugins might need to hook into the All in One SEO Pack functions. To solve this problem, the All in One SEO Pack API was born.

The WooCommerce plugin has an important need to hook into the document title, meta description, meta keywords, and canonical URL that the All in One SEO Pack plugin produces. Because WordPress and All in One SEO Pack weren’t aware of the generated product pages, they had the same canonical URL, which is detrimental for SEO purposes. The fix was simple.

In the All in One SEO Pack plugin, after the canonical URL is generated and immediately before printing it to the screen, the apply_filters function is used on the variable. This solution allows WooCommerce to use add_filter to hook in and filter the canonical URL, returning the appropriate URL for that page. That is, developers added the following code to the All in One SEO Pack plugin:

function prepare_canonical_url(){

$canonical_url = my_determine_canonical_url_function();

$new_canonical_url = apply_filters( 'aioseop_canonical_url',$canonical_url);

 

return $new_canonical_url;

}

This code returns the value of the canonical URL and lets another plugin filter it, if desired, before returning the final value.

The part you can add in the primary WooCommerce plugin file (at the end of the file before the closing ?>) is just as simple. It works the same way add_filter does for hooking into the WordPress API:

add_filter('aioseop_canonical_url','wooc_change_canonical_url');

 

function wooc_change_canonical_url($old_url){

$new_url = determine_current_product_page_url($old_url);

return $new_url;

}

This filter provides a simple solution that allows other plugins (and themes) to hook into a plugin without the need to add specific code for any plugin.