0

I have this plugin I created that has a custom post type and a template file that renders the posts. I want to be able to render the template in any page using a short code. So what I created was this piece of code which I am sure is wrong and would totally appreciate it if someone could answer how I am supposed to render the template

function display_timeline(){

    ob_start();  
    locate_template(plugin_dir_path( __FILE__ ) . '/timelines.php', true, false);  
    $ret = ob_get_contents();  
    ob_end_clean();  

    return $ret;    

}

add_shortcode( 'wptimeline_display', 'display_timeline' );

Also, the template and the plugin functions are both in the same directory.

1

locate_template() only look for a specific template in themes, not in plugins, so your function wil never work. This also goes for get_template_part() which is in essence just a wrapper function for locate_template().

You will need to use funtions like file_exists() to check whether or not the required template exists and then use something like include() to load your template. You can use the above to write your own function like locate_template(), this is at least what I will do, and have a fallback to lacate a template in themes should the template not exist in the plugin.

Just a note, always always first check if a value/template/condition exists before doing something with it. This will avoid unnecessary bugs and issues should a failure occur. Also, have something in place like returning false, adding a redirect, exiting etc etc to safely handle your code should you have a failure

0

Please try using get_template_part('timelines'); instead of locate_template(plugin_dir_path( __FILE__ ) . '/timelines.php', true, false); I just given it a try and it works fine. Let me know if this solves your problem.

  • Hey, thanks but it didn't solve the issue on my end. I get a blank page as before – Bazinga777 Sep 20 '15 at 15:07
  • That is strange. Did you try turning your WP_DEBUG to true? Please let me know what errors you get so that I may assist. – Prasad Nevase Sep 20 '15 at 15:47
  • What Pieter answered above makes sense. – Prasad Nevase Sep 20 '15 at 16:41

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.