Make WordPress Core

Opened 12 months ago

Last modified 6 months ago

#58174 new defect (bug)

A shortcode block that evaluates to nothing, renders as a space in the HTML

Reported by: asafm7's profile asafm7 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Shortcodes Keywords:
Focuses: Cc:


A shortcode block that evaluates to nothing renders as a space in the HTML.

A screenshot:

I've noticed it while using an [acf] shortcode referencing an empty field, but it also happens with an empty [audio] core shortcode.

This makes styling with CSS cumbersome. For example, the :empty selector can't be used.

When I remove the shortcode block, leaving the column block completely empty, the redundant space isn't being rendered (so it isn't a browser issue).

I've tried to reverse-engineer it really hard, even trying to catch the HTML at the last minute in the get_the_block_template_html() function, but I couldn't find the culprit spaces.

Change History (4)

#1 @asafm7
12 months ago

On second thought it might be some sort of line break. Although I've searched for \n and \r between the tags and couldn't find anything.

#2 @asafm7
12 months ago

Okay, so it is a line break (\n).

It is being added here:

    $content = preg_replace_callback("/$pattern/", 'do_shortcode_tag', $content);

In the do_shortcode() function in shortcodes.php.

I made sure the 'do_shortcode_tag' output never contains \n. But still, after this line, the $content contains \n in place of empty shortcodes. It doesn't make a lot of sense, but it seems there is some kind of bug with the native preg_replace_callback() PHP function.

Adding a

$content = str_replace(">\n", ">", $content);

fixes the problem.

There is no filter after this point though, so it seems it can't be fixed without modifying the core code.

#3 @asafm7
12 months ago

My research was inaccurate. I ended up solving it with:

add_filter("render_block_core/shortcode", "filter_core_shortcode_block", 100, 2);

function filter_core_shortcode_block($block_content, $block) {
    $block_content = rtrim($block_content, "\n");

    return $block_content;

#4 @sabernhardt
6 months ago

  • Component changed from General to Shortcodes
Note: See TracTickets for help on using tickets.