Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress title tag filters

You can dynamically set the title tag with PHP code using the WordPress pre_get_document_title or document_title_parts filters if you're using Genesis SEO features:

add_filter( 'pre_get_document_title', 'custom_quick_title' );
function custom_quick_title() {
  return 'My custom title';
}

The document_title_parts filter gives you more fine-grained control if you just wanted to override the title or tagline, for example:

add_filter( 'document_title_parts', 'custom_single_title' );
function custom_single_title( $parts ) {
  $parts['title'] = 'My custom title';
  $parts['tagline'] = 'My site description';

  return $parts;
}

These features are part of WordPress itself, and are documented here:

If the site uses an SEO plugin such as Yoast SEO, you'd need to use that plugin's filters instead. For example, with Yoast:

add_filter( 'wpseo_title', 'custom_yoast_title' );
function custom_yoast_title() {
  return 'My custom title';
}

Yoast filters are documented here: https://yoast.com/wordpress/plugins/seo/api/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.