prevent "&" from being converted to "&" in the gutenberg shortcode block

hi Guys.

I am adding an image URL directly in the Gutenberg shortcode block and it is using & in the URL, after the page is saved and refreshed the & is converted to & and breaks the URL, is there a way to prevent this?

Thank you.

Something like this but it breaks the block…

function gutenberg_shortcode_block_editor_assets() {
wp_add_inline_script(
‘wp-blocks’,
‘wp.hooks.addFilter( “blocks.getSaveElement”, “gutenberg_shortcode_block”, function( element, blockType, attributes ) {
if ( blockType.name === “core/shortcode” && attributes.shortcode.indexOf(“[fwdsap”) !== -1 ) {
var div = document.createElement( “div” );
div.innerHTML = attributes.content;
var decodedText = div.firstChild.getAttribute( “text” );
div.firstChild.setAttribute( “text”, decodedText.replace( /&/g, “&” ) );
attributes.content = div.innerHTML;
}
return element;
} );’
);
}
add_action( ‘enqueue_block_editor_assets’, ‘gutenberg_shortcode_block_editor_assets’ );

Hello,

I checked on this in Gutenberg and it is happening for me also, without any plugin activated, with latest WordPress version. I looked this up and indeed, some other people also reported this as a Gutenberg Editor bug, check below (the first issue listed below is currently still open, commenting on it might be helpful, so it can get more attention):
https://github.com/WordPress/gutenberg/issues/26362
https://github.com/WordPress/gutenberg/issues/19371
https://github.com/WordPress/gutenberg/issues/14186

I think the cleanest solution is to wait for the Gutenberg team to solve this issue, however, if solving this is urgent for you, if the shortcode was created by you, I recommend modifying it, so it expects the & characters being encoded in the URL, so the shortcode can decode them, using htmlspecialchars_decode. Another option, if possible, is to modify the shortcode to not be needed to add the URL parameters directly in the URL, but in a separate shortcode parameter, as suggested here: https://wordpress.org/support/topic/problem-with-gutenberg-escaping-shortcode-attributes/

I hope this helps.

Cheers!

Thank you for your help!

1 Like

I have changed the shortcode to this format and it is better Problem with Gutenberg escaping shortcode attributes | WordPress.org

Thank you for your help Szabi!

1 Like

This is the plugin I need it for Spectrum Audio Player by FWDesign | CodeCanyon

1 Like

I am glad to help. :slight_smile:

1 Like