Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Created September 27, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcernis/8c6b62b05f65f151b5e23844640dd98e to your computer and use it in GitHub Desktop.
Save nickcernis/8c6b62b05f65f151b5e23844640dd98e to your computer and use it in GitHub Desktop.
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