1

I am developing a custom plugin where I am using a single page template and changing the template parts only using ajax,I want to retrieve the template parts slugs or name (like custom-header.php, custom-footer.php, custom-sidebar.php, custom-profile.php etc).

Is there any possible way to get all the template parts names which are used in a page ?

something like get_included_files() we can not use it in wordpress as it may show hundreds of files included, for me it is 156 :P and non-assosiative array :P

  • You mean you want to get all page templates name ? Custom page template name ? – Sumit May 20 '16 at 10:27
  • @Sumit I want all template-part's name in a page. not the master template used for a page . – Prafulla Kumar Sahu May 20 '16 at 14:42
2

You could filter the include paths returned by get_included_files by removing any files from the list that are not in your theme (and/or child theme) directory:

function get_theme_includes() {

    $includedfiles = get_included_files();
    // normalize theme paths for matching
    $styledir = str_replace("\\","/",get_stylesheet_directory());
    $templatedir = str_replace("\\","/",get_template_directory());

    $i = 0; // loop included files
    foreach ($includedfiles as $includedfile) {
        // normalize include path for match
        $includedfile = str_replace("\\","/",$includedfile);
        // check if included file is in stylesheet directory
        if (substr($includedfile,0,strlen($styledir)) != $styledir) {
            // if stylesheet is same as template, not a child theme
            if ($styledir == $templatedir) {unset($includedfiles[$vi]);}
            else {
                // check if included file is in template directory
                if (substr($includedfile,0,strlen($templatedir)) != $templatedir) {unset($includedfiles[$i]);}
                else {
                    // strip template directory from include path
                    $pathinfo = pathinfo(str_replace(dirname($templatedir),'',$includedfile));
                    // add filename.php => pathinfo array to the template array
                    $themeincludes[$pathinfo['basename']] = $pathinfo;
                }
            }
        } else {
            // strip stylesheet dir from include path
            $pathinfo = pathinfo(str_replace(dirname($styledir),'',$includedfile));
            // add filename.php => pathinfo array to the template array
            $themeincludes[$pathinfo['basename']] = $pathinfo;
        }
        $i++;
    }
    return $themeincludes;
}

Depending on when this function is run however you would get different results, you can use this to your advantage to further remove early theme includes since those are likely not a page templates.

add_action('wp_loaded','check_theme_includes');
add_action('wp_footer','check_theme_templates');

function check_theme_includes() {
    global $themeincludes; $themeincludes = get_theme_includes();
}

function check_theme_templates() {
    global $themeincludes, $templateincludes;
    $templateincludes = get_theme_includes();

    // strip out already included theme files from template list
    foreach ($templateincludes as $template => $pathinfo) {
        if (array_key_exists($template,$themeincludes)) {
            if ($pathinfo['dirname'] == $themeincludes[$template]['dirname']) {
                unset($templateincludes[$template]);
            }
        }
    }

    // debug point
    // echo "<!-- INCLUDED TEMPLATES: "; print_r($templateincludes); echo "-->";

   // output template array for use by jquery/ajax
   echo "<script>var templatenames = new Array(); var templatepaths = new Array(); ";
   $i = 0;
   foreach ($templateincludes as $template => $pathinfo) {
       // optionally strip the .php extension
       $template = str_replace('.php','',$template);
       // output the template array key/value
       echo "templatenames[".$i."] = '".$pathinfo['filename']."'; ";
       echo "templatepaths[".$i."] = '".$pathinfo['dirname']."'; ";
       $i++;
   }
   echo "</script>";
}

Which for example would give something like this in your page footer:

<script>var templatenames = new Array(); var templatepaths = new Array(); templatenames[0] = 'front'; templatepaths[0] = '/bioship/sidebar'; templatenames[1] = 'subfront'; templatepaths[1] = '/bioship/sidebar'; templatenames[2] = 'index'; templatepaths[2] = '/bioship'; templatenames[3] = 'header'; templatepaths[3] = '/bioship/sidebar'; templatenames[4] = 'loop-hybrid'; templatepaths[4] = '/bioship'; templatenames[5] = 'content'; templatepaths[5] = '/bioship/content'; templatenames[6] = 'loop-nav'; templatepaths[6] = '/bioship/content'; templatenames[7] = 'footer'; templatepaths[7] = '/bioship/sidebar'; </script>

Which would allow you to compare these arrays and add to them with jQuery/javascript callbacks.

  • Your answer seems to be something nearly fulfilling my need.my situation is the templates and template-parts are inside my plugin and I want to maintain session with different template parts that is why I need to know when the request for another template part is sent through ajax which template part is used to display the contain and if any user is logged in that time .I am trying to modify your code to meet that. – Prafulla Kumar Sahu May 20 '16 at 14:46
  • 1
    replacing get_stylesheet_directory with dirname(__FILE__) in the first function should output the plugin templates instead (if using it in a plugin file.) of course you can make further tweaks to the variable names etc and remove parent template checking too, but as far as I can see that would be enough. – majick May 21 '16 at 5:39
  • 1
    a pleasure, thanks for finding get_included_files - I was looking to use it for debugging a complex project not long ago and simply couldn't find it. :-) – majick May 21 '16 at 9:13

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.