使用插件创建自定义页面模板?
4 个回答
- 投票数
-
- 2010-11-02
get_page_template()
可以通过page_template
过滤器覆盖.如果您的插件是一个包含模板作为文件的目录,则只需传递这些文件的名称即可.如果要"即时"创建它们(在管理区域中编辑它们并将它们保存在数据库中?),则可能需要将它们写入缓存目录并引用它们,或者挂接到template_redirect
并进行一些疯狂的eval()
的东西.一个简单的插件示例,如果满足特定条件,该插件"重定向"到同一插件目录中的文件:
add_filter( 'page_template', 'wpa3396_page_template' ); function wpa3396_page_template( $page_template ) { if ( is_page( 'my-custom-page-slug' ) ) { $page_template = dirname( __FILE__ ) . '/custom-page-template.php'; } return $page_template; }
get_page_template()
can be overridden via thepage_template
filter. If your plugin is a directory with the templates as files in them, it's just a matter of passing the names of these files. If you want to create them "on the fly" (edit them in the admin area and save them in the database?), you might want to write them to a cache directory and refer to them, or hook intotemplate_redirect
and do some crazyeval()
stuff.A simple example for a plugin that "redirects" to a file in the same plugin directory if a certain criterium is true:
add_filter( 'page_template', 'wpa3396_page_template' ); function wpa3396_page_template( $page_template ) { if ( is_page( 'my-custom-page-slug' ) ) { $page_template = dirname( __FILE__ ) . '/custom-page-template.php'; } return $page_template; }
-
嗨,Jan,您有一些示例代码如何将插件文件作为自定义页面模板传入?Hey Jan, do you have some example code on how to pass a plugin file in as a custom page template?
- 0
- 2010-11-16
- jnthnclrk
-
@trnsfrmr:真的很简单,我在回答中添加了一个简单的示例.@trnsfrmr: It's really easy, I added a simple example to my answer.
- 0
- 2010-11-16
- Jan Fabry
-
请注意,在更高版本(3.1+)中,它已或多或少地被"template_include"过滤器所取代.Note that this has been more or less replaced by the "template_include" filter in later versions (3.1+).
- 12
- 2013-06-10
- Inigoesdr
-
完美!!!,您已经节省了我的时间@JanFabryPerfect !!!, you have save my time @JanFabry
- 0
- 2018-12-21
- Kishan Chauhan
-
如@fireydude所述,这不是通用解决方案.这是一种硬编码页面代码的解决方法.As stated by @fireydude, this is not a generic solution. It's a workaround that hard-codes the page slug.
- 0
- 2019-07-07
- Mauro Colella
-
- 2015-10-12
覆盖
get_page_template()
只是一个快速的技巧.它不允许从"管理"屏幕中选择模板,并且页面标签已硬编码到插件中,因此用户无法知道模板的来源.首选解决方案是按照本教程,它允许您从插件在后端注册页面模板.然后,它就像其他模板一样工作.
/* * Initializes the plugin by setting filters and administration functions. */ private function __construct() { $this->templates = array(); // Add a filter to the attributes metabox to inject template into the cache. add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); // Add a filter to the save post to inject out template into the page cache add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); // Add a filter to the template include to determine if the page has our // template assigned and return it's path add_filter('template_include', array( $this, 'view_project_template') ); // Add your templates to this array. $this->templates = array( 'goodtobebad-template.php' => 'It\'s Good to Be Bad', ); }
Overriding
get_page_template()
is just a quick hack. It does not allow the template to be selected from the Admin screen and the page slug is hard-coded into the plugin so the user has no way to know where the template is coming from.The preferred solution would be to follow this tutorial which allows you to register a page template in the back-end from the plug-in. Then it works like any other template.
/* * Initializes the plugin by setting filters and administration functions. */ private function __construct() { $this->templates = array(); // Add a filter to the attributes metabox to inject template into the cache. add_filter('page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); // Add a filter to the save post to inject out template into the page cache add_filter('wp_insert_post_data', array( $this, 'register_project_templates' ) ); // Add a filter to the template include to determine if the page has our // template assigned and return it's path add_filter('template_include', array( $this, 'view_project_template') ); // Add your templates to this array. $this->templates = array( 'goodtobebad-template.php' => 'It\'s Good to Be Bad', ); }
-
如果您可以从答案中的链接中发布相关代码,那将是不错的选择(*并且是首选*),否则,只不过是comment肿的注释而已:-)Would be nice (*and prefered*) if you can post the relevant code from the link in your answer, otherwise this is nothing more than a bloated comment :-)
- 0
- 2015-10-12
- Pieter Goosen
-
该教程实际上将[Tom McFarlin的示例](https://github.com/tommcfarlin/page-template-example)归功于此方法的创建者.The tutorial actually credits [Tom McFarlin's example](https://github.com/tommcfarlin/page-template-example) as the originator of this approach.
- 1
- 2015-10-12
- fireydude
-
- 2014-12-04
是的,有可能.我发现这个示例插件非常有用.
我想到的另一种方法是使用 WP Filesystem API 来创建主题模板文件.我不确定这是否是最好的方法,但是我确定它会起作用!
Yes, it is possible. I found this example plugin very helpful.
Another approach that is come into my head is using WP Filesystem API to create the template file to theme. I am not sure that it is the best approach to take, but I am sure it work!
-
链接的示例插件甚至都有很好的文档说明.我喜欢.:)The linked example plugin is even pretty well documented. I like that. :)
- 0
- 2017-08-14
- Arvid
-
- 2019-10-22
以前的答案都没有为我的工作.在这里,您可以在Wordpress管理员中选择模板.只需将其放在您的主要php插件文件中,并通过您的模板名称更改
template-configurator.php
//Load template from specific page add_filter( 'page_template', 'wpa3396_page_template' ); function wpa3396_page_template( $page_template ){ if ( get_page_template_slug() == 'template-configurator.php' ) { $page_template = dirname( __FILE__ ) . '/template-configurator.php'; } return $page_template; } /** * Add "Custom" template to page attirbute template section. */ add_filter( 'theme_page_templates', 'wpse_288589_add_template_to_select', 10, 4 ); function wpse_288589_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) { // Add custom template named template-custom.php to select dropdown $post_templates['template-configurator.php'] = __('Configurator'); return $post_templates; }
None of the previous answers was working for mine. Here one where you can choose your template in Wordpress admin. Just put it in your main php plugin file and change
template-configurator.php
by your template name//Load template from specific page add_filter( 'page_template', 'wpa3396_page_template' ); function wpa3396_page_template( $page_template ){ if ( get_page_template_slug() == 'template-configurator.php' ) { $page_template = dirname( __FILE__ ) . '/template-configurator.php'; } return $page_template; } /** * Add "Custom" template to page attirbute template section. */ add_filter( 'theme_page_templates', 'wpse_288589_add_template_to_select', 10, 4 ); function wpse_288589_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) { // Add custom template named template-custom.php to select dropdown $post_templates['template-configurator.php'] = __('Configurator'); return $post_templates; }
是否可以通过插件提供自定义页面模板?