Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
Use plugin_dir_path(). wp-content can be moved and renamed. –  toscho Jun 25 '12 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 '12 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 '12 at 7:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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