0

I have this simple template in a my_template.php file:

<div class="popup_canvas_container">
    <p>This is a template I'm gonna load</p>
</div>

And I implemented this function to load the template from custom shortcode:

public static function load_template($atts) {       
    function load_my_template() {
        ob_start();

        //if ( file_exists(TEMPLATE_MODULE_DIR . 'my-template.php') ) {
        //  include_once( TEMPLATE_MODULE_DIR . 'my-template.php' );
        //}


        get_template_part('my-template');
        //locate_template( TEMPLATE_MODULE_DIR . 'my-template.php', true, true );
        
        return ob_get_clean();
        
        //global $wpdb;                     
        //$current_user_id = $wpdb->get_var( "SELECT user_email FROM newk3_users WHERE user_login = 'xxxxxx' " );       
        //$result = "<div class='box_external'><p>$current_user_id</p></div>";
        //$result = "This is return";       
        
        //return $result;
        
    }
    add_shortcode( 'site_template', 'load_my_template' );

If I use include_once the template would load in the page but if I use wordpress's functions like get_template_part or locate_template it doesn't load the template... since with include_function works I thought maybe I missed something else, even if I put also the complete path to the template file.... could you give me a direction?

Thanks in advance to everyone! Cheers Luigi

3
  • Where's the template file located? Because if for example it's in a plugin folder, then it's normal if get_template_part() or locate_template() did not load the template.
    – Sally CJ
    Commented Dec 13, 2021 at 7:57
  • 1
    Hi Sally! Thank you for your answer...The cute thing is I debugged that TEMPLATE_MODULE_DIR . 'my-template.php' and the path in a plugin's subfolder is right... anyway the template is in the same path of the php where I call that template... In fact I was asking just because debugging all ways I get the path, absolute or relative that is, it was right but just the function wasn't loading anything... in fact as I said in my post, with include_once it loads perfectly...
    – Luigino
    Commented Dec 13, 2021 at 8:34
  • Yes, and that's why I asked where the template file is located. But anyway, check my answer and see if it helps?
    – Sally CJ
    Commented Dec 13, 2021 at 12:55

1 Answer 1

0

From your comment: (formatted for brevity)

The cute thing is I debugged that TEMPLATE_MODULE_DIR . 'my-template.php' and the path in a plugin's subfolder is right

in fact as I said in my post, with include_once it loads perfectly

So because you said it's in a plugin subfolder (in wp-content/plugins), then as I said in the comment, it's normal if get_template_part() or locate_template() did not load the template.

And that's because locate_template() (which is used by get_template_part()) only searches templates in the active theme's folder in wp-content/themes and wp-includes/theme-compat if necessary, so for templates in other places like a plugin folder, you would need to use require or include, or their "_once" version. Or there's load_template() if you want to use it instead.

Try placing a copy of your my-template.php in the active theme folder and add "test loading from the active theme folder" somewhere in the template, then see if get_template_part() and/or locate_template() now loads the template?

1
  • 1
    Hello Sally! Thanks a lot for your explanation.... in fact it was looking in template folder so just had to use include/require for plugin implementations.. thanks again
    – Luigino
    Commented Dec 16, 2021 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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