Performant HTML manipulation in WordPress 6.2
WordPress 6.2 quietly shipped with an update that will have a significant impact on our ability to work with markup: The
This class provides a performant way to work with tags and attributes in HTML markup – but not content within tags.
The idea is to provide a performant class that is capable of doing this. To quote the documentation:
This is music to my ears because this means that we now have a reliable and performant alternative to
DOMDocument and its many quirks to work with attributes and tags, or even RegEx.
However, we will still have to reach out to
DOMDocument to fully parse and work with tag content – for now at least.
Here are a few use cases that I can think of for this:
- Counting the number of list items
- Getting the
SRCattributes of all images
- Look for images with missing ALT text
- Counting how many items of a particular tag have a class
- Adding a class to alternating tags
- Adding an attribute to the second last column in a table that is not a whose cell is a
TD, not a
- Add a preload attribute to
LINKtags in the
- Adding classes to navigation menu items
My wishlist for the future:
- Add the ability to create tags
- Add the ability to get outer and inner HTML of tags