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.
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!
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!
I am glad to help.