What is the correct way to write this conditional statement?

I’d like to remove auto formatting ‘wpautop’ from specific pages. Here is what I’ve got, but it doesn’t appear to work:

if ( is_page ( 'services' ) ) {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
};

I am quite new to php, I have done some research to see how to write this statement, but I have had trouble finding an example that addresses my need. What would be the correct way to write this conditional statement?

Related posts

Leave a Reply

2 comments

  1. You need to add your function in the template_redirect hook. You need to first wait for wordpress to finish loading pages before you can add your hook to remove wpautop, otherwise your hook will simply get run over. So your funtion will look like this

    function pietergoosen_remove_wpautop() {
    if ( is_page ( 'services' ) ) {
    remove_filter( 'the_content', 'wpautop' );
    remove_filter( 'the_excerpt', 'wpautop' );
    }
    }
    
    add_action( 'template_redirect', 'pietergoosen_remove_wpautop' );
    
  2. Here’s what I’ve found based off this question at Stack Overflow

    You’ll have to remove wpautop entirely then add it back in whenever needed, which I agree in the linked comment, is unfortunate:

    remove_filter('the_content', 'wpautop');
    remove_filter('the_excerpt', 'wpautop');
    
    /** Change How The Content Works **/
    function no_content_autop($content){
        global $post;
    
        if($post->post_type != 'services')
            return wpautop($content);
    
        return $content;
    }
    add_filter('the_content','no_content_autop');
    
    /** Change How The Excerpt Works **/
    function no_excerpt_autop($content){
        global $post;
    
        if($post->post_name != 'services')
            return wpautop($content);
    
        return $content;
    }
    add_filter('the_excerpt','no_excerpt_autop');
    

    The question is old so you may want to watch this question for a day or two and see if somebody comes up with an improved answer.