Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcodes are evaluated instead of printed as text #668

Closed
TobiasBg opened this issue Dec 18, 2022 · 10 comments · Fixed by #696
Closed

Shortcodes are evaluated instead of printed as text #668

TobiasBg opened this issue Dec 18, 2022 · 10 comments · Fixed by #696
Labels
bug Something isn't working

Comments

@TobiasBg
Copy link

When adding a Shortcode to a core/code block, the Shortcode is printed as text instead of being evaluated (this is useful e.g. for documentation pages of a plugin's Shortcodes).

When then activating "Syntax-highlighting Code Block (with Server-side Rendering)" version 1.3.1 (tested on WP 6.1.1 and trunk), the Shortcode is however evaluated.

To reproduce this:

  1. Add a small plugin that adds a Shortcode:
<?php
/*
Plugin Name: Shortcode Test
Description: Shortcode Test
Version: 1.0
*/

add_shortcode( 'test', 'shortcode_test' );
function shortcode_test( $atts, $content ) {
    return 'Just a test';
}
  1. Add a "Code" block to a post or page with the Shortcode [test] as the content.
  2. Compare the page output with "Syntax-highlighting Code Block (with Server-side Rendering)" deactivated and activated.
@TobiasBg
Copy link
Author

TobiasBg commented Dec 18, 2022

My feeling is that the output that is generated by the plugin is then again run through do_shortcode() by WordPress, which does not seem to happen for the normal core/code block.

I then tried to use the escaped Shortcode [[test]], which however seems to have a core or Gutenberg bug: https://core.trac.wordpress.org/ticket/57351

As a temporary remedy, inserting a simple replacement

$content = str_replace( '[', '&#91;', $content );

before

if ( ! DEVELOPMENT_MODE ) {
	set_transient( $transient_key, compact( 'content', 'attributes' ), MONTH_IN_SECONDS );
}

in the plugin's render_block() function seems to work.

@westonruter westonruter added the bug Something isn't working label Dec 21, 2022
@westonruter
Copy link
Owner

Thanks for the report. I'm not sure why this is happening, as we're not applying the_content filters or manually doing shortcodes with the output. I'll try to reproduce the issue in the new year.

You've also verified the issue happens when your shortcode plugin and this plugin are only active with a generic core theme? Is there any other plugin/theme that is causing the issue?

@TobiasBg
Copy link
Author

Yes, all this is just with WP 6.1.1, the test Shortcode, and a default block theme.

My feeling (see the details in my previous reply) is that the output is run through the default the_content filters (which includes do_shortcode()), but as the &#91; HTML entity has been replaced by a [, that then gets recognized as a Shortcode.

@westonruter
Copy link
Owner

Ah, a Block theme. Can you try with an older theme, like Twenty Twenty One?

@TobiasBg
Copy link
Author

Sure, just tested Twenty Twenty One, Twenty Twenty, Twenty Seventeen, and Twenty Ten.
Same problem everywhere, so it's not related to block themes. :-/

@westonruter
Copy link
Owner

Sorry for the delay. I can reproduce the issue.

This was also reported as an issue for Gutenberg in WordPress/gutenberg#13927 and fixed in WordPress/gutenberg#13996. The fix was for the block's save function to output &#91; instead of [. However, this is getting undone by us since we need the entities removed for passing into highlight.php:

$content = html_entity_decode( $matches['content'], ENT_QUOTES );

if ( $language ) {
$r = $highlighter->highlight( $language, $content );
} else {
$r = $highlighter->highlightAuto( $content );
}

So as you suggest, re-encoding the special characters may be the solution. Nevertheless, it'll have to be careful to not accidentally encode any of the markup that highlight.php is generating. This may require doing two passes, first to replace all tags with placeholders, then do the entity encoding, and then replace the placeholders with the original tags. I'll give it a shot.

@TobiasBg
Copy link
Author

TobiasBg commented Mar 4, 2023

Thanks for looking into this! I've been using that str_replace() from #668 (comment) for now, and that seems to have helped a little bit (e.g. on this page) but not if that page content is loaded in a Loop (see this page and open the "The Shortcode [table id=N /]" section). But that might also be caused by Core#57351 or rather Core#55996 .

But something more robust than that str_replace() will probably be good.

@westonruter
Copy link
Owner

I've got a PR open to address the shortcode problem in #696

@westonruter
Copy link
Owner

This has been released on WordPress.org as part of v1.4.0.

@TobiasBg
Copy link
Author

Great! Thanks, @westonruter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants