6

I want to try and remove automatic <p> tag and <br> tag addition for my custom post type.

It looks like

remove_filter( 'the_content', 'wpautop' );

will do this, but how would I make the change only for my chosen post type I've tried writing a function for this, but it doesn't seem to work:

function remove_tags() {
    global $post_type;

    if ( 'client' == $post_type ) {
        remove_filter( 'the_content', 'wpautop' );
    }
}
2
  • Where are you putting these functions?
    – s_ha_dum
    Jan 23, 2013 at 23:15
  • in my custom post type plugin file
    – user319940
    Jan 23, 2013 at 23:16

2 Answers 2

10

Hook into the_content before the wpautop filter has been called, check the post type, and remove the wpautop filter, which is added in wp-includes/default-filters.php with the default priority 10:

add_filter( 'the_content', 'wpse_82860_remove_autop_for_posttype', 0 );

function wpse_82860_remove_autop_for_posttype( $content )
{
    # edit the post type here
    'post' === get_post_type() && remove_filter( 'the_content', 'wpautop' );
    return $content;
}
3
  • 1
    After years, I still have problems understanding priority handling of filters in WP. Doesn't 0 mean that it's first called and after that the default wpauto is called with its priority of 10 and puts the <p>s back in?
    – Volker E.
    Feb 1, 2015 at 0:52
  • 2
    @VolkerE. wpautop is not called anymore, because it has been unregistered earlier.
    – fuxia
    Feb 1, 2015 at 0:55
  • 1
    Oh, for sure. Thanks! And it's not even late at my place right now. :}
    – Volker E.
    Feb 1, 2015 at 0:58
9
remove_filter('the_content','wpautop');

//decide when you want to apply the auto paragraph

add_filter('the_content','my_custom_formatting');

function my_custom_formatting($content){
if(get_post_type()=='my_custom_post') //if it does not work, you may want to pass the current post object to get_post_type
    return $content;//no autop
else
 return wpautop($content);
}

found this code on stackoverflow - looks to do the trick.

3
  • 2
    +1 for finding your own answer :) Please post the URL for the question where you found the code though.
    – s_ha_dum
    Jan 23, 2013 at 23:29
  • original can be found here stackoverflow.com/questions/7697465/…
    – user319940
    Jan 23, 2013 at 23:33
  • 3
    This will break when another plugin wants to do the same for another custom post type.
    – fuxia
    Jan 24, 2013 at 3:59

Your Answer

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

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