Make WordPress Core

Opened 3 years ago

Last modified 18 months ago

#19296 new enhancement

add/apend templates 'transparently' via plugin to currently active theme or child theme.

Reported by: anointed Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords: close
Focuses: Cc:


In building a custom 'theme plugin' for bbpress I was just made aware that it is impossible for a plugin to 'transparently' add/append template files to the currently active theme or child theme.

I'd like to request the enhancement where when a theme is looking for a custom template such as single-forum.php to search a plugin folder if it is not found in the child theme or parent theme.

I'm thinking of some type of action where my plugin can say 'add me' to the list of places you are looking for templates.

If my understanding is correct, when WordPress is looking for a template it starts in the child, then the parent theme but ends there.

If I have a user manually add all of the custom templates needed to their current theme, and they update their theme, they will loose all the templates they manually added.

This means I have no way of giving a user custom theme templates that won't be lost when upgrading, thus requiring them to manually add the templates back in every time they upgrade their theme.

What it really means for me is that there is no real path for me to create custom bbpress templates for users where I don't have to worry about them loosing them.

I can go the route that bbpress currently uses in this situation which is that bbpress uses the themes page.php template and replaces the_content(). The problem is I am stuck with whatever is included in the active themes page.php file.

*for instance twentyten has the title hardcoded in and the call to the sidebar, which I can't remove

I can't go the child theme route, because I have to assume that they are already using a child theme.

Another dev told me today that this issue came up about 2 years ago. I am praying that there was proof of concept code somewhere that I can try to utilize. Does anyone happen to know if concept code exists anywhere?

Change History (6)

comment:1 dd323 years ago

There have been numerous requests for this.

It ultimately boils down to this simple fact:

Markup differs between themes, Try finding some markup which will work with most themes allowing you to just replace page.php for example, near impossible.

comment:2 anointed3 years ago

If I can get my plugin to append the template files then I am able to take care of the issue you mention without problems.

Long story short, I have the user input the top portion of their page.php file and save that as a variable in a plugin admin panel. The same thing is done for the second half of their page.php file. *my custom code is basically inserted between the variables in the template files.

I then call these variables in the opening and closing portions of my custom templates, thus making it work on almost all themes and only requiring a small amt of work by the user.

My problem is not having a way for my plugin to append all these new templates to the users currently active theme. That is what I am looking for.

comment:3 anointed3 years ago

@dd32 this part doesn't matter much, but will show how I was attacking the problem you explain:

example of archive-forum.php

<?php do_action( 'page_pre' ); ?>

<div class="tumbleboard" role="archive-forum">

	<?php bbp_get_template_part( 'bbpress/content', 'archive-forum' ); ?>

</div><!-- /.tumbleboard -->

<?php do_action( 'page_post' ); ?>

I use a plugin admin page where the user populates page_pre like so, twentyeleven used as example:

<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">

page_post would be like so:

</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

As you can see, it is easy for me to make this work with any scenario and only requires the user to fill in 2 textarea blocks on the plugin admin page thus making it work with just about any theme.
*may be better ways of doing this, but heck it works and I'm rather new.

Now, to just get WordPress to append/add these files to the active theme.
*that's the part I am requesting as an enhancement, or for concept code*

comment:4 curtismchale3 years ago

  • Cc curtis@… added

comment:5 johnbillion3 years ago

  • Keywords close added

All you need is the template_include filter (I've just realised this is a poorly documented filter). Then your custom template would look something like:

// output the user entered header code
// output all the custom stuff that the plugin is providing
// output the user ented footer code
Note: See TracTickets for help on using tickets.