Stop wpautop from running in Gutenberg custom html blocks

Refresh

1 weeks ago

Views

10 time

2

I am frontend developer and Wordpress is having its way with me. I have tried every solution I've found on every marginally-related post from Stack Exchange to Quora and back and I've accomplished nothing. Please help me, I'm one failed attempt away from turning to black magic for a solution.

Maybe I borked one of the many filters I've already tried. It's possible that I could have broken them during modification. I am, after all, a frontend dev traversing the nightmare land of PHP.

Also, I swear to Bill Murray if anyone marks this as a duplicate I'll hunt you down and cry at your feet until you feel as miserable as I do right now. I have scoured every post you could possibly link and none of them worked.

Problem:

I want to stop wpautop from massacring my custom html blocks, you know, because it's supposed to be CUSTOM HTML that Wordpress should leave alone. But the Worpress overlords developers seem to think that every <a>, <img>, and <button> tag need themselves a heaping helping of <p> tags, even inside CUSTOM HTML blocks.

Solution A:

Prevent wpautop from running inside custom html blocks.

Solution B:

Prevent wpautop from running on a specific page.

Solution A is preferred but at this point I'll take any solution that can actually tame the deranged wpautop function.


Attempts at disabling wpautop for custom html block elements

Gutenberg custom blocks php render issue

Looking at the post above I attempted to make this work with my limited PHP knowledge:

function die_wpautop_die()
{
    $block_content = '';
    remove_filter('the_content', 'wpautop');
    remove_filter('the_excerpt', 'wpautop');

    add_filter('the_content', 'wpautop');
    add_filter('the_excerpt', 'wpautop');

    return $block_content;
}
add_action( 'wp-head','die_wpautop_die' );

I'm not sure what post I got this one from
Not that it matters since it didn't work either.

add_filter( 'render_block', function ( $block_content, $block ) {
    if ( 'acf/featured-pages' === $block['customhtml'] ) {
        remove_filter( 'the_content', 'wpautop' );
    } elseif ( ! has_filter( 'the_content', 'wpautop' ) ) {
        add_filter( 'the_content', 'wpautop' );
    }

    return $block_content;
}, 10, 2 );

Attempts at disabling wpautop on a specific page

Stop Wordpress Wrapping Images In A ā€œPā€ Tag

function filter_ptags_on_images($content)
{
    return preg_replace('/<p>(\s*)(<img .* \/>)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

or with optional filter:

return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);

My variation:

function filter_ptags_on_buttons($content)
{
    return preg_replace('/<p>(\s*)(<button .* \/>)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'filter_ptags_on_buttons');

Wordpress - Apply remove_filter only on one page

if (!is_page_template('my-page.php'))
  remove_filter('the_content', 'wpautop');

wpautop filter off for pages but on for posts

function disable_wpautop_for_pages(){
   if ( is_page()){
       remove_filter( 'the_content', 'wpautop' );
   }
}
add_action('init', 'disable_wpautop_for_pages');

My variation:

function die_wpautop_die_seriouslypls(){
    if (is_page(145919)):
        remove_filter('the_content', 'wpautop');
    endif;
}
add_filter('the_content', 'die_wpautop_die_seriouslypls');

How do I remove auto paragraph formatting for pages ONLY, and not posts (WordPress)

function remove_p_on_pages() {
    if ( is_page() ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );
    }
}
add_action( 'wp_head', 'remove_p_on_pages' );

My variation:

function die_wpautop_please_die() {
    if ( is_page(145919) ) {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );
    }
}
add_action( 'wp_head', 'die_wpautop_please_die' );

I've even tried CSS Tricks posts

function you_win_wpautop_just_kill_me_now($content){
   return preg_replace('ā€‹/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'you_win_wpautop_just_kill_me_now');

Unfortunately for me, dear reader, none of the above attempts succeeded. If you can fix one of them or point me in the right direction I'd be eternally grateful.

0 answers