0

I'm developing a theme for Themeforest and one of their requirements is about the tag. Themeforest states their requirement as follows:

The theme must let WordPress add and manage the title. This is done by adding add_theme_support( 'title-tag' ); to functions.php instead of using wp_title() in the document head.

The current output is on the home page: 'Site title - Site tagline'. On other pages it is 'Post or page title - Site tagline'.

The separator is '-'. Can anyone give an example of a filter for this, so that I can change the separator to '|' or a character of my choice?

Many thanks in advance!

9
  • The title tag shouldn't be determined by the theme. Remove the <title> tag entirely. WordPress will output it at <?php wp_head(); ?>. Commented Jul 6, 2020 at 12:00
  • Ok @JacobPeattie thanks. I removed it and currently it displays only the website name. How should I modify that?
    – ralphjsmit
    Commented Jul 6, 2020 at 12:01
  • On every page? Make sure to remove the entire <title> tag, not just the wp_title() part. So bloginfo() should be removed to, as well as the HTML. Commented Jul 6, 2020 at 12:10
  • Yes, I removed everything. There is now nothing in my header.php file. The only thing I have is the theme support for title tag in functions.php. Yeah, I want to modify it on every page, so [PAGE NAME OR POST NAME OR CATEGORY NAME OR WHATEVER] [SEPERATOR ( "|" )] [BLOGINFO NAME].
    – ralphjsmit
    Commented Jul 6, 2020 at 12:11
  • It looks like title-tag uses wp_get_document_title() which, at first glance, does append the site title if you're not on the home page? There are filters there to override the separator and more too.
    – Rup
    Commented Jul 6, 2020 at 12:48

1 Answer 1

2

You use the document_title_separator filter.

So in your case:

<?php
function theme_prefix_filter_document_title_separator() {
    return '|';
}
add_filter( 'document_title_separator', 'theme_prefix_filter_document_title_separator' );
?>
2
  • Hey Fränk, thanks for the answer! Is it correct that a filter always requires some input and then replaces something with that input?
    – ralphjsmit
    Commented Jul 6, 2020 at 15:01
  • 1
    More or less: in this case, the calling code passes in its default value for separator, -, as an argument to the filter. But in this case you're just discarding that value and returning a new one. (I'd still probably accept the argument and pass argument count 1 to add_filter, but I guess it doesn't matter.)
    – Rup
    Commented Jul 6, 2020 at 15:06

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.