If you use WordPress on a regular basis you’ve probably heard of the All in One SEO Pack plugin. In summary it automates a lot of the SEO across your site and allows you to customise the meta properties on a per-page basis.
Today I came across a scenario where I needed to override this plugin for certain pages or posts, inparticular changing the title. Of course, you can do this within WordPress by disabling the plugin from within individual pages:
This disables the entire plugin for the page in question though. In my scenario I wanted to make the rules a little more hardcoded, only override the title, and take the option away from the user, for example, in the event that they forgot to disable it when adding a new page. I was able to achieve this adding a filter like so to my themes functions.php file:
function my_title($title) { global $post; // Check if a certain scenario is met if ( $post->post_parent == 22 ) { // Do whatever here with your title... $title = 'I Rock | ' . $post->post_title . ' | ' . get_bloginfo('name'); // We can also do other things here, like remove the canonical tag, for example... remove_action('wp_head', 'rel_canonical'); } return $title; } add_filter('aioseop_title', 'my_title', 1);
As you can see, we’re plugging into the aioseop_title filter and calling our own function called my_title. This checks if the current page/post parent is set to 22 and, if so, sets the title to something specific.