PHPackages                             arraypress/wp-dom-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. arraypress/wp-dom-utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

arraypress/wp-dom-utils
=======================

A lean WordPress library for DOM operations with CSS selector support and element manipulation

10PHP

Since Jul 7Pushed 10mo agoCompare

[ Source](https://github.com/arraypress/wp-dom-utils)[ Packagist](https://packagist.org/packages/arraypress/wp-dom-utils)[ RSS](/packages/arraypress-wp-dom-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress DOM Utils - Lean DOM Manipulation
===========================================

[](#wordpress-dom-utils---lean-dom-manipulation)

A lightweight PHP library for DOM operations with CSS selector support and comprehensive element manipulation. Designed with clean APIs that make complex DOM tasks simple and reliable.

Features
--------

[](#features)

- 🎯 **Clean API**: Separate classes for different DOM operations (`DOM`, `Element`, `CSS`)
- 🔍 **CSS Selectors**: Modern `querySelector` and `querySelectorAll` support
- 🏗️ **Element Manipulation**: Wrap, unwrap, insert, replace, and transform elements
- 🎨 **Style Management**: Parse and modify CSS styles with ease
- 🏷️ **Class Utilities**: Add, remove, and modify CSS classes intelligently
- 🛠️ **Structure Modification**: Change tag names, clone elements, and restructure DOM
- ⚡ **WordPress Ready**: Built for WordPress but works in any PHP environment
- 🔒 **Safe Operations**: Graceful error handling with null returns

Why Not WordPress's HTML API?
-----------------------------

[](#why-not-wordpresss-html-api)

WordPress 6.2+ includes an HTML Tag Processor, but it's limited to linear attribute modifications. Our library provides:

- **Full DOM tree manipulation** (WordPress can't wrap, insert, or restructure)
- **CSS selector support** (WordPress uses linear tag scanning)
- **Element creation and cloning** (WordPress is read-modify only)
- **Style and class management** (WordPress focuses on attributes)

Requirements
------------

[](#requirements)

- PHP 7.4 or later
- DOM extension
- libxml extension

Installation
------------

[](#installation)

```
composer require arraypress/wp-dom-utils
```

Basic Usage
-----------

[](#basic-usage)

### Creating and Parsing DOM

[](#creating-and-parsing-dom)

```
use ArrayPress\DOMUtils\DOM;

// Create DOM from HTML string
$dom = DOM::create( 'Hello World' );

// Query elements with CSS selectors
$container  = DOM::query_selector( '.container', $dom );
$paragraphs = DOM::query_selector_all( 'p', $dom );

// Convert back to HTML
$html = DOM::to_html( $dom );
```

### Element Manipulation

[](#element-manipulation)

```
use ArrayPress\DOMUtils\Element;

// Wrap an element
$wrapper = Element::wrap( $paragraph, 'div', [ 'class' => 'wrapper' ] );

// Change tag name
$new_element = Element::change_tag_name( 'section', $div );

// Insert content
Element::insert_before( $target, 'Before' );
Element::insert_after( $target, 'After' );
Element::replace_with( $target, 'Replacement' );

// Clone elements
$clone = Element::clone_element( $original, true ); // deep clone
```

### Class Management

[](#class-management)

```
use ArrayPress\DOMUtils\Element;

// Add multiple classes
Element::add_classes( $element, [ 'active', 'highlighted' ] );

// Remove classes
Element::remove_classes( $element, [ 'old', 'deprecated' ] );

// Replace classes
Element::replace_classes( $element, [ 'old-class' ], [ 'new-class' ] );

// Check for classes
if ( Element::has_classes( $element, [ 'required', 'validated' ] ) ) {
	// Element has all required classes
}
```

### CSS Style Management

[](#css-style-management)

```
use ArrayPress\DOMUtils\CSS;
use ArrayPress\DOMUtils\Element;

// Parse CSS string to array
$styles = CSS::string_to_array( 'color: red; margin: 10px; padding: 5px;' );
// Returns: ['color' => 'red', 'margin' => '10px', 'padding' => '5px']

// Convert array back to CSS string
$css_string = CSS::array_to_string( [ 'color' => 'blue', 'font-size' => '14px' ] );
// Returns: "color: blue; font-size: 14px"

// Add styles to element
Element::add_styles( $element, [
	'background-color' => '#f0f0f0',
	'border'           => '1px solid #ccc',
	'padding'          => '10px'
] );
```

Common Use Cases
----------------

[](#common-use-cases)

### Content Transformation

[](#content-transformation)

```
function transform_content( $html ) {
	$dom = DOM::create( $html );

	// Wrap all images in figure elements
	$images = DOM::query_selector_all( 'img', $dom );
	foreach ( $images as $img ) {
		Element::wrap( $img, 'figure', [ 'class' => 'image-wrapper' ] );
	}

	return DOM::to_html( $dom );
}
```

### WordPress Integration

[](#wordpress-integration)

```
// Hook into WordPress content filters
add_filter( 'the_content', function ( $content ) {
	$dom = DOM::create( $content );

	// Add lazy loading to images
	$images = DOM::query_selector_all( 'img', $dom );
	foreach ( $images as $img ) {
		if ( ! Element::get_attribute( $img, 'loading' ) ) {
			Element::set_attributes( $img, [ 'loading' => 'lazy' ] );
		}
	}

	// Wrap tables for responsiveness
	$tables = DOM::query_selector_all( 'table', $dom );
	foreach ( $tables as $table ) {
		Element::wrap( $table, 'div', [ 'class' => 'table-responsive' ] );
	}

	return DOM::to_html( $dom );
} );
```

API Reference
-------------

[](#api-reference)

### DOM Class

[](#dom-class)

- `create( string $html, array $options = [] ): DOMDocument`
- `query_selector( string $selector, $context ): ?DOMElement`
- `query_selector_all( string $selector, $context ): DOMNodeList`
- `create_element( DOMDocument $dom, string $tag, array $attributes = [], $content = ''): ?DOMElement`
- `to_html( $node, bool $inner_html = false ): string`

### Element Class

[](#element-class)

**Attributes:**

- `get_attribute( DOMElement $element, string $attribute, $default = null `
- `set_attributes( DOMElement $element, array $attributes ): void`
- `remove_attributes( DOMElement $element, array $attributes ): void`

**Classes:**

- `get_classes( DOMElement $element ): array`
- `add_classes( DOMElement $element, array $classes ): void`
- `remove_classes( DOMElement $element, array $classes ): void`
- `has_classes( DOMElement $element, array $classes ): bool`

**Styles:**

- `get_styles( DOMElement $element ): array`
- `add_styles( DOMElement $element, array $styles ): void`

**Structure:**

- `wrap( DOMElement $element, string $wrapper_tag, array $attributes = [] ): ?DOMElement`
- `unwrap( DOMElement $element ): bool`
- `change_tag_name(string $name, DOMElement $element ): ?DOMElement`
- `insert_before( DOMElement $target, $content ): bool`
- `insert_after( DOMElement $target, $content ): bool`
- `replace_with( DOMElement $target, $content ): bool`
- `clone_element( DOMElement $element, bool $deep = true ): DOMElement`

### CSS Class

[](#css-class)

- `string_to_array( string $style_string ): array`
- `array_to_string( array $styles ): string`

Error Handling
--------------

[](#error-handling)

All methods return `null`, `false`, or empty arrays for invalid inputs rather than throwing exceptions, making them safe for direct use in conditionals.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

License
-------

[](#license)

This project is licensed under the GPL-2.0-or-later License.

Support
-------

[](#support)

- [Documentation](https://github.com/arraypress/wp-dom-utils)
- [Issue Tracker](https://github.com/arraypress/wp-dom-utils/issues)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd6eb8aff0903d87eb674d1ba3c5f3653899c0d7661504eb0deb7798ed86b643?d=identicon)[arraypress](/maintainers/arraypress)

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (1 commits)")

### Embed Badge

![Health badge](/badges/arraypress-wp-dom-utils/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-wp-dom-utils/health.svg)](https://phpackages.com/packages/arraypress-wp-dom-utils)
```

###  Alternatives

[avstudnitz/scopehint2

Displays a hint when a configuration value is overwritten on a lower scope (website or store view).

1671.5M1](/packages/avstudnitz-scopehint2)[ml/iri

IRI handling for PHP

276.4M6](/packages/ml-iri)[octopush/sms-api

Octopush - Send SMS like a PRO

1796.0k1](/packages/octopush-sms-api)[phpcfdi/cfdi-cleaner

Clean up Mexican CFDI

1334.1k1](/packages/phpcfdi-cfdi-cleaner)[icetalker/filament-picker

A Regular Picker for Filament Form

1052.8k](/packages/icetalker-filament-picker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
