Créer des modèles de page personnalisés avec des plugins?
4 réponses
- votes
-
- 2010-11-02
get_page_template()
peut être remplacé via lefiltrepage_template
. Si votrepluginest un répertoire contenant lesmodèles sousforme defichiers,il suffit depasser lesnoms de cesfichiers. Si vous souhaitez les créer "à la volée" (lesmodifier dans la zone d'administrationet lesenregistrer dans labase de données?),Vouspouvez les écrire dans un répertoire de cacheet yfaire référence,ou vous connecter àtemplate_redirect
et faites uneeval()
trucs.Unexemple simple deplugin qui "redirige" vers unfichier dans lemême répertoire deplugin si un certain critèreest vrai:
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; }
-
Hey Jan,avez-vous unexemple de code sur lafaçon detransmettre unfichier depluginen tant quemodèle depagepersonnalisé?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: C'est vraiment simple,j'ai ajouté unexemple simple àma réponse.@trnsfrmr: It's really easy, I added a simple example to my answer.
- 0
- 2010-11-16
- Jan Fabry
-
Notez que cela a étéplus oumoins remplacépar lefiltre "template_include" dans les versions ultérieures (3.1+).Note that this has been more or less replaced by the "template_include" filter in later versions (3.1+).
- 12
- 2013-06-10
- Inigoesdr
-
Parfait !!!,vous avezgagnémontemps @JanFabryPerfect !!!, you have save my time @JanFabry
- 0
- 2018-12-21
- Kishan Chauhan
-
Commeindiquépar @fireydude,cen'estpas une solutiongénérique.C'est une solution de contournement qui codeen dur le slug depage.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
Remplacer
get_page_template()
estjuste un hack rapide. Ilne permetpas de sélectionner lemodèle àpartir de l'écran d'administrationet le slug depageest codéen dur dans leplugin,de sorte que l'utilisateurn'a aucunmoyen de savoir d'où vient lemodèle.La solutionpréférée serait de suivre cetutoriel qui vouspermet d'enregistrer unmodèle depage dans leback-end àpartir duplug-in. Ensuite,celafonctionne commen'importe quel autremodèle.
/* * 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', ); }
-
Ce seraitbien (*et préféré *) si vouspouvezpublier le code correspondant àpartir du lien dans votre réponse,sinon cen'est rien deplus qu'un commentairegonflé :-)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
-
Letutoriel attribueen fait [l'exemple de Tom McFarlin] (https://github.com/tommcfarlin/page-template-example) à l'initiateur de cette approche.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
Oui,c'estpossible.J'aitrouvé cet exemple deplugin très utile.
Une autre approche quime vient à l'esprit consiste à utiliser WP Filesystem API pour créer lefichier demodèle sur lethème.Jene suispas sûr que ce soit lameilleure approche à adopter,maisje suis sûr que celafonctionne!
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!
-
Leplugin d'exemple liéestmême assezbien documenté.J'aime ça.:)The linked example plugin is even pretty well documented. I like that. :)
- 0
- 2017-08-14
- Arvid
-
- 2019-10-22
Aucune des réponsesprécédentesne fonctionnaitpour lamienne. Ici,vouspouvez choisir votremodèle dans l'administration Wordpress. Mettez-le simplement dans votrefichierprincipal depluginphpet changez
template-configurator.php
par lenom de votremodèle//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; }
Est-ilpossible de rendre desmodèles depagepersonnalisés disponibles àpartir d'unplugin?