admin管理员组

文章数量:1192406

I'm running a multisite network with 200+ themes installed and activated. Is there a way to manipulate the list of available themes in wp-admin so that certain theme does not appear in the list? I want to do it without editing core files.

I know WordPress gets the list of available themes from the function get_themes(). And that there is a function called get_allowed_themes() that might be the solution. But no idea about how to use it.

I'm running a multisite network with 200+ themes installed and activated. Is there a way to manipulate the list of available themes in wp-admin so that certain theme does not appear in the list? I want to do it without editing core files.

I know WordPress gets the list of available themes from the function get_themes(). And that there is a function called get_allowed_themes() that might be the solution. But no idea about how to use it.

Share Improve this question asked Jun 13, 2012 at 7:43 grosshatgrosshat 8610 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 4 +100

The following filter works in Multisite in the following screens:

  • /wp-admin/network/themes.php
  • /wp-admin/network/site-themes.php?id=1 (individual sites allowed themes)
add_filter( 'all_themes', 'wpse_55081_remove_themes_ms' );
function wpse_55081_remove_themes_ms($themes)
{
    unset($themes['Twenty Ten'], $themes['Twenty Eleven']);
    return $themes;
}

For the regular theme selector in a single site or sub-site of a network /wp-admin/themes.php (Appearance -> Themes), looks like there's no hook...

  • Manipulate list of themes in wp-admin
  • Reordering themes in admin panel

I found that $wp_themes global var holds the array with all themes, but couldn't manage to unset items in it...

The old jQuery trickery does the job, but pagination may become funny...

add_action( 'admin_head-themes.php', 'wpse_55081_remove_themes' );

function wpse_55081_remove_themes()
{
    ?>
    <script type="text/javascript">
    jQuery(document).ready( function($) {
        $('div.available-theme:contains("comicpress")').remove();
        $('div.available-theme:contains("twentyten")').remove();
        $('div.available-theme:contains("starkers")').remove();
    });     
    </script>
    <?php
}

Update

Looks like there's a new hook on the block: extra_theme_headers.
But there's something weird:

  • it is documented as a new filter in WP 3.4
  • but it appears in wp-includes/deprecated.php (?!)
/*
 * The returning $arr is always empty, but we are able to unset items in the global $wp_themes
 * Works in all theme screens, Multisite or Single Site (and doesn't bugs pagination)
 * 
 * It is defined this way: apply_filters( 'extra_theme_headers', array() )
 * The array value is always empty but, if we return it, the filter doesn't works..
 *
 */
add_filter( 'extra_theme_headers', 'wpse_55081_remove_themes_everywhere', 10, 1 );

function wpse_55081_remove_themes_everywhere($arr)
{
    global $wp_themes;
    unset($wp_themes['Convertible/Convertible'], $wp_themes['grido-child'], $wp_themes['ilost'], $wp_themes['parallels'], $wp_themes['twentyeleven']);
    // return $arr;
}
add_filter( 'wp_prepare_themes_for_js', 'hideThemes');
function hideThemes($arr){
    foreach($arr as $key => &$value){
        //unset what you want
    }
    return $arr;
}`

Or alternatively:

add_filter( 'wp_prepare_themes_for_js', 'hideThemes');
function hideThemes($arr){

    unset($arr["twentyfourteen"]);
    unset($arr["twentythirteen"]);
    unset($arr["twentytwelve"]);

    return $arr;
}`

Copy and past in functions.php

add_filter( 'wp_prepare_themes_for_js', 'hideThemes');
function hideThemes($arr){

    unset($arr["twentyfourteen"]); 
    unset($arr["twentythirteen"]);
    unset($arr["theme-folder-name"]);

    return $arr;
}

本文标签: multisiteHide a theme on list of themes in wpadmin without editing core files