Introducing page templates (customized pages) in WordPress in the form of plug-ins

In WordPress, you can use different page templates to create pages. Generally, page templates are introduced by themes to page-xxx.php The form of is stored in the theme folder. If you want to Files in other locations are registered as page templates How to do it?

demand

Recently, I wrote a plug-in to register an existing page template to WordPress Page Template List Medium. The traditional way of introducing page templates requires copying files to the theme directory, but this violates the original intention of the plug-in.

I hope the plug-in can register the template file in the plug-in folder or other locations to the WordPress page template list, so that users can install or update the template only by installing or updating the plug-in.

Another advantage of this is that even if the user changes the theme, the page template I inserted will still work normally.

realization

WordPress provides two hooks related to template loading.

  1. theme_page_templates
    theme_page_templates It is the function hook called by WordPress when it gets the list of page templates.

     //Wild/rabbit @ catalpa/meow/come out/disappear (www.azimiao. com) apply_filters( 'theme_page_templates', array $page_templates, WP_Theme $this, WP_Post|null $post )

    Among them, $page_templates This is the original page template list array.

  2. template_include
    template_include It is a function hook called by WordPress when it obtains the path of the template file before including the template file.

     //Zimiao.com apply_filters( 'template_include', string $template )

    Among them, $template This is the original page template file path.

Using the above two function hooks, you can easily complete the requirements. The example code is as follows:


//Define the template file path prefix, and use the plug-in root directory here define('ZM_PLUGIN_DIR', plugin_dir_path(__FILE__)); //List of external templates (Zimiao appearance blog | azimiao. com) to be imported $templates_new = array( "Page mycustom. php"=>"My presence 1" ); function zm_register_page(){ add_filter('theme_page_templates', 'zm_add_template'); add_filter('template_include', 'zm_view_template'); } function zm_add_template( $posts_templates ) { global $templates_new; $posts_templates = array_merge( $posts_templates,$templates_new ); return $posts_templates; } function zm_view_template( $template ) { global $post; global $templates_new; if ( !isset( $post ) ) return $template; //Get the page (Zimiao haunt blog | azimiao. com) template name $t_template_name = get_post_meta( $post->ID, '_wp_page_template', true ); //The page template is not in the custom list. Return directly if ( ! isset( $templates_new[ $t_template_name ] ) ) { return $template; } //The correct path of splicing template (Zimiao haunting blog | azimiao. com) //Assume that the template is stored in the plug-in directory/custompage/ $file = ZM_PLUGIN_DIR . ' custompage/' . $ t_template_name; //(Zimiao haunts blog | azimiao. com) if( file_exists( $file ) ) { return $file; } return $template; }

In the above code, I use the function hook to convert the page template Zimiao haunts 1 Add to the list of WordPress page templates. Then, when WordPress loads the page template file, it returns the correct template file path.

Finally, you only need to call the zm_register_page , you can attach the function hook. The code is as follows:

 add_action( "plugins_loaded", "zm_register_page");

verification

In the WordPress page editor, you can (Zimiao haunt blog | azimiao. com) see the customized page Zimiao haunts 1 Has been added to the list of page templates.

It can be used as a normal page template.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/6409.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*