WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#15803 closed feature request (duplicate)

Page Templates within Plugins

Reported by: sterlo Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.3
Component: Template Keywords: needs-patch
Focuses: Cc:

Description

It would be awesome if we could create a Page Template from within a the confines of a Plugin.

Currently, we have to create Page Templates from within a Theme.
Source: http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

If we could add an action or if WordPress could look inside a plugin's directory, we could have Custom Page Templates within a plugin.

Story:
If The User has a Theme activated that has a persistent sidebar and when My Awesome Plugin requires a page to not have a sidebar (it needs to use the full with of a site - or whatever - let your imagination run wild), then there is currently little recourse - other than going into the theme and adding a Page Template.

This can leave a Plugin "Theme Dependent".

Ideas:

  • Add a hook for Page Templates
  • Extend WordPress' search for Page Template to include plugin directories

Change History (14)

comment:1 sterlo3 years ago

More research:

I understand that there is a hook for template overrides:
http://codex.wordpress.org/Plugin_API/Action_Reference#Template_Actions

But since we are already doing this: #13818

Why wouldn't it be a good fit here?

comment:2 WraithKenny3 years ago

http://www.unfocus.com/2010/08/10/including-page-templates-from-a-wordpress-plugin/

The bigger issue is that themes' code/css varies wildly. You could code up page templates that work with Twenty-Ten and some of the Frameworks/Parent Themes like Hybrid or Thematic but no solution would ever work with the majority of themes. The theme's CSS requires too specific markup and class/ids. The tutorial above will explain how to implement the code in your own plugin if you'd support some themes over the rest.

comment:3 WraithKenny3 years ago

In other words, doing "Page Templates in Plugins" would certainly make the plugin "Theme Dependent."

comment:4 follow-up: coffee2code3 years ago

Implementation of my most recent patch for #13239 can achieve this (among other things).

In fact, my latest comment there includes example code for a plugin to override theme templates (by giving preference to templates defined in a sub-directory of the plugin's directory), which appears to be exactly what you're requesting.

Depending on what you're doing and how you're doing it, though, your "theme independence" may meet with varying success in practice.

comment:5 in reply to: ↑ 4 sterlo3 years ago

Replying to coffee2code:

Implementation of my most recent patch for #13239 can achieve this (among other things).

In fact, my latest comment there includes example code for a plugin to override theme templates (by giving preference to templates defined in a sub-directory of the plugin's directory), which appears to be exactly what you're requesting.

Depending on what you're doing and how you're doing it, though, your "theme independence" may meet with varying success in practice.

Thank you!

What I got from that: Plugins should not provide Page Templates because the environment they would operate in is dependent on the Theme by nature.

What I am thinking in regards to that:
As for the conflicts/varying success: I suppose if you name-spaced your markup and included your own CSS...it would turn out fine?

A Theme includes it's Header/Footer - which may/may-not contain a wrapper around the content. But the code for the plugin's template can be written in such a way that it renders successfully with or without a container.

For instance - a plugin that interacts with an outside API and returns data - but it needs to have it's own template to allow the user to manipulate that data.

It can be built in a modular fashion so that it works independent of any theme.

As for the patch: Looks good to me. If it does what it needs to do then it would probably solve my needs.

comment:6 sterlo3 years ago

  • Version set to 3.0.3

comment:7 sterlo3 years ago

FYI: The current way to I am having to get this to do what I want it to do is add a call to template_redirect, then use die() after including my file (which has to include the header and footer templates from the theme) and then I have the plugin inject a category with the same slug as my custom post type (if I don't...it displays the content properly but returns a 404).

comment:8 jane3 years ago

The BuddyPress Template Pack plugin creates templates within an existing theme. Because of variability issues mentioned by others on this ticket, the plugin has to walk the user through a couple of steps to make it all work/look right.

comment:9 jane3 years ago

  • Milestone changed from Awaiting Review to Future Release

comment:10 sterlo3 years ago

I believe I've figured a more elegant way to deal with these things. After I have viable proof I will post the solution.

comment:11 sterlo3 years ago

Elegant solution for the time being:

Add a rewrite rule...like:

106     function add_rewrite_rule( $existing_rules ) {
107 
108       $new_rule = array();
109 
110       $new_rule[ '^(mycustomtaxonomy)' ] = 'index.php?taxonomy=mycustomtaxonomy';
111
112       return $new_rule + $existing_rules;
113 
114     }

Then add a function call to template_redirect and check to see if the taxonomy is set in query_vars.

If it is, then:

get_header();
# DISPLAY TEMPLATE HERE
get_footer();
exit;

This will cleanly do the work for you until there is something better.

Make sure to flush the rules so they are rebuilt.

comment:12 urbandev3 years ago

  • Cc urbandev added

comment:13 ocean902 years ago

  • Component changed from Plugins to Template
  • Keywords needs-patch added

Closed as a duplicate: #20034

comment:14 scribu2 years ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.