Getting started with WordPressget_bloginfo()Enqueuing scriptsMaking network requests with HTTP APIEnqueuing Styleshome_url()Custom Post Typestemplate_includeThe Loop (main WordPress loop)AJAXThe $wpdb ObjectActions and Filterswp_get_current_user()Add/remove contact info for users with user_contactmethods filter hookCreating a custom templateCustomizer Hello WorldCustomizer Basics (Add Panel, Section, Setting, Control)The Admin Bar (aka "The Toolbar")Querying postsAlternating main loop (pre_get_posts filter)ShortcodeCreate a Post Programmaticallyget_template_part()Taxonomiesget_template_part()ShortcodesPost FormatsCustom exerpts with excerpt_length and excerpt_morePlugin developmentSecurity in WordPress - EscapingTemplate hierarchyRemove Version from Wordpress and StylesheetsChild Theme Basicsadd_action()get_template_part()Shortcode with attributeSidebarsSecurity in WordPress - SanitizationinitCreate Template for Custom Post TypeFunction: add_action()Add ShortcodeHow Can I integrate Markdown editor with Advance Custom Field's repeater Add-on.Installation and Configurationwp_get_current_user()WP-CronSecure your installationOptions APIFunction : wp_trim_words()WP_Query() LoopUpdate WordPress ManuallyThemesWP-CLIDebuggingadd_menu_page()add_submenu_page()get_option()get_permalink()get_the_category()the_title()get_the_title()add_editor_style()add_theme_support()WordPress Plugin creationRun WordPress local with XAMPPAdmin Dashboard WidgetsSite MigrationMeta BoxRemove Auto Line Breaks From Content and Excerptget_home_path()Wordpress theme and child-theme developmentREST API

template_include

Other topics

Remarks:

You must return $template even if not modifying. If this confuses you, look at examples where apply_filter() has been used in code

You should not set up variables here for use later, there are better hooks for this.

A useful program flow for this filter is:

  1. Check $template includes our custom post type name --> template hierarchy!!
  2. if not, search our plugin for suitable files --> Its better to point to specific files rather than searching through folders for files. More efficient. But completely up to the developer.
  3. return the template.

Simple example

This filter is very useful. One of the common problems for developers is how to include templates in plugins they develop.

The filter is applied immediately after wordpress locates the appropriate template in the active child/parent theme using the wp hierarchy.

Be careful to define when you want to modify the template path. In the below example, the code checks to see if the current page is the single view of our custom post type cpt.

Simple enough example to get started with!

add_filter('template_include', 'custom_function');


function custom_function($template){
    
    //change a single post template...
    
    if( is_singular('cpt')  ){
         $template= 'path/to/another/template_file';
    }
      
    
    return $template;

}

More Adv example

add_filter('template_include', 'custom_function');


function custom_function($template){
    
    /*
    *     This example is a little more advanced. 
    *    It will check to see if $template contains our post-type in the path.
    *    If it does, the theme contains a high level template e.g. single-cpt.php
    *    If not we look in the plugin parent folder for the file. e.g. single-cpt.php
    */
    
  
    //check to see if the post type is in the filename (high priority file)
    //return template if it is!
    
    global $post;
   
    if( strpos($template, 'single-'.$post->post_type.'php' ) !== false && strpos($template, 'archive-'.$post->post_type.'php' ) !== false ){
        return $template;
    }
    
    $plugin_path = 'var/etc/wp-content/plugins/plugin'; //include own logic here...
    
    if( is_singular($post->post_type) && file_exists($plugin_path.'/single-'.$post->post_type.'.php')   ){
         $template= $plugin_path.'/single-'.$post->post_type.'.php';
    } elseif ( is_archive($post->post_type) && file_exists($plugin_path.'/archive-'.$post->post_type.'.php') ) {
         $template= $plugin_path.'/archive-'.$post->post_type.'.php';
    } 
      
    return $template;
}

Parameters:

ParameterExplanation
$templatePasses one parameter to the filter, $template is the current path to the appropriate file for the post type as found in the active child theme or parent theme (if no child theme in place or child theme has lower ranked templates. See wordpress template hierarchy for more details).

Contributors

Topic Id: 1439

Example Ids: 4701,4702

This site is not affiliated with any of the contributors.