1

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?

2
  • Where are you adding this conditional? Are you using any hooks?
    – Howdy_McGee
    Commented Apr 3, 2014 at 18:29
  • opps sorry. I am including this in functions.php, I am not using any hooks, but should I be?
    – adoboda
    Commented Apr 3, 2014 at 18:44

2 Answers 2

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
  • I'm doing some research about template redirect. How did you come upon this solution?
    – adoboda
    Commented Apr 3, 2014 at 20:52
  • This was actually a suggestion from one of the moderator on a similiar question I answered a while ago. The logic here is, and you can go and try it out yourself. Your remove_filter works perfectly when added in the after_setup_theme hook without the conditional statement. The problem is your conditional statement. Pages are loaded after the after_setup_theme hook, which renders your conditional ineffective. By the time template_redirect is executed, your pages are loaded, so any conditionals regarding pages can now be executed successfully Commented Apr 4, 2014 at 4:29
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.