Last active
October 8, 2025 17:07
-
-
Save ironprogrammer/6cb0d2a07b937c819bde2c3ff5ec4d34 to your computer and use it in GitHub Desktop.
Trac 57575 "Sandbox" MU Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Trac 57575: WP_HTML_Tag_Processor Testing | |
| * https://core.trac.wordpress.org/ticket/57575 | |
| * | |
| * Also refer to unit test suite for additional examples: | |
| * https://github.com/WordPress/wordpress-develop/tree/5b4ad8fa5634177102ee5771a2b7edfd86927f8b/tests/phpunit/tests/html | |
| * | |
| * ******************************** WARNING *********************************** | |
| * * This plugin intentionally prevents the normal WordPress UI from loading. * | |
| * * Do not install if you are not familiar with mu-plugin management. * | |
| * **************************************************************************** | |
| * | |
| * To install and test: | |
| * 1. Copy this file into your test site's wp-content/mu-plugins/ directory. | |
| * 2. Load your test site's homepage. | |
| * 3. View the rendered output, as well as the markup (via View Source). | |
| * | |
| * To uninstall: | |
| * Remove this file from wp-content/mu-plugins/. | |
| */ | |
| // Set initial HTML to process. | |
| $html = '<h2 class="old-class">h2 heading</h2><p>This class\'s <code>old-class</code> will be replaced with <code>new-class</code>.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| if ( $tags->next_tag( [ 'tag_name' => 'h2' ] ) ) { | |
| $tags->set_attribute( 'class', 'new-class' ); | |
| } | |
| $html = $tags->get_updated_html(); | |
| // Keep working with the result. | |
| $html .= '<h2 class="find-class">h2 heading</h2><p>This <code><h2></code> is found by class <code>find-class</code>, and updated with <code>found-class</code>.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| $tags->next_tag( [ 'class_name' => 'find-class' ] ); | |
| $tags->add_class( 'found-class' ); | |
| $html = $tags->get_updated_html(); | |
| // Now modify the second h2. | |
| $html .= '<p>Now update this heading\'s <code>style</code> attribute to red text.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| $tags->next_tag( 'h2' ); | |
| $tags->next_tag( 'H2' ); // Also validate that tag case doesn't matter. | |
| $tags->set_attribute( 'style', 'color:red;' ); | |
| $html = $tags->get_updated_html(); | |
| // Now modify each paragraph's style, skipping the first one. | |
| $html .= '<p>Now change all paragraphs to italic.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| $p_count = 0; | |
| while ( $tags->next_tag( 'p' ) ) { | |
| $tags->set_attribute( 'style', 'font-style:italic;' ); | |
| $p_count++; | |
| } | |
| $html = $tags->get_updated_html(); | |
| // Now update the third paragraph's style. | |
| $html .= '<p>Now change the third paragraph to bold using <code>seek()</code>.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| $tags->next_tag( 'p' ); | |
| $tags->set_bookmark( 'first p' ); | |
| $tags->next_tag( 'p' ); | |
| $tags->set_bookmark( 'second p' ); | |
| $tags->next_tag( 'p' ); | |
| $tags->set_bookmark( 'third p' ); | |
| $tags->next_tag( 'p' ); | |
| $tags->set_bookmark( 'fourth p' ); | |
| $tags->next_tag( 'p' ); | |
| $tags->set_bookmark( 'fifth p' ); | |
| $tags->seek( 'third p' ); | |
| $tags->set_attribute( 'style', 'font-weight:bold;' ); | |
| $html = $tags->get_updated_html(); | |
| // Now update the second paragraph's style. | |
| $html .= '<p>Now unset the second paragraph\'s style using <code>$match_offset</code>.</p>'; | |
| $tags = new WP_HTML_Tag_Processor( $html ); | |
| $tags->next_tag( [ 'tag_name' => 'p', 'match_offset' => 2 ] ); | |
| $tags->remove_attribute( 'style' ); | |
| $html = $tags->get_updated_html(); | |
| // Output final result. | |
| printf( "<h1>Trac 57575: <code>WP_HTML_Tag_Processor</code> Testing</h1>\n%s", $html ); | |
| exit; // View source to validate resultant HTML. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment