Hooks, Themes and Conformance

WordPress ‘hooks’ are a very powerful feature as long as they are universally supported by all themes. Sadly this is not always the case.

A good example is the pre_get_posts filter hook. Whenever WordPress retrieves posts to display this hook should be triggered. This allows plugins to intervene in the process, filtering out selected posts and preventing them from being displayed. In principle this is a pretty simple process. A typical implementation will look something like this:

/** 
 *  Hide 'restricted' posts 
 */ 
add_filter( 'pre_get_posts', 'my_exclude_restricted', 1 ); 
 
function my_exclude_restricted( $query ) { 
 
    // Allow in dashboard 
    if ( is_admin() ) { 
        return $query; 
    } 
 
    $cat = - get_cat_ID('Restricted'); 
    $query->set( 'cat', $cat); 
    return $query; 
}

Here we are simply filtering out all posts that have the category Restricted. $query is a WP_Query object and when the function is called will contain the information that will be used to retrieve the posts. Its set() method allows this information to be modified before the retrieval takes place. The flag ‘cat’ in the first argument says that we want to specify the category or categories to be returned. If, as here, the second argument is negative it means all categories except that one will be retrieved.

I was developing the plugin in one of the excellent Thrive themes. It didn’t work. By which I mean that it made no difference at all to the posts that were retrieved. Thinking I must have misunderstood something I spent a day or more trying to fathom what the problem might be, before it occurred to me that perhaps the theme simply didn’t support the pre_get_posts hook.

Sure enough, when I changed to one of the default WordPress themes it worked exactly as expected. On further investigation I found that none of the Thrive themes supported the hook. A support ticket to Thrive subsequently confirmed that this is the case. They add that they are working on improving their themes but that they don’t have an ETA for fixing this issue.

Personally, much as I like Thrive themes, I think this is unfortunate. There may of course be technical reasons why they cannot support the hook, but if so they should at least recognise that failure to do so means the themes are not strictly WordPress compatible. I don’t know if other themes suffer from similar limitations. If so, it makes life difficult for plugin developers. Not only do they have to ensure that they comply with the standards dictated by the WordPress API, but they also have to check compatibility with all the major themes.

This means that instead of just being informed that ‘This plugin has been tested with your version of WordPress,’ plugin users also need to know whether it has been tested with the theme they happen to be using.