Posting this answer because it was the search result I ended up clicking on while searching about targeting the filter hook the_title
while ignoring the filter effect for navigation items.
I was working on a section in a theme which I wanted to add buttons to the page title within the heading one tag.
It looked similar to this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
I was then "hooking in" like this:
add_filter( 'the_title', 'my_callback_function' );
However, the above targets literally everything which calls the_title
filter hook, and this includes navigation items.
I changed the filter hook definition like this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
Pretty much every call to the_title
filter passes parameter 1 as the $post->post_title
and parameter 2 as the $post->ID
. Search the WordPress core code for apply_filters( 'the_title'*
and you'll see for yourself.
So I decided to add a third parameter for situations where I want to target specific items which call the_title
filter. This way, I can still receive the benefit of all callbacks which apply to the_title
filter hook by default, while also having the ability to semi-uniquely target items that use the_title
filter hook with the third parameter.
It's 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 however you want. This is what worked for me, and it does exactly what I need it to do. This answer may not be 100% relevant to the question asked, but this is where I arrived while searching to solve this problem. Hope this helps someone in a similar situation.