So I was running into an issue recently with Yoast SEO.  If you don’t know Yoast SEO is a great SEO tool for WordPress.  However I wanted to define a specific title to a template page and couldn’t find a way to do it.  With a little bit of looking into Yoast SEO I was able to find a solution. I didn’t see anything on the web related to this so I figured I’d share it.  This is for a custom database table I imported and gets the ID from the URL. However you could use it for anything with a little bit of modification.  For instance beneath that is setting a page title I setup, beneath that is for a specific post. Simply drop these in your functions.php and you are good to go!

Change title on specific template
[php]
function video_filter_wp_title( $title ) {
if ( is_page_template( ‘template-video-view.php’ ) ) {
$id = basename($_SERVER[‘REQUEST_URI’]);
global $wpdb;
$video = $wpdb->get_row("SELECT * FROM videos WHERE id = ".$id);
return ‘Video: ‘.$video->title.’ – ‘.get_bloginfo( ‘name’ );
}
// Otherwise, don’t modify the document title
return $title;
}
add_filter( ‘wpseo_title’, ‘video_filter_wp_title’ );
[/php]

Change title on specific page
[php]
function page_filter_wp_title( $title ) {
if ( is_page( 999 ) ) { //replace 999 with your page ID
$title = "Some super amazing title";
return $title.’ – ‘.get_bloginfo( ‘name’ );
}
// Otherwise, don’t modify the document title
return $title;
}
add_filter( ‘wpseo_title’, ‘page_filter_wp_title’ );
[/php]

Change title on specific post
[php]
function post_filter_wp_title( $title ) {
if ( is_single( 999 ) ) { //replace 999 with your post ID
$title = "Some super amazing title";
return $title.’ – ‘.get_bloginfo( ‘name’ );
}
// Otherwise, don’t modify the document title
return $title;
}
add_filter( ‘wpseo_title’, ‘post_filter_wp_title’ );
[/php]


Leave a Reply

Your email address will not be published. Required fields are marked *