Question

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?

Was it helpful?

Solution

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' );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top