0

I decided to organize my work in FTP because I've got over 25 templates by now and the list of files/directories in theme's root is getting really long.

I stumbled on article where I learned that page-templates directory in theme's root is read by default and everything should be fine.

I manage all my 3rd party enqueued scripts, css and much more with is_page_template() in order to load them only when needed.

Problem is that is_page_template() doesn't read templates now.. Why? Do I need to declare or define anything in functions.php or something?

Currently: wp-content/themes/my-theme/page-templates/templates.php

Previously: wp-content/themes/my-theme/templates.php

0

When a page is assigned to a template, this template selection is saved in the post meta table under the key _wp_page_template. These template values are as follow

  • default if no page template in explicitely set

  • {$template}.php for templates in root

  • subfolder/{$template}.php for templates in a subfolder where subfolder should be the name of the subfolder

is_page_template() explicitely compares the value from the template name passed to it to the value stored in _wp_page_template. Note, the page template is returned by get_page_template_slug() inside is_page_template(). If these two values don't match, false is returned

Example:

If we have a template called template.php we would match that with is_page_template( 'template.php' ), because the value of _wp_page_template for the specific page will be template.php.

When we move this template to a subfolder called subfolder and reassign the page template to the page, the value of _wp_page_template will be subfolder/template.php, abviously which will fail is_page_template( 'template.php' ) because 'template.php' !== 'subfolder/template.php'

You will need to manually adjust your conditional statement to is_page_template( 'subfolder/template.php ) to match up with the move to subfolders as there are no filters that I can see to safely change this.

EDIT from comments

  1. Change the structure aka move templates to directiory

As you already explained, moving templates to a subfolder breaks the current structures

  1. Reassign page templates

You will need to reassign pages to the correct template again. If you move templates (our example is template.php being moved into subfolder) into a subfolder, the value of _wp_page_template does not change. The old value (which is template.php in our example) remains in db. With this in mind, it is very easy to bulk update all pages and reassigning the correct page template to a page.

All we need to do is to get the old value from db and then resaving the value with subfolder/ appended to the old template name. I have written a small action to achieve this, no you do not need to do it manually anymore ;-). All you need to do is to add an array of page names which you have moved to the value parameter in the meta_query , this will get all pages which have those old templates assigned to them, then you simply append subfolder/ to the template names and resave them.

As the action runs on admin_init, all you need to do is, load any admin page once, and your done., After that you can remove the function. Just one note before running the action, make double sure all values are correct before you run the action as it alters values in the db. Also have a backup of the db stored somewhere

add_action( 'admin_init', function ()
{
    // Get all pages in order to change the template paths to subfolder
    $args = [
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'meta_query'     => [
            [
                'key' => '_wp_page_template',
                'value' => ['template.php'] // Set all the templates you have moved into an array
            ]
        ]
    ];
    $q = get_posts( $args );

    if ( !$q )
        return;

    foreach ( $q as $id ) {
        // Get the current template name saved
        $template = get_post_meta( $id, '_wp_page_template', true );
        // The name of the subfolder with trailing slash
        $subfolder_name = 'subfolder/';
        // Update the templates to add the subfolder name. Change name as needed
        update_post_meta( 
            $id, // Page ID
            '_wp_page_template', //Meta key under which the page template name is saved
            $subfolder_name . $template, //the new value of the template name, in this case subfolder/{$template}.php
            $template // Old value of the template name
        );
    }
});
  1. Add page-templates/ path to every is_page_template() to my every code?

That is correct. There is unfortunately no other way to to this. You can bulk update this in your theme if you have a good code editor

I wasn't sure about the 3. step because what I read from article was that WP "knows" by default if templates are in page-templates/ directory

Wordpress does not know that. Another wrong in the codex, one again. Wordpress uses the value stored in _wp_page_template to load the correct template from either root or a subfolder according to the value returned. Without knowing the value of _wp_page_template, Wordpress will not know which template was assigned to the page and where to look for the template. template.php means the page template is in root, subfolder/template.php means the template is in the subfolder calles subfolder.

You can check out how templates are loaded for pages by looking at the source code for get_page_template()

  • Did I understand correctly? 1. Change the structure aka move templates to directiory 2. Reassign page templates 3. Add page-templates/ path to every is_page_template() to my every code? Okay, it's a bit of searching, Im pretty sure there are more of those than only in functions.php but I think that organized FTP is worth it. I wasn't sure about the 3. step because what I read from article was that WP "knows" by default if templates are in page-templates/ directory. – N00b Dec 15 '15 at 16:42
  • In short, the answer to all three questions is yes. As for step 3, Wordpress only knows that a template is in a subfolder because it is saved as such in _wp_page_template ;-). Question 2 is relatively easy if you haven't yet made the move. We can create a small scripts to change templates on movement of the templates – Pieter Goosen Dec 15 '15 at 19:56
  • Please see my update, I have also included a way to reassign templates in bulk automatically – Pieter Goosen Dec 16 '15 at 13:59
0

Before clarifying the problem. Just a simple explanation, how the system of templates working in WordPress.

You may create a template file with following code on your php file which you put it on your themes root directory.

 <?php
  /**
   * Template Name : My template page
   **/?>

And then you moved to select the template for a page by moving to wp-admin. When you are selecting a template here, it will store an post meta in posts meta table with path of the template file which you have choosen under _wp_page_template meta and it's value is my-template.php which is there in root folder of your theme files. I mean the Same directory which has the style.css.
So it queries the path and get it when the page is viewed. Now if you moved the file to another folder. Than the path becomes invalid now.

And if you move the file to another sub directory. Than it should be like this sub-directory/my-template.php. Than will work perfectly.

So the problem is path of the file when you move to another folder. And the solution is

You have to move files one by one and once a file is moved just goto the back-end of your site and followed by pages than select the page to edit and again select the template from the list and update the page again.

That's it.

Edit: I am not guessing it, see the attached screenshot . I created a new file and saved it with my own custom template code. Than i created a page in the backend by selecting the template . Than you can see the posts_meta table. I checked those two fields in it, The post ID is 11. and check the meta ID 21. Whether I am guessing or giving the actual working. You can see the file path in editors top.

First screenshot

and after the first screenshot i moved the file to sub directory inc. than I saved the template page again and took the another screenshot.

Second Screenshot After changes

  • You are making guesses here which in turn lead to wrong information being given to someone that does not know better. -1 – Pieter Goosen Dec 15 '15 at 6:01
  • Where the hell are you getting _template_page. What you are saying is that, as a developer, I should assume you are talking about an apple if you are writing orange. Bull my friend. Page template selection are saved as _wp_page_template, period. There is no such thing as _template_page or suppose templates are save in _template_page. It is deceiving info, incorrect and deserves to be downvoted – Pieter Goosen Dec 15 '15 at 8:14
  • I am not saying Apple and orange. It's instead of green Apple, I am saying Apple. Ok my friend, your answer is always right. Do you want me to delete my ( guess) answer. – Kvvaradha Dec 15 '15 at 8:26
  • Please post correct info. I have edited your post and removed incorrect data. Also, if you have issues why your posts is downvoted, do not discuss those issues in you posts as it has no relevance to the question at hand. Open a new discussion on WordPress Development Meta if you have issues with downvotes – Pieter Goosen Dec 15 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.