avatar Deluxe Blog Tips About Projects

Add more placeholder tags to WordPress SEO plugin

WordPress SEO plugin is the best SEO plugin for WordPress. It's very flexible in customizing meta title using placeholder tags like %%title%%, %%sitename%%. The problem is the list of placeholder tags are fixed and sometimes not satisfy our needs. For example, we can't show value of a custom field (post meta) in the title! In this post I'll show you a way to add more placeholder tags and a complete solution (a flexible one) that you can use to show any custom field in the meta title.

Determine which custom field you want to show in title

Assuming we have a website of books, each book has a ISBN number and you store this number in a custom field named isbn.

And in the front end, you want to show the title like this: "Book title - ISBN number". Here we'll determine our custom placeholder tag for isbn number %%isbn%% and put it in the settings box of WordPress SEO plugin, like this:

Custom placeholder tag

Now we have to add some code to make this tag works in the front end.

The hook for meta title

The first thing we have to care about is the hook for meta title. The WordPress SEO plugin uses its custom hook wpseo_title to let developers change meta titles. Of course you can use default WordPress's hook wp_title with a high priority to overwrite it (> 15). But sometimes it doesn't work, because some bad themes don't use the proper way to output titles. This plugin has a mechanism that can solve it (using output buffering), so we'll go with wpseo_title.

Code snippet

Knowing the right hook, everything is straightforward:

add_filter( 'wpseo_title', 'your_prefix_meta_title' );

/**
 * Change meta title using custom placeholder tags
 *
 * @param string $title
 *
 * @return string
 */
function your_prefix_meta_title( $title )
{
    // Check for single post only
    if ( !is_single() )
        return $title;

    $isbn = get_post_meta( get_the_ID(), 'isbn', true );
    $title = str_replace( '%%isbn%%', $isbn, $title );

    return $title;
}

There're 3 things in this code:

  • Conditions: here we change title for single post only, so is_single() is used.
  • Getting the post meta value is simply via get_post_meta function.
  • Replace it

That's all we have to do. Don't forget to add this snippet in your functions.php file of your theme or in a functionality plugin.

Extend the code (case 1)

You see in the snippet above, we change the title for single post only. You can extend the code by adding placeholder tags for archive page, contact page, homepage, etc. and then use better conditions here. Remember in that case you have to write your own code to get tags' values (maybe it's not from custom field anymore).

Extend the code (case 2)

Another direction of extending the code is creating a placeholder tag that works with all custom field. I'm intending to build a placeholder tag %%meta_XXX%%, where XXX is the name of custom field. For example %%meta_published_date%% will take value from published_date custom field, %%meta_book_author%% will take value from book_author custom field and so on. That will give us unlimited placeholder tags for custom fields and we can output the meta title whatever we want!

And here is the code:

add_filter( 'wpseo_title', 'your_prefix_meta_title' );

/**
 * Change meta title using custom placeholder tags
 *
 * @param string $title
 *
 * @return string
 */
function your_prefix_meta_title( $title )
{
    // Check for single post only
    if ( !is_single() )
        return $title;

    preg_match_all( '/%%meta_([a-z0-9_-])+%%/i', $title, $matches );

    // No tags found
    if ( empty( $matches[1] ) )
        return $title;

    $post_id = get_the_ID();
    $replacements = array();
    foreach ( $matches[1] as $tag )
    {
        $replacements[] = get_post_meta( $post_id, $tag, true );
    }
    $title = str_replace( $matches[1], $replacements, $title );

    return $title;
}

In this code, I use regular expression to find all placeholder tags formed %%meta_XXX%% and use a simple loop to get all meta values for them, then str_replace is used to replace all placeholder tags with their values. Quite simple.

Now you can enter something like %%title%% [%%meta_isbn%%] - %%meta_book_author%% - %%meta_published_date%% in WordPress SEO settings page and you'll have "My book [1234] - By me - 5/2013" in the front end!

I believe that when we combine this technique with the idea of extending the code for other page types (in case 1), we can have the great flexibility to customize the meta title. And if you don't use the WordPress SEO plugin, you still can use this idea for your theme.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments