在后台按文件夹对主题进行分类

时间:2013-04-18 作者:navjotjsingh

我想组织如下主题:

  1. wp-content/themes/themeshop/theme1
  2. wp-content/themes/themeshop/theme2
  3. wp-content/themes/themeshop/theme3
据我所知,这个组织在文件夹级别工作得非常好。我把主题放在了子目录中,一切似乎都很好。现在是否可以在主题页面的管理后端复制此分类?

2 个回复
最合适的回答,由SO网友:brasofilo 整理而成

<子>Updated plugin version available at GitHub.

我第一次看到你的问题是在[wp-hackers] list, 并且,在实施解决方案后,即将发布一个Q&;这是我的荣幸。嗯,它已经在这里了,上面有悬赏金:)

Daniel Bachhuber 在线程中指出:

WordPress。com将主题放在子目录中

/wp-content/themes/public 
/wp-content/themes/premium
我提出此解决方案是为了在后端显示:

在多站点中,我们可以在主题屏幕中添加一列,并使用manage_themes_custom_columnmanage_themes-network_columns.

在单站点安装中,我找到的唯一入口是theme_action_links.

<?php   
/**
 * Plugin Name: Theme Folders Categories
 * Plugin URI: http://wordpress.stackexchange.com/q/96361/12615
 * Version: 1.0
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv2 or later
 */
class WPhackersSE_Theme_Folders
{   
    public function __construct()
    {
        add_action( \'plugins_loaded\', array( $this, \'start_up\' ) );
    }    

    /**
     * Hooks for Network themes and Single Site themes
     * Nothing happens on sub-sites of a Network 
     */
    public function start_up()
    {
        if( is_network_admin() )
        {
            add_filter( \'manage_themes-network_columns\', array( $this, \'column_register\' ) );
            add_action( \'manage_themes_custom_column\', array( $this, \'column_display\' ), 10, 3 );
            add_action( \'admin_head-themes.php\', array( $this, \'network_theme_category_css\' ) );
        } 
        elseif( !is_multisite() )
        {
            add_filter( \'theme_action_links\', array( $this, \'theme_folder_single_site\' ), 10, 2 );
            add_action( \'admin_head-themes.php\', array( $this, \'theme_category_css\' ) );
        } 
    }

    /**
     * Add custom category (folder) column in network themes
     * 
     * @param array $columns
     * @return array
     */
    public function column_register( $columns ) 
    {
        $columns[\'theme_folder\'] = \'Category\';
        return $columns;
    }

    /**
     * Display custom row in network themes
     * $stylesheet contains a string "folder/theme_name"
     * $theme is a WP_Theme object
     * 
     * @param string $column_name
     * @param string $stylesheet 
     * @param object $theme 
     * @return string
     */
    public function column_display( $column_name, $stylesheet, $theme ) 
    {
        if( \'theme_folder\' != $column_name  )
            return;

        echo $this->make_button( $stylesheet );
    }

    /**
     * Adjust column width and button style in Multisite screen
     */
    public function network_theme_category_css()
    {   
        echo "<style type=\'text/css\'>
            #theme_folder { width: 10% }
            {$this->button_style()}
            </style>";
    }

    /**
     * Show theme category (folder) in single site theme action row
     * $theme is a WP_Theme object
     * 
     * @param array $actions
     * @param object $theme
     * @return array
     */
    public function theme_folder_single_site( $actions, $theme )
    {
        array_unshift( $actions, $this->make_button( $theme->stylesheet ) );
        return $actions;
    }

    /**
     * Adjust button style in Single site screen
     */
    public function theme_category_css()
    {   
        echo "<style type=\'text/css\'>{$this->button_style()}</style>";
    }

    /**
     * Common button for Multi and Single sites
     * The category name is extracted from a string "folder/themefolder"
     * 
     * @param object $theme
     * @return string
     */
    private function make_button( $stylesheet )
    {
        $button_category = sprintf( 
            \'<a href="javascript:void(0)" class="button-secondary theme-folder" title="%1$s">%1$s</a>\',
            dirname( $stylesheet )
        );
        return $button_category;
    }
    
    /**
     * Common style for Multi and Single sites
     * 
     * @return string
     */
    private function button_style()
    {
        return \'.theme-folder { 
            cursor: default !important;
            line-height: 15px !important;
            height: 17px !important;
            background-image: -webkit-gradient(linear, left top, left bottom, from(#DCFEDE), to(#CBEBCD)) !important;
            background-image: -webkit-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: -moz-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: -o-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: linear-gradient(to bottom, #DCFEDE, #CBEBCD) !important;
        }\';
    }
}

new WPhackersSE_Theme_Folders;
<小时/>Result in Multisite:
文件夹/themes/clientes//themes/brasofilo/). 网络的子站点中没有输出。

multisite theme categories

很可能在这里添加一个过滤器。

<小时/>Result in a single site:
未启用多站点。文件夹/themes/defaults//themes/others/.

single site theme categories

这个屏幕在定制方面确实有限。我同意@Ralf912分析。


Important note:将活动主题移动到子文件夹后,所有网站都会丢失其主题配置,each site theme has to be set again

SO网友:Ralf912

我认为您正在寻找一个动作挂钩或过滤器,以将您的主题从标准主题列表中排除,并创建一个表示文件夹结构的额外列表。或者根据卷边所在的文件夹对卷边进行更简单的分组。

如果您打开wp-admin/themes.php, 您将看到没有动作挂钩或过滤器。在所使用的函数或list类中也没有操作挂钩或过滤器(WP_Themes_List_Table) 您可以在其中修改主题列表。

您唯一能做的就是重新编码在主题页面上显示主题的完整过程,并替换指向themes.php 在管理菜单中添加到您自己的脚本。

结束