disable wpautop on multiple custom post types

2019-09-12 08:12发布

I am trying to figure out the best way to disable wpautop in 4 custom post types.

I am using this snippet:

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

    function wp1_remove_autop_for_posttype( $content )
{

'esh' === get_post_type() && remove_filter( 'the_content', 'wpautop' );
return $content;
} 

I also need to remove it for custom post types named menu, op, external. do i just copy and paste this and change the function name or is there a cleaner way to do it?

wisdom appreciated! thanks!

标签: php wordpress
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-12 08:41

You can make array with all CPT that you need to remove wpautop from, and then in this hook to do:

$cpts = ['esh', 'menu', 'op', 'external'];
if ( in_array( get_post_type(), $cpts ) ) { 
    remove_filter( 'the_content', 'wpautop' );
}

You can check this function get_post_types(), if you want to grab CPTs automatically. For instance, if you want to loop through all your CPTs:

$cpts = get_post_types(['_builtin' => false, 'public' => true], 'names');

or of course, you can put other arguments to select needed types that you want to loop through.

查看更多
登录 后发表回答