1

I'm using WP 4.7.3 and a custom template set (mh-magazine).

By default, WP sets a title tag like "Page title" - "Blog title".

Now all I want to achieve is to replace "-" into a pipe symbol for layout reasons. So the processed HTMl should look like:

My page title | My blog title

I thought that this is easy to achieve, but I see I need some help since I'm not an expert in PHP or WP.

1
  • Before changing your title separator, you should consider how screen readers will read your page title. While vertical pipe looks good it will now read <your site> "vertical pipe" <blog name>. This can provide an undesired experience for assistive users. See this article for a better understanding of this: standards-schmandards.com/2004/title-text-separators
    – Centerwork
    Commented Jun 3, 2022 at 16:54

3 Answers 3

3

It all depends on how your theme's header.php is structured, but the most likely way is via the document_title_separator filter, as in

add_filter ('document_title_separator', 'wpse_set_document_title_separator') ;

function
wpse_set_document_title_separator ($sep)
{
    return ('|') ;
}
6
  • +1, but those first brackets are not necessary in the return statement, kind of looks funny ;)
    – Fayaz
    Commented Apr 3, 2017 at 1:59
  • Personal style...I don't know why but I've been writing return statements as if they were a func call for 30 years Commented Apr 3, 2017 at 2:03
  • 30! PHP itself isn't that old! or perhaps you are referring to another language :)
    – Fayaz
    Commented Apr 3, 2017 at 2:29
  • 2
    @Fayaz the 1st lang I did production coding in was C, and while I don't quite go back this far, the 1975 version of the C Reference Manual - Bell Labs and Programming in C - A Tutorial "require" parens on return statements and my usage of such probably results from that...and return statements without them "kind of look funny" to me :-) Commented Apr 3, 2017 at 15:06
  • Cool background man, salute! In comparison, my experience sounds like yesterday :)
    – Fayaz
    Commented Apr 3, 2017 at 15:25
2

Possibility-1:

If your theme has:

  1. add_theme_support( 'title-tag' ); somewhere in your functions.php file, and

  2. wp_head() function call in header.php file

then using document_title_separator filter will work as @Paul's answer suggested.

If the filter doesn't work even after meeting those conditions above, then perhaps a plugin is overriding it. Try increasing the priority of the filter so that it runs last, like so:

function wpse262196_document_title_separator( $sep ) {
    return '|';
}
add_filter( 'document_title_separator', 'wpse262196_document_title_separator', PHP_INT_MAX );

or if the document_title_separator filter already exists in your theme's functions.php file, then change it there.


Possibility-2:

May be your theme doesn't include add_theme_support( 'title-tag' ); and instead uses the old <title><?php wp_title(); ?></title> in header.php file.

In this case, you may change it like: <title><?php wp_title( '|' ); ?></title> to change the separator.

Note: it's strongly advised to change it back to add_theme_support( 'title-tag' );, as wp_title() is likely to be deprecated.

Also, if it doesn't work, then may be your a plugin is replacing it using wp_title filter. In that case, use the filter below in your themes functions.php file to change the behaviour:

function wpse262196_wp_title( $title, $sep, $seplocation ) {
    return str_replace( " $sep ", " | ", $title );
}
add_filter( 'wp_title', 'wpse262196_wp_title', PHP_INT_MAX );

or if the wp_title filter already exists in your theme's functions.php file, then change it there.

Hopefully one the solutions will work for you.

1
  • @Toby The detail @Fayaz provides is great and is what I meant by "It all depends on how your theme's header.php is structured" Commented Apr 3, 2017 at 15:11
1

If you would like to go a plugin route, you can use the Yoast SEO plugin which is a free and powerful SEO tool.

Note: Yoast SEO is a full featured SEO plugin. So if you're not going to make use of the other SEO features of Yoast and changing the title separator is all you need, then this plugin will be overkill and the other answers given would be both much lighter weight and more appropriate for your needs.

  1. Install it from the WordPress repo (within your WordPress installation is easiest) as you would any other plugin. Then activate it.
  2. Head to Yoast > Dashboard > Features and make sure to turn on the "Advanced Settings Pages" and hit save. Yoast - turn on Advanced Settings
  3. Click the "Titles & Metas" option page. The very first tab ("General") will give you the ability to globally specify your title separators. Title Separators
4
  • An entire plugin only for a separator! What if he is using another SEO plugin? ;)
    – Fayaz
    Commented Apr 3, 2017 at 2:32
  • 1
    Anyways, different solutions are welcome. +1 for your effort. But please edit it and say something like if you are using Yoast SEO plugin, then ...
    – Fayaz
    Commented Apr 3, 2017 at 2:34
  • Oh for sure, if it's only for the title, yeah it's overkill which is why I mentioned that towards the end of the post ;). Any SEO plugin worth their salt should let you change the titles. I'll add that note towards the top so it's a bit clearer. Thanks for the constructive insight :)
    – Ian
    Commented Apr 3, 2017 at 2:39
  • I had a look at the plugin - but I found it not worth being installed just for that little trick. Anyway, it's surely great if someone needs all the SEO stuff around.
    – Toby
    Commented Apr 29, 2017 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.