最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

conditional tags - How should I be using filters and is_single together?

programmeradmin1浏览0评论

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
  • It's too early to do it that way, WP doesn't know if is_single is true or false yet in functions.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:10
  • Ok. That makes sense (I think.) Thanks for explaining it in plain english to me. I guess that is why most other code I see wrap the condition in a function? – Anders Carlén Commented Aug 13, 2020 at 12:13
  • If I were to try and wrap it in a function, is there a particulary good action (is that the correct term?) to try and use, so it's executed at the right time? – Anders Carlén Commented Aug 13, 2020 at 12:15
  • It's generally not a great idea to make function calls in functions.php or a plugin at the top level, it avoids the problem of ordering. I was going to suggest adding if ( 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:17
  • Ok, no worries. For what it's worth, the simple remove_filter statement in functions.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
Add a comment  | 

1 Answer 1

Reset to default 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' );
发布评论

评论列表(0)

  1. 暂无评论