WordPress template_include - how to connect it correctly

I am currently encoding a WP plugin and must override templates.

My filter hook looks like this: and it does:

add_filter('template_include', 'mcd_set_template',10);

The mcd_set_template () function simply returns the desired path as a string - or the default WP template if the file does not exist.

I’ve been doing this for many hours, I can even include this alternative template (but it appears at the bottom of the page).

So my question is how to get WP 3.2.1 to simply load another template file instead - and what priority is required?

Update: I also noticed that when using var_dump ... it appears near the end of the file - but should appear before the opening HTML tag ...

According to this ticket, it should work with hook_include hook: http://core.trac.wordpress.org/ticket/11242

Or is this the only way to bind these filters: http://codex.wordpress.org/Template_Hierarchy#Filter_Hierarchy

+5
source share
2 answers

Have you tried using add_action? For example, you can try something like the following in your plugin:

add_action('template_redirect', 'mcd_set_template');
//Redirect to a preferred template.
function mcd_set_template() {
    $template_path = TEMPLATEPATH . '/' . "templatename.php";
    if(file_exists($template_path)){
        include($template_path);
        exit;
    }
}

Here is a useful link: http://www.mihaivalentin.com/wordpress-tutorial-load-the-template-you-want-with-template_redirect/

+3
source

template_redirect, , , , WordPress , . , , , .

, ...

add_action('template_include', 'mcd_set_template');

function mcd_set_template() {
    return locate_template('templatename.php');
}

, locate_template() . "template_redirect", , locate_template, .

add_action('template_redirect', 'mcd_set_template');

function mcd_set_template() {

      /**
       * Order of templates in this array reflect their hierarchy.
       * You'll want to have fallbacks like index.php in case yours is not found.
       */
      $templates = array('templatename.php', 'othertemplate.php', 'index.php');

      /**
       * The first param will be prefixed to '_template' to create a filter
       * The second will be passed to locate_template and loaded.
       */
      include( get_query_template('mcd', $templates) );

      exit;
}

, . , "category_template" "page_template". , , - WordPress

:

add_filter('category_template', 'filter_category_template');
function filter_category_template($template){
    /* Get current category */
    $category = get_queried_object();

    /* Create hierarchical list of desired templates */
    $templates = array (
      'category.php',
      'custom-category-template.php', 
      'category-{$category->slug}.php',
      'category-{$category->term_id}.php', 
      'index.php'
    ); 


    return locate_template($templates);
}

, , locate_template(). , , , .

+14

All Articles