Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove auto <p> from all content except posts

Tags:

wordpress

I am using this code to remove the autop generated by wp.

remove_filter('the_content', 'wpautop');

All good, but I need to have the autop only in the blog articles.

After searching I found that I can detect if its a post or not, mixing another question and wp doc came up with this wasnt working until Matt pointed the error. Belows code is working right on wp 4.7.3.

remove_filter('the_content','wpautop');

add_filter('the_content','if_is_post');

function if_is_post($content){
    if(is_singular('post'))
        return wpautop($content);
    else
        return $content;//no autop
}
like image 216
Tyra Pululi Avatar asked Sep 19 '25 05:09

Tyra Pululi


1 Answers

If you want the wpautop filter applied only in posts, then your if statement is backwards. Right now, you're removing the filter and if the current post type is post, it stays removed. Believe your code should look like this.

remove_filter('the_content','wpautop');

add_filter('the_content','if_is_post');

function if_is_post($content){
if(is_singular('post'))
    return wpautop($content);
else
    return $content;//no autop
}
like image 152
mburesh Avatar answered Sep 22 '25 05:09

mburesh