php - 如何在 Wordpress 中隐藏模板?

标签 php wordpress wordpress-theming

<分区>

有没有办法在管理员中隐藏模板文件?

例如,我有一个只有在安装了特定插件后才可用的模板,而且我已经知道如何检查插件是否处于事件状态。但是如何隐藏模板呢?

例如我想隐藏“Blogger Redirection”-模板如下:

Image is just an example...

我找到了几个链接,但所有的解决方案似乎都已弃用。

编辑: 如果有人对我如何检查插件是否处于事件状态感兴趣,我可以使用以下函数来完成:

function isPluginActive($plugin){
        if ( in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
        {
            return true;
        }
        return false;
    }

最佳答案

更新 - 安德鲁在下面的评论中指出的警告:

Use this code with caution, If you have any pages using the template that you removed from the select, updating the page will cause it to revert to the default template


我不确定是否有一种 PHP 方法可以做到这一点 - 您必须查看 WP_Theme 类,但从我的第一眼来看,这可能是不可能的,因为为了获取所有模板,该类使用名为 scandir() 的内部函数它扫描当前主题目录并从那里获取所有 .php 文件。然后它会查找相应的 Template Name: 标识符,如果它存在,它就会被添加到模板列表中。

因此,我建议您添加一些 JS,以便从页面模板选择中删除此选项。这是一个代码片段:

function my_remove_page_template() {
    global $pagenow;
    if ( in_array( $pagenow, array( 'post-new.php', 'post.php') ) && get_post_type() == 'page' ) { ?>
        <script>
            (function($){
                $(document).ready(function(){
                    $('#page_template option[value="sidebar-page.php"]').remove();
                })
            })(jQuery)
        </script>
    <?php 
    }
}
add_action('admin_footer', 'my_remove_page_template', 10);

这将从下拉列表中删除模板 sidebar-page.php。条件是仅在页面的添加和编辑屏幕上添加脚本。

根据您的情况进行调整并享受 :)

关于php - 如何在 Wordpress 中隐藏模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13566676/

相关文章:

php - 如何在 PHP 中安排事件

php - 从排序数组 PHP 返回键的顺序

php - 当我尝试访问数组项时,我只得到 1. one

php - 即使在主类别上,页面也会返回子类别的 ID

html - Logo 覆盖我的 slider wordpress

css - 如何在 Bootstrap 中创建可滚动的列?

php - 为什么一个刚刚定义的array of array count = 1?

php - 使用准备好的 PDO 语句

wordpress - 如何显示自定义帖子类型的自定义数据

php - 从自定义帖子类型编辑屏幕中删除标题