arraypress/wp-theme-utils

A lean WordPress library for working with themes and theme management

dev-main 2025-07-01 15:47 UTC

This package is auto-updated.

Last update: 2025-09-02 14:41:21 UTC


README

A lightweight WordPress library for working with themes and theme management. Provides clean APIs for theme operations, feature detection, and value/label formatting perfect for forms and admin interfaces.

Features

  • 🎯 Clean API: WordPress-style snake_case methods with consistent interfaces
  • 🔍 Built-in Search: Theme search with value/label formatting
  • 📋 Form-Ready Options: Perfect value/label arrays for selects and forms
  • 🎨 Feature Detection: Easy theme feature and support checking
  • 📄 Template Management: Page template discovery and formatting
  • 🧩 Child Theme Support: Parent/child theme relationship handling
  • 🏗️ Block Theme Detection: FSE and block theme identification
  • 🔧 Version Checks: Theme version comparison utilities

Requirements

  • PHP 7.4 or later
  • WordPress 5.0 or later

Installation

composer require arraypress/wp-theme-utils

Basic Usage

Working with Single Themes

use ArrayPress\ThemeUtils\Theme;

// Get current theme
$current_theme = Theme::get_current();

// Get theme by stylesheet
$theme = Theme::get( 'twentytwentyfour' );

// Check if theme exists
if ( Theme::exists( 'my-theme' ) ) {
	// Theme exists
}

// Get theme information
$name          = Theme::get_name();
$version       = Theme::get_version();
$directory_uri = Theme::get_directory_uri();

// Check if theme is active
if ( Theme::is_active( 'twentytwentyfour' ) ) {
	// Theme is active
}

// Check multiple themes
if ( Theme::is_active( [ 'theme-one', 'theme-two' ] ) ) {
	// One of the themes is active
}

// Check if using default WordPress theme
if ( Theme::is_default_active() ) {
	// Using a default WP theme
}

// Check theme features
if ( Theme::supports( 'post-thumbnails' ) ) {
	// Theme supports featured images
}

if ( Theme::supports_gutenberg() ) {
	// Theme supports Gutenberg features
}

if ( Theme::is_block_theme() ) {
	// Theme is a block/FSE theme
}

// Version checking
if ( Theme::is_version( '2.0', '>=' ) ) {
	// Current theme version is 2.0 or higher
}

// Child theme checking
if ( Theme::is_child() ) {
	$parent = Theme::get_parent();
}

// Theme mods
$logo = Theme::get_mod( 'custom_logo', false );

Working with Multiple Themes

use ArrayPress\ThemeUtils\Themes;

// Get all themes
$all_themes = Themes::get_all();

// Get themes as options for forms
$theme_options = Themes::get_options();
// Returns: [['value' => 'stylesheet', 'label' => 'Theme Name'], ...]

// Search themes
$search_results = Themes::search_options( 'twenty' );
// Returns: [['value' => 'twentytwentyfour', 'label' => 'Twenty Twenty-Four'], ...]

// Get page templates
$templates        = Themes::get_page_templates();
$template_options = Themes::get_page_template_options();

// Get filtered theme lists
$child_themes   = Themes::get_child_themes();
$parent_themes  = Themes::get_parent_themes();
$block_themes   = Themes::get_block_themes();
$default_themes = Themes::get_default_themes();

// Get themes by feature
$post_format_themes = Themes::get_by_feature( 'post-formats' );

// Get themes by author
$author_themes = Themes::get_by_author( 'WordPress.org' );

// Count themes
$theme_count = Themes::count();

// Check if multiple themes exist
if ( Themes::all_exist( [ 'theme-one', 'theme-two' ] ) ) {
	// All themes exist
}

Page Templates

// Get page templates from current theme
$templates = Themes::get_page_templates();
// Returns: ['' => 'Default Template', 'page-custom.php' => 'Custom Page']

// Get as form options
$template_options = Themes::get_page_template_options();
// Returns: [['value' => '', 'label' => 'Default Template'], ...]

// Without default template
$templates = Themes::get_page_templates( false );
$options   = Themes::get_page_template_options( false );

Advanced Examples

// Check if current theme supports specific features
$features_to_check  = [ 'post-thumbnails', 'custom-logo', 'html5' ];
$supported_features = Theme::get_supported_features( $features_to_check );

// Get theme hierarchy information
if ( Theme::is_child() ) {
	$parent_themes = Theme::get_parent_themes();
	foreach ( $parent_themes as $parent ) {
		echo "Parent: " . $parent->get( 'Name' ) . "\n";
	}
}

// Search for themes with specific capabilities
$modern_themes = Themes::search( 'gutenberg' );

// Get customizer URL for current theme
$customizer_url = Theme::get_customizer_url();

// Advanced feature checking examples
$required_features = [ 'post-thumbnails', 'custom-header', 'custom-background' ];
$optional_features = [ 'custom-logo', 'widgets', 'html5' ];

// Check if theme meets requirements
if ( Theme::supports_all_features( $required_features ) ) {
	$bonus_features = Theme::get_supported_features( $optional_features );
	echo "Theme ready! Bonus features: " . implode( ', ', $bonus_features );
} else {
	$missing_features = Theme::get_unsupported_features( $required_features );
	echo "Missing required features: " . implode( ', ', $missing_features );
}

// Check for modern theme capabilities
$modern_features = [ 'align-wide', 'responsive-embeds', 'editor-styles' ];
if ( Theme::supports_any_features( $modern_features ) ) {
	echo "Theme has modern Gutenberg support";
}

Key Features

  • Value/Label Format: Perfect for forms and selects
  • Feature Detection: Easy theme capability checking
  • Block Theme Support: FSE and block theme identification
  • Template Management: Page template discovery and formatting
  • Child Theme Handling: Parent/child relationships
  • Version Utilities: Theme version comparison
  • Search Functionality: Built-in theme search

Requirements

  • PHP 7.4+
  • WordPress 5.0+

Contributing

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

License

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

Support