1

I'm learning how to use short codes but I don't undrestand why WordPress keeps rendering broken paragraph tags.

I created a file called wp-content/themes/twentytwentythree/templates/single.html with the following content:

<main class="wp-block-group" style="margin-top:var(--wp--preset--spacing--50)">
<!-- wp:shortcode -->
[wl_event_head]
<!-- /wp:shortcode -->
</main>

I then made a file called wp-content/plugins/wl/wl.php with the following contents:

/*
 * Plugin Name: Hello
 * Description: World
 */

function wl_event_head($att) {
  return '<section>
  <div>
    <h1>Hello WP</h1>
  </div>
</section>';
}

add_shortcode('wl_event_head', 'wl_event_head');

When I look View Source of FireFox, I see this:

<main class="wp-block-group" style="margin-top:var(--wp--preset--spacing--50)">
<section>
<div>
<h1>Hello WP</h1>
</p></div>
</section>

</main>

Why is there a closing p tag? I noticed that if I delete some spaces in my return string for function wl_event_head($att), then sometimes maybe the closing p tag goes away.

What am I doing wrong?


Note: I tried putting this code in my wp-content/themes/twentytwentythree/functions.php

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

It did not fix the problem.

1 Answer 1

0

I worked around my problem by minifying my html in my plugin. So I made a function called wl_minify_html(String $htmlString) (which I found some another stack overflow answer somewhere). So here's what my plugin looks like now:

<?php

function wl_minify_html($htmlString) {

    $search = array(
        '/\>(\s)+\</s',     // strip white space between tags
        '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
        '/[^\S ]+\</s',     // strip whitespaces before tags, except space
        '/(\s)+/s',         // shorten multiple whitespace sequences
        '/<!--(.|\s)*?-->/' // Remove HTML comments
    );

    $replace = array(
        '><',
        '>',
        '<',
        '\\1',
        ''
    );

    $buffer = preg_replace($search, $replace, $htmlString);

    return $buffer;
}


function wl_event_head($att) {
  return wl_minify_html('<section>
  <div>
    <h1>Hello WP</h1>
  </div>
</section>');
}

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.