Using WordPress ‘pre_prepare_themes_for_js’ PHP filter

The ‘pre_prepare_themes_for_js’ WordPress PHP filter allows developers to modify theme data before it is prepared for JavaScript.

Usage

add_filter( 'pre_prepare_themes_for_js', 'my_custom_theme_preparation', 10, 3 );

function my_custom_theme_preparation( $prepared_themes, $themes, $current_theme ) {
    // Your custom code to modify theme data
    return $prepared_themes;
}

Parameters

  • $prepared_themes (array) – An associative array of theme data. Default empty array.
  • $themes (WP_Theme[]|null) – An array of theme objects to prepare, if any.
  • $current_theme (string) – The active theme slug.

Examples

Change Theme Screenshot Change the theme screenshot for a specific theme.

add_filter( 'pre_prepare_themes_for_js', 'change_theme_screenshot', 10, 3 );

function change_theme_screenshot( $prepared_themes, $themes, $current_theme ) {
    foreach ( $prepared_themes as &$theme ) {
        if ( 'my-theme' === $theme['id'] ) {
            $theme['screenshot'] = 'https://example.com/my-custom-screenshot.png';
        }
    }

    return $prepared_themes;
}

This code changes the screenshot of the theme with the slug ‘my-theme’ to a custom image URL.

Add Custom Theme Data Add custom data to all themes.

add_filter( 'pre_prepare_themes_for_js', 'add_custom_theme_data', 10, 3 );

function add_custom_theme_data( $prepared_themes, $themes, $current_theme ) {
    foreach ( $prepared_themes as &$theme ) {
        $theme['custom_data'] = 'My custom data';
    }

    return $prepared_themes;
}

This code adds custom data to all themes in the theme list.

Hide Inactive Themes Hide all themes except the current theme.

add_filter( 'pre_prepare_themes_for_js', 'hide_inactive_themes', 10, 3 );

function hide_inactive_themes( $prepared_themes, $themes, $current_theme ) {
    return array_filter( $prepared_themes, function( $theme ) use ( $current_theme ) {
        return $theme['id'] === $current_theme;
    });
}

This code filters the theme list, keeping only the current active theme.

Add Update Information Add update information to the themes that have updates available.

add_filter( 'pre_prepare_themes_for_js', 'add_update_information', 10, 3 );

function add_update_information( $prepared_themes, $themes, $current_theme ) {
    $updates = get_theme_updates();

    foreach ( $prepared_themes as &$theme ) {
        if ( isset( $updates[ $theme['id'] ] ) ) {
            $theme['update'] = 'An update is available';
        }
    }

    return $prepared_themes;
}

This code adds an “An update is available” text to themes with available updates.

Mark Premium Themes

This code appends the word “Premium” to the theme name for the themes with slugs ‘premium-theme-1’ and ‘premium-theme-2’.

add_filter( 'pre_prepare_themes_for_js', 'mark_premium_themes', 10, 3 );

function mark_premium_themes( $prepared_themes, $themes, $current_theme ) {
$premium_themes = array( 'premium-theme-1', 'premium-theme-2' );

foreach ( $prepared_themes as &$theme ) { 
if ( in_array( $theme['id'], $premium_themes ) ) { 
$theme['name'] .= ' (Premium)'; 
} 
}
return $prepared_themes;

}