[wp-trac] [WordPress Trac] #47420: Block markup containing HTML in block attributes is corrupted when using wp_insert_post
WordPress Trac
noreply at wordpress.org
Tue May 28 17:19:45 UTC 2019
#47420: Block markup containing HTML in block attributes is corrupted when using
wp_insert_post
--------------------------+------------------------------
Reporter: modernnerd | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Resolution:
Keywords: 2nd-opinion | Focuses:
--------------------------+------------------------------
Changes (by jeremyfelt):
* keywords: => 2nd-opinion
Comment:
Hi @modernnerd, thanks for opening a ticket.
As you've noticed, the HTML stored in a block attribute needs to be
encoded properly. WordPress doesn't necessarily have a core function for
doing this directly, but a combination of things will help:
{{{
<?php
// String from non-Gutenberg data source.
$string = 'Hey, a <a href="https://jeremyfelt.com/">link to my
website</a>.';
$attribute = wp_json_encode( $string, JSON_HEX_TAG | JSON_HEX_APOS |
JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
// And $attribute is now:
// "Hey, a \u003Ca href=\u0022https:\/\/jeremyfelt.com\/\u0022\u003Elink
to my website\u003C\/a\u003E."
// If storing with wp_insert_post(), etc...
$attribute = addslashes( $attribute );
$block = '<!-- wp:jf/custom { "data":' . $attribute . ' } /-->';
}}}
I'd support a helper function for this like `wp_block_attribute_encode()`,
though I could also see it being solved with documentation that explains
the extra flags on `wp_json_encode()`.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47420#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list