Set Multiple WordPress Page Titles With The SEO Framework

I love the plugin The SEO Framework by Sybre Waaijer. It's a breath of fresh air compared to the bloated, distracting, and overly-promotional SEO plugins that have been so popular in WP for years. One advantage of using a plugin that's well-written and accessible is that it's easier to make little tweaks to fit the plugin to better suit your needs. In my case, I recently needed the option to display multiple page titles: one title for browsers, and a custom override title for Facebook, whose algorithm now censors content with rude words.

Sybre's source code is easy to follow, and the documentation is pretty good, but I thought I'd post my specific configuration in case anyone is looking to perform similar modifications.

This hook calls a custom function when The SEO Framework is preparing to output the title:

add_filter( 'the_seo_framework_ogtitle_output', 'og_title_override', 10, 2 );

This custom function checks to see if a particular meta value is set on the post in question. If the meta value exists, it returns that value as the title, otherwise it returns the title set by the SEO Framework.

/**
 * Injects a replacement Open Graph title when user fills out custom metabox.
 *
 * @param string $title The current title.
 * @param int $id The Post, Page or Term ID.
 * @return string Title. User input title is santized on metabox save
 */

function og_title_override( $title = '', $post_id = 0) {

    $user_title = get_post_meta( $post_id, '_fb_override_og_title', true );

    if  ( $user_title != '' ) {
      $title = $user_title;
    }

    return $title;
}

A very simple function, made easy to implement by a well-written plugin.

Photo credit: Adam Birkett