How to Load a Specific Template for a Given URL in WordPress

Home » Snippets » How to Load a Specific Template for a Given URL in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Navigating WordPress’s template hierarchies can sometimes feel like a challenging maze. However, there’s no denying the immense flexibility and customization these options offer to developers. One such customization of great interest is the ability to load a specific template depending on a given URL. This can significantly enhance your users’ experience and site navigation. In this article, we will guide you through the process of assigning unique templates to certain URLs in an easy-to-understand, step-by-step manner.

					function wpturbo_load_specific_template($template) {
    if(is_page()) {
        if($_SERVER['REQUEST_URI'] == '/your-specific-url') {
            $new_template = locate_template(array('your-template.php'));
            if ('' != $new_template) {
                return $new_template;
            }
        }
    }
    return $template;
}
add_filter('template_include', 'wpturbo_load_specific_template');
				

The function wpturbo_load_specific_template($template) is the core of this code snippet. It checks if a particular page has to load a specific template based upon the URL.

if(is_page()) {

is_page() is an inbuilt WordPress function. It checks whether the current request is for a page post type. This function returns true when the current page matches the conditions.

if($_SERVER['REQUEST_URI'] == '/your-specific-url') {

The $_SERVER['REQUEST_URI'] is a global PHP server variable that holds the path of the current page, minus any parameters. Here we match it with ‘/your-specific-url’ to recognize the specific page on which we want to load the custom template.

$new_template = locate_template(array('your-template.php'));

Now if the specific URL is accessed, the function locate_template(array('your-template.php')) is run to locate the template in WordPress directories.

if ('' != $new_template) {
    return $new_template;
}

In case the template file is found (i.e., if $new_template is not an empty string), WordPress will return the path to the desired template file which will override the default or any other template file defined elsewhere.

Then the template file determined by $template is returned:

return $template;

This will work as the fallback for all other pages where the specific URL is not found.

The final line of the code, add_filter('template_include', 'wpturbo_load_specific_template');, hooks our function wpturbo_load_specific_template into the template_include filter. This filter allows you to change which template file is loaded for a certain condition.

In sum, WordPress will call this function when it is trying to decide which template file to use for the current request, and the function added to the template_include filter will override the default choice if the current request is for the page with a specific URL that you’ve defined.

Register an account to save your snippets or go Pro to get more features.