Site icon Milind More – WordPress Developer

WordPress Plugin Template Redirects Custom Post, Page, Taxonomy

This PHP code, typically found in a WordPress plugin or theme, intercepts the template loading process using the template_redirect action to load custom template files based on the current URL query.

It checks the global $wp query variables to determine the page context: if the pagename is ‘agenda’, it loads page-event-list.php; if the post_type is ‘events’, it loads single-event.php; and if it’s a ‘location’ taxonomy archive, it loads taxonomy-event_location.php.

For each case, it first checks if the active theme provides a file override within TEMPLATEPATH, otherwise defaulting to a template file located in the plugin’s own /templates/ directory.

The custom function do_theme_redirect handles including the correct file and then calls die() to stop WordPress from loading its default templates, effectively implementing the custom routing for specific content types.

<?php

// Hook into WordPress's template loading process
add_action('template_redirect', 'my_theme_redirect');

/**
 * Redirects to a custom template file based on the current query.
 */
function my_theme_redirect() {
    global $wp;
    
    // Get the current directory of the plugin/theme file
    $plugindir = dirname(__FILE__);
    
    // Initialize template filename and path variables
    $template_filename = '';
    $return_template = '';

    // --- CASE 1: A Specific Page by Title/Name (e.g., /agenda) ---
    if (isset($wp->query_vars['pagename']) && $wp->query_vars['pagename'] === 'agenda') {
        $template_filename = 'page-event-list.php';
        
        // Check if the theme has overridden the template
        if (file_exists(TEMPLATEPATH . '/' . $template_filename)) {
            $return_template = TEMPLATEPATH . '/' . $template_filename;
        } else {
            // Use the default template inside the plugin's 'templates' folder
            $return_template = $plugindir . '/templates/' . $template_filename;
        }

        do_theme_redirect($return_template);
    }

    // --- CASE 2: A Specific Custom Post Type (e.g., /events/my-event) ---
    elseif (isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] === 'events') {
        $template_filename = 'single-event.php';
        
        // Check if the theme has overridden the template
        if (file_exists(TEMPLATEPATH . '/' . $template_filename)) {
            $return_template = TEMPLATEPATH . '/' . $template_filename;
        } else {
            // Use the default template inside the plugin's 'templates' folder
            $return_template = $plugindir . '/templates/' . $template_filename;
        }

        // Custom function check before redirecting (assumed from original code)
        if (function_exists('dmk_redirect_to_login_if_not_logged_in')) {
            dmk_redirect_to_login_if_not_logged_in();
        }
        
        do_theme_redirect($return_template);
    }

    // --- CASE 3: A Custom Taxonomy Page (e.g., /location/new-york) ---
    // Note: The original logic `is_tax('location') == 'location'` is unusual. 
    // I've kept the explicit `is_tax` check for clarity in case that was the intent.
    elseif (isset($wp->query_vars['taxonomy']) && $wp->query_vars['taxonomy'] === 'location' || is_tax('location')) {
        $template_filename = 'taxonomy-event_location.php';
        
        // Check if the theme has overridden the template
        if (file_exists(TEMPLATEPATH . '/' . $template_filename)) {
            $return_template = TEMPLATEPATH . '/' . $template_filename;
        } else {
            // Use the default template inside the plugin's 'templates' folder
            $return_template = $plugindir . '/templates/' . $template_filename;
        }

        do_theme_redirect($return_template);
    }
}

// ----------------------------------------------------------------------

/**
 * Loads the specified template file and halts execution, or sets a 404.
 *
 * @param string $url The full path to the template file.
 */
function do_theme_redirect($url) {
    global $wp_query; // $post is not strictly needed here unless you use it in the included file
    
    // Ensure there are posts available to display
    if (have_posts()) {
        include($url);
        // Important: Stop the standard WordPress template loading process
        die();
    } else {
        // If no posts are found, force a 404 response
        $wp_query->is_404 = true;
    }
}Code language: PHP (php)
Exit mobile version