I'm trying to modify the WordPress page title. However I'm fairly new to WordPress functions, hooks (filters & actions) and so on.

What I could achieve is to change and modify WordPress titles in the <body>. I also was able to change to Page title (<head>), but not to modify it. E.g.:

Original page title:
My house is red

Modified page title (| -> is used for seperate examples):
My house is green | My house is red and blue

So I want to add or remove specific characters or numbers/integers of my page title. To be clear what I exactly try to achieve is:

I want to hook the original page title of my (custom post types) pages and "filter" the first three numbers of my title.

My titles of my custom post types are numbered and look like:

  • 000 Main Category X
  • 100 Sub Category X
  • 101 Sub Category 01 X
  • 200 Sub Category Y
  • ... and so on

I changed the titles for WordPress, so my custom post types are sorted correctly for the back/forward navigation. The module I used isn't able to sort the custom post types on its own.

So I don't want any numbers (always three numbers) in front of my page titles. I planned to "filter" them out by using (one of these php functions):

  • $x = ltrim($x, 'example123');
  • $b = substr($a, 3);

As far I know these filter hooks are achieve:

  • pre_get_document_title
  • document_title_separator
  • document_title_parts

Furter I used these WordPress functions/filter hooks:

function change_page_title( $title ) {
    return $title;
add_filter( 'pre_get_document_title', 'change_page_title' );


function change_page_title( $title ) {
    $title['title'] = 'Changed title';
add_filter( 'document_title_parts', 'change_page_title' );

So I'm sorry if there might be already a solution for my problem, but I couldn't find any and I'm at the moment not able to solve if on my own. Therefore I hope you might be able to help me. Also you if you have a good recommendation to get better in WordPress development (ressources like videos, courses etc.) I would appreciate it. I do know HTML, CSS, JS and also basic understanding in programming and PHP.

I tried modifying the functions above, like that (and much more, but it didn't work):

function change_page_title( $title ) {
    return $title;                     // works as expected
    // return ' test';                 // works as expected
    // return $title . ' new title';   // doesn't work

    // $new = $title;
    // $new2 = $title . ' test2';
    // return $new;                    // seem also to work
    // return $new2;                   // doesn't work
add_filter( 'pre_get_document_title', 'change_page_title' );

So these were some things I tried to get it working, or rather to test if I'm able to modify the title. I'm sure there is a simple solution and I'm making some sort of stupid mistake.

Thank you for your time.

  • Filters always return something, but not all of the filters you tried do this. Think of it like a water filter, it wouldn't make sense if water went into a filter, then mysteriously vanished with no water coming out of the filter. It's the same in WP with filters, something goes in, something always comes out. If you don't return something, then null comes out. There's no point changing $title['title'] if you don't return it!
    – Tom J Nowell
    Jul 29, 2020 at 9:56

1 Answer 1


It's important to read the documentation for filters. The documentation for pre_get_document_title says (emphasis mine):

Filters the document title before it is generated.


(string) The document title. Default empty string.

So when using pre_get_document_title, the title has not been set yet, so when you do this:

return $title . ' new title';

$title is — as documented — an empty string, so the result will be:

' new title'

So if you want to modify the existing title, you need to use document_title_parts, where you'll modify $title['title']:

    function( $title ) {
        $title['title'] = substr( $title['title'], 4 ); // This needs to be 4, to account for the space.

        return $title;
  • thank you very much for the answer (solution) and the explanation. It actually makes all sense now. It's just the first paragraph which describes it and I just didn't could wrap my head around it... Thank you again! Jul 29, 2020 at 14:26

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.