Get the topmost category in WordPress

If you’re using WordPress, here’s a quick snippet of code to get the oldest ancestor (topmost parent) of a category or subcategory.

This can just go in functions.php.

The only caveat to the snippet is that it will always return only a single category, so if that’s not a problem, you should be fine.

Remove extra RSS feeds in WordPress

Here’s a WordPress snippet to remove RSS feeds for comments while keeping the main one available.

RSS Icon

RSS is super nifty. It’s a great way to keep on top of breaking news and articles from your favourite blogs and/or aggregators (I’ve got over 100 subscriptions in my feedly reader).

So it’s a wonder then that WordPress offers RSS feeds for comments on all your posts. I presume there’s a group of people who really like to track conversations, but in my experience, it’s the minority. This functionality should really be an additional (and not core) part of WordPress. If you want to prevent your site serving pointless additional RSS feeds, you’ll need to remove them from wp_head.

Removing them, however, is a two-step process. Add the following to your functions.php file:

And that’s it. The first two actions strip the RSS using remove_action with a high priority. The final add_action call tells WordPress to load our function when processing wp_head. This will go through and reinsert the main feed, leaving the comment feed out.

Congratulations! Your post conversations are now untrackable.

WordPress: Add page slug to body class (including parents!)

Are you using WordPress as a CMS? Do you want to style pages with CSS logically? You might want to try this.

By default, WordPress will inject the page and its ID into the body class. That’s great if you’re only running a single install, or if you really really don’t care what your CSS looks like.

Let’s face it – you care. Otherwise, you wouldn’t be here. Here’s a snippet that will inject the current page’s slug and all slugs of its parents into the body class of your HTML.

Add this to functions.php

And that’s it! You can then start chaining together page classes to target little overrides that your clients are probably hassling you about.

Help! register_activation_hook isn’t working!

If you’re developing a plugin, chances are you’ve been caught out by the WordPress activation/deactivation hooks.

I’ve been working on an Instagram plugin for WordPress, and in doing so have needed to use the functions register_activation_hook and register_deactivation_hook.

However, I – like many others, it seems – can’t get the hooked functions to execute. They simply don’t fire. To illustrate the problem, here’s an example from the code:

I would fully expect the system to execute the functions hooked within the constructor. Had that been the case, I wouldn’t have lost a day debugging a non-existant problem.

Finding a solution

Of course, everyone’s setup is different. There were many suggestions for fixing the problem. For the sake of usefulness, here’s the other major suggestion:

Your global variables aren’t global

If (like me), your plugin is a global, you actually need to explicitly define it as such with the global keyword. For example, the snippet

should instead by written this way:

Why? Well, for this, we need to consider how the plugin has its code loaded. After installation, your plugin has its code included “normally” with standard includes. This creates the global scope that you would expect. However, when you register a function with register_(de)activation_hook, it is called from within another function’s scope, effectively hiding your non-explicit globals.

Confused? Consider the plugin example from earlier. The variable $my_instagram is declared in the global scope, with global visibility. When your code executes, your plugin is included at the same scope, meaning you will have access to the variable (as they share the same suitably-high scope). Due to the way the hook/filter system in WordPress works, your activation/deactivation hooks are executed within a localised (not global) scope.

Screen beans - the only thing worse than scope.
Screen beans – the only thing worse than scope.

This scope-confusion applies to all functions created using create_function, or (in our specific example), functions called with call_user_func (and call_user_func_array). These functions have their own scope and are thus shielded from the cheap-and-nasty pile of implicit “globals” that haven’t been properly declared as such.

You can find a suitably more technical (and probably correct) write-up at the WordPress Codex page for register_activation_hook.

tl;dr: Explicitly declare your globals if you want them to work everywhere.

Ok, that’s great, but what’s the answer?

After having accidentally (and rather unwillingly) been given a very rough refresher on scope, I found that this was not my problem at all. Even running proxy functions (which eliminates scope issues) failed to call the functions specified in the hook.

After much trial and error (and I mean much), I found that the paths within register_(de)activation_hook were not matching the path found in plugin_dir_path. In fact, __FILE__ was returning a completely different path.

The problem? I had symlinked the theme directory in my install, meaning that __FILE__ returned a different path to plugin_dir_path. This is especially important, as the file path specified by the first parameter of register_(de)activation_hook is included before the function specified by the second parameter is called. So, if the path in the first parameter is wrong, you probably aren’t going to be executing many functions today.

The Fix

This is always my favourite part. To fix the symlinking problem, you just have to be very specific when you’re asking WordPress to execute a hook for you.

Fixing the first example, we get:

The most important changes occur on lines 1, 16, and 17. The first line fixes the definition by manually specifying the path using the base plugin directory (instead of an automagic WordPress search). Lines 16 and 17 specify the main plugin file itself as the path (instead of relying on __FILE__).

This allowed WordPress to find the plugin files without a hitch, which is especially important in the installation/uninstallation phases.

Moral of the story? Check your paths first.

Paths