I have a plugin that uses this filter to add some content to a Custom Post Type.
// From plugin: add_filter( 'the_content', 'sixtenpresssermons_get_meta', 15 );
I’d like to remove that filter on certain views (singles).
I’d like to do so using code in my theme files – without editing the plugin.
I tried with the code below in functions.php, but obviously it’s not working.
// Not working, in functions.php
if ( is_single('sermon') ) {
remove_filter( 'the_content', 'sixtenpresssermons_get_meta', 15 )
}
Can I use conditionals with a filter like this, or how can I remove the filter on singles only?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can do this in functions.php, but you have to make sure the function is hooked into wp. Any earlier than that and is_single is undefined.
Try adding this to functions.php:
/**
* Unhooks sixtenpresssermons_get_meta from the_content if currently on single post.
*/
function sx_unhook_sixtenpresssermons_get_meta() {
// Do nothing if this isn't single post
if ( ! is_single() ) {
return;
}
remove_filter( 'the_content', 'sixtenpresssermons_get_meta', 15 );
}
add_action( 'wp', 'sx_unhook_sixtenpresssermons_get_meta' );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0