Retrieving detailed information about the active theme in WordPress can be crucial for debugging, development, and customization purposes. This guide will show you how to get comprehensive details about the active theme using the WordPress functions `WP_Debug_Data::debug_data()` and `wp_get_theme()`.
Step 1: Define the Function
In your functions.php
file, add the following code:
<?php
function get_theme_information() {
// Get theme data
$theme = wp_get_theme();
$active_theme = get_option('active_theme'); // Example, adjust according to how you store this data
$theme_mods = get_theme_mods(); // Retrieves theme modifications/customizations
// Prepare theme information array
$theme_info = array(
'name' => $theme->get('Name'),
'uri' => $theme->get('ThemeURI'),
'description' => $theme->get('Description'),
'version' => $theme->get('Version'),
'tags' => $theme->get('Tags'),
'author' => $theme->get('Author'),
'author_uri' => $theme->get('AuthorURI'),
'template' => $theme->get('Template'),
'stylesheet' => get_stylesheet(),
'text_domain' => $theme->get('TextDomain'),
'domain_path' => $theme->get('DomainPath'),
'requires_wp' => $theme->get('RequiresWP'),
'requires_php' => $theme->get('RequiresPHP'),
'theme_features' => $active_theme['theme_features']['value'],
'theme_path' => $active_theme['theme_path']['value'],
'auto_update' => $active_theme['auto_update']['debug'],
'nav_menu_locations' => isset($theme_mods['nav_menu_locations']) ? $theme_mods['nav_menu_locations'] : [],
'custom_css_post_id' => isset($theme_mods['custom_css_post_id']) ? $theme_mods['custom_css_post_id'] : null,
'features' => get_theme_feature_list(), // Custom function to list theme features
);
return $theme_info;
}
// Example custom function to list theme features
function get_theme_feature_list() {
$features = array();
if (current_theme_supports('post-thumbnails')) {
$features[] = 'Post Thumbnails';
}
if (current_theme_supports('custom-logo')) {
$features[] = 'Custom Logo';
}
// Add other feature checks as needed
// ...
return $features;
}
?>
Step 2: Display the Information
You can call this function wherever you need to display the theme information. For example, to display it in the footer:
<?php
function display_theme_information() {
$theme_info = get_theme_information();
echo '<ul>';
foreach ($theme_info as $key => $value) {
echo '<li><strong>' . esc_html($key) . ':</strong> ';
if (is_array($value)) {
echo '<ul>';
foreach ($value as $sub_value) {
echo '<li>' . esc_html($sub_value) . '</li>';
}
echo '</ul>';
} else {
echo esc_html($value);
}
echo '</li>';
}
echo '</ul>';
}
add_action('wp_footer', 'display_theme_information');
?>
Step 3: Testing the Function
To test if your function works correctly, you can temporarily call it directly in your theme files (like footer.php
) to see the output:
<?php
$theme_info = get_theme_information();
echo '<pre>';
print_r($theme_info);
echo '</pre>';
?>
Explanation
- Get the Active Theme Object:
The `wp_get_theme()` function retrieves the current active theme object, which contains various details about the theme.
<?php
$theme = wp_get_theme();
?>
This function returns an object representing the current active theme, containing various properties like name, URI, version, etc.
- Retrieve Active Theme Details:
By combining the data from `WP_Debug_Data` and `wp_get_theme()`, we can get a complete picture of the active theme, including custom fields and features.
- Extract Active Theme Fields:
<?php
$active_theme = $debug_data['wp-active-theme']['fields'];
?>
From the debug data, we extract specific fields related to the active theme.
- Get Theme Modifications:
<?php
$theme_mods = get_theme_mods();
?>
This function retrieves all the modifications made to the current theme.
- Prepare Theme Information Array:
The `$theme_info` array consolidates all relevant theme information, combining data from the `wp_get_theme()` object, debug data, and theme modifications.
- Custom Function for Theme Features:
<?php
function get_theme_feature_list() {
$features = [];
if (current_theme_supports('post-thumbnails')) {
$features[] = 'Post Thumbnails';
}
if (current_theme_supports('custom-logo')) {
$features[] = 'Custom Logo';
}
if (current_theme_supports('custom-header')) {
$features[] = 'Custom Header';
}
// Add other features as needed
return $features;
}
?>
This example function checks for various theme supports and lists them. You can expand this function to include any additional features your theme might support.
- Print Theme Information:
<?php
echo '<pre>';
print_r($theme_info);
echo '</pre>';
?>
This code prints the theme information in a readable format for debugging purposes.