Conducting this answer because it was a search result on which I clicked the button while searching for the_title filter hook the_title , ignoring the filter effect for the navigation items.
I worked on a section in a topic in which I wanted to add buttons to the page title in the title tag.
It looked something like this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
I then "connected" as follows:
add_filter( 'the_title', 'my_callback_function' );
However, above the target is literally everything that calls the_title filter, and this includes navigation elements.
I changed the definition of the filter hook as follows:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
Almost every call to the_title filter passes parameter 1 as $post->post_title and parameter 2 as $post->ID . Find the core WordPress code for apply_filters( 'the_title'* , and you will see for yourself.
So, I decided to add a third parameter for situations where I want to target certain elements that call the the_title filter. Thus, I still benefit from all callbacks that apply to the default the_title filter binding, and also have the ability to semi-uniquely target elements that use the the_title filter the_title with the third parameter.
This is a simple boolean parameter:
/** * @param String $title * @param Int $object_id * @param bool $theme * * @return mixed */ function filter_the_title( String $title = null, Int $object_id = null, Bool $theme = false ) { if( ! $object_id ){ return $title; } if( ! $theme ){ return $title; } // your code here... return $title; } add_filter( 'the_title', 'filter_the_title', 10, 3 );
Label the variables as you like. This is what worked for me, and it does exactly what I need for this. This answer may not be 100% relevant to the question asked, but it was here that I came when I was looking for a solution to this problem. Hope this helps someone in a similar situation.