有没有WordPress插件可以将插件文件注册为自定义页面模板?

时间:2010-11-16 作者:jnthnclrk

我需要创建一个插件,使自定义页面模板可在wp管理。我想知道是否有人已经解决了这个问题,因为这似乎是一个非常典型的过程?

4 个回复
SO网友:Bainternet

就像Rarst回答的那样,您确实可以做到这一点,而无需编辑核心文件,也无需删除页面属性元框,只需稍加修改即可使用相同的代码创建。下面的代码是/admin/include/meta框的代码。phpand i添加了a注释,以显示额外页面模板选项的位置:

function page_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
        $pages = wp_dropdown_pages(array(\'post_type\' => $post->post_type, \'exclude_tree\' => $post->ID, \'selected\' => $post->post_parent, \'name\' => \'parent_id\', \'show_option_none\' => __(\'(no parent)\'), \'sort_column\'=> \'menu_order, post_title\', \'echo\' => 0));
        if ( ! empty($pages) ) {
        ?>
        <p><strong><?php _e(\'Parent\') ?></strong></p>
        <label class="screen-reader-text" for="parent_id"><?php _e(\'Parent\') ?></label>
        <?php echo $pages; ?>
        <?php
        } // end empty pages check
    } // end hierarchical check.
    if ( \'page\' == $post->post_type && 0 != count( get_page_templates() ) ) {
        $template = !empty($post->page_template) ? $post->page_template : false;
        ?>
        <p><strong><?php _e(\'Template\') ?></strong></p>
        <label class="screen-reader-text" for="page_template"><?php _e(\'Page Template\') ?></label><select name="page_template" id="page_template">
        <option value=\'default\'><?php _e(\'Default Template\'); ?></option>
        <?php page_template_dropdown($template); ?>

        // add your page templates as options

        </select>
        <?php
    } ?>
    <p><strong><?php _e(\'Order\') ?></strong></p>
    <p><label class="screen-reader-text" for="menu_order"><?php _e(\'Order\') ?></label><input name="menu_order" type="text" size="4" id="menu_order" value="<?php echo esc_attr($post->menu_order) ?>" /></p>
    <p><?php if ( \'page\' == $post->post_type ) _e( \'Need help? Use the Help tab in the upper right of your screen.\' ); ?></p>
    <?php
}
不知道这是否是对您的修复,但当我需要在插件内置主题中显示帖子类型时,我有一个更笑脸的案例,为此我使用了add_filter(\'the_content\', \'my-function\');我的函数创建了要显示的输出。

另一种选择是让插件在当前主题目录中创建模板文件,如下所示:

function create_plugins_theme_file(){
    $file_name = TEMPLATEPATH . \'/\' . $tamplate_name . \'.php\';
    $handle = fopen($file_name, \'w\') or wp_die(\'Cannot open file for editing\');
    $file_contents = <<<OUT
<?php
/*
Template Name: $tamplate_name
*/
?>

//you theme file here

OUT;

   fwrite($handle, $file_contents);
   fclose($handle);
 }
您可以在第一次检查文件是否存在后运行此操作

if(!file_exists( $file_name)){create_plugins_theme_file();}
希望其中一个有帮助。

SO网友:Lars Koudal

我不完全确定我是否理解您想要实现的目标,至少不知道您为什么想要一个插件来实现这一目标。

创建不同页面模板的正常过程是:

在ACTIVE theme目录中创建一个新的页面模板(复制page.php)。

更改模板的名称(文件内)。

/*模板名称:全宽页面*/

将页面代码更改为您想要实现的目标。

现在,您可以创建一个新页面并选择要使用的“模板”。

alt text

。。。

我希望这就是你想要实现的目标?

此处的官方文件:http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

SO网友:Rarst

这似乎很难实现。get_page_templates() 函数主动丢弃不在父和子根目录中的任何内容。它也不存储在全局变量中,也不允许过滤生成的模板列表。

我认为页面属性元框需要分叉并完全替换。即使在那时也不确定这是否可能。

我同意这似乎是有道理的,但WordPress代码非常精确,因为它希望命名模板只来自主题的目录。

SO网友:Denis de Bernardy

这样做不是register_theme_directory() 被介绍了?

http://core.trac.wordpress.org/ticket/10467

当时这是一个与BuddyPress相关的问题,BuddyPress希望在这里和那里添加额外的模板。

我认为它们没有正确地列为页面模板,不过:

http://core.trac.wordpress.org/ticket/15803

结束

相关推荐

How do you debug plugins?

我对插件创作还很陌生,调试也很困难。我用了很多echo,它又脏又丑。我确信有更好的方法可以做到这一点,也许是一个带有调试器的IDE,我可以在其中运行整个站点,包括插件?