[wp-trac] [WordPress Trac] #56299: WP_HTML_Walker: Inject dynamic data to block HTML markup in PHP

WordPress Trac noreply at wordpress.org
Thu Jul 28 13:08:23 UTC 2022


#56299: WP_HTML_Walker: Inject dynamic data to block HTML markup in PHP
-------------------------+-----------------------------
 Reporter:  zieladam     |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 This Trac ticket surfaces
 [https://github.com/WordPress/gutenberg/pull/42485 Gutenberg PR 42485 from
 GitHub] – let's keep the discussion there.

 Let's introduce a lean HTML processor:

 {{{#!php
 <?php
 $w = new WP_HTML_Walker('<div id="first"><img /></div>');
 $w->next_tag('img');
 $w->set_attribute('src', '/wp-content/logo.png');
 echo $w;
 // <div id="first"><img src="/wp-content/logo.png" /></div>
 }}}


 Why?

 Because dynamic blocks often need to inject a CSS class name or set <img
 src /> in the rendered block HTML markup but lack the means to do so:

 * [https://github.com/WordPress/gutenberg/pull/40859 strip whitespaces in
 render_block_core_cover]
 * [https://github.com/WordPress/gutenberg/pull/39658 Makes cover block
 dynamic and adds featured image binding]
 * [https://github.com/WordPress/gutenberg/pull/42236 Proof of concept: Add
 a CSS class to all static blocks with className supports]
 * [https://github.com/WordPress/gutenberg/pull/42122 Heading block: Add a
 wp-block-heading CSS class]

 At the moment, every block comes up with a custom-tailored regular
 expression. It is limiting, error-prone, and demands code duplication. It
 also leads to PRs such as strip whitespaces in render_block_core_cover.

 [https://github.com/WordPress/gutenberg/pull/42485 Gutenberg PR on GitHub]

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56299>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list