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?
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?
Share Improve this question edited Aug 13, 2020 at 11:48 Anders Carlén asked Aug 13, 2020 at 11:41 Anders CarlénAnders Carlén 156 bronze badges 5 |1 Answer
Reset to default 1You 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' );
is_single
is true or false yet infunctions.php
. Depending on how the plugin is built it might not have had a chance to even add the filter yet for you to remove it – Tom J Nowell ♦ Commented Aug 13, 2020 at 12:10functions.php
or a plugin at the top level, it avoids the problem of ordering. I was going to suggest addingif ( is_singular() ) { return; }
to the hook as an answer but I see you can't change the original, I'm not sure what the appropriate action to hook into for this would be right now to write an answer though – Tom J Nowell ♦ Commented Aug 13, 2020 at 12:17remove_filter
statement infunctions.php
(without the conditional) works. But removes it from all instances, and not just singular. – Anders Carlén Commented Aug 13, 2020 at 12:20