I'm writing a plugin that lets people add course information online. 'Courses' is a custom post type. I've already made the admin menu to add the course information to the database (in the post meta table).

Now I need to bring it out onto the front end. Ideally I'd like to make a new template page (single-courses.php) so I can fully customise the page. I've not found anything that fits the bill online yet, is there an easy way of generating this through the plugin?


1 Answer 1


I managed to do it, not perfect but it works...

add_action('template_redirect', 'my_template');

function my_template(){
global $post;
if( get_post_type($post->ID) == 'courses'){
include(ABSPATH. 'wp-content/plugins/courses/single-course.php');

And the file the include points to is my new template file in the plugin.

  • Use plugin_dir_path(). wp-content can be moved and renamed.
    – fuxia
    Jun 25, 2012 at 13:53
  • With the uncertainty of themes, I think this is generally a challenge. You might look at The Events Calendar which has fairly robust templating for a plugin. They do something similar to this. Also, you might consider filtering the_content instead of adding a template. That strikes me as much safer when it comes to cross-theme compatibility.
    – mrwweb
    Jun 25, 2012 at 17:47
  • Yep I actually did a filter on the_content first, i agree i thought it was safer, but my boss and collegues seemed to think a template would be better so just doing what I'm told! Thanks for the answers, I've kept it as it is redirecting to the new template, I checked out the event calendar, there is some good but quite heavy stuff there! And I'll change the abspath to something more futureproof. Thanks :)
    – danbrown
    Jul 2, 2012 at 7:55

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.