0

I have some Pages that have a specific string in their Title, e.g. "ASDF".

I want to remove this specific string from ever appearing inside the <title> tag on the HTML generated by my theme.

In my functions.php theme file, there's this line:

add_theme_support( 'title-tag' );

In my header.php theme file, there's this line:

    wp_head();

That last line generates a title tag that (say) looks something like this:

<title>Blah Blah ASDF &#8211; Blog Name</title>

But if "ASDF" appears in the title tag, I want the "ASDF" string to be removed, so the output would be like:

<title>Blah Blah &#8211; Blog Name</title>

I tried adding the following code to my function.php file:

function tweak_custom_title( $title ) {
    return str_replace('ASDF', '', $title); 
}
add_filter( 'wp_title', 'tweak_custom_title');

But that didn't work.

Am I missing a central concept here?

1

FYI, I was able to get this to work by add a filter to 'document_title_parts' rather than 'wp_title', like so:

 add_filter( 'document_title_parts', 'tweak_custom_title' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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