WordPress.org

Ready to get started?Download WordPress

Forums

Duplicative Templates (19 posts)

  1. danweasel
    Member
    Posted 9 years ago #

    So, I love the whole themes concept; it works great and all, but I do have one (small) issue with it. If I make modifications to a theme---the sidebar for instance---by adding in non WP-managed links, links to Bloglines, or whatever; in order for these to be persistent across multiple themes (if I wanted to provide several themes for my users to choose between), I would have to duplicate this code across every available theme and update it in each theme package everytime I wanted to make a change.

    Code duplication is Bad (tm) So what can be done about this? Any solutions that jump to mind? I suppose I need a file that stores custom adjustments to a WP template that exists independently of the theme structure, but that each theme can call on at will.

    That sounds great, but I wouldn't know where to begin, my knowledge of the WP architecture being shaky at best. Any suggestions?

  2. desk003
    Member
    Posted 9 years ago #

    I'd say making all your themes use the same CSS structure then you can call via php one central sidebar.php file.
    <?php include="http://example.com/sidebar.php" ?>

  3. dissurion
    Member
    Posted 9 years ago #

    Why not include an include 'path/interesting_addition.php'? You'll have to include this once in every theme, but afterwards it doesn't need to be updated anymore.

  4. danweasel
    Member
    Posted 9 years ago #

    Sounds good; thanks. Curse my php skills not being up to par.

  5. desk003
    Member
    Posted 9 years ago #

    Not a problem. :-)

  6. danweasel
    Member
    Posted 9 years ago #

    Hmm...

    Of course, that would remove the ability to edit sidebar.php from within the Presentation page of the WP admin, right?

  7. desk003
    Member
    Posted 9 years ago #

    Yep it would. I don't use that though, I like having two copies of my pages, one on my hdd, and one on my web server. I always FTP everything.

  8. dissurion
    Member
    Posted 9 years ago #

    You could try keeping the sidebar in only one theme dir, and having the others include it, pointing it to that sole location. That way, whatever theme your on, you can go to the theme manager and select the theme with the sidebar in it's directory. Changes will apply to all themes.

  9. desk003
    Member
    Posted 9 years ago #

    However, you could do this.

    make a new theme folder in ~/wp-content/themes/, and put your static pages in there, then call them
    <?php include "http://example.com/wp-content/
    themes/static/sidebar.php" ?>

    -desk003

  10. desk003
    Member
    Posted 9 years ago #

    We said the same thing, however I did not know because I was typing it when you posted and you were typing it when I was reading his reply. lmao. :-P

  11. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    If you want different themes to share one set of templates, you can have stylesheet-only themes inherit templates from a base theme.

    Let's say you want to create a theme that inherits templates from the default theme. This theme would contain only a style.css and perhaps an alternate set of images. To have this stylesheet-only theme pick up templates from the default theme, include the following in the theme header in style.css:

    Template: default

    This tells the theme system to use the templates provided in the wp-content/themes/default directory.

  12. Joshua Sigar
    Member
    Posted 9 years ago #

    Too good to be true, Ryan. You're awesome.

  13. danweasel
    Member
    Posted 9 years ago #

    So...if I killed everything from the default theme directory except sidebar.php, I could then use the get_sidebar() function in other themes, leave sidebar.php out of them, and they would pick up (by something like inheritance?) the sidebar.php from the default template?

    Do I have that right?

  14. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    You can't pick and choose certain files to inherit, you must inherit a full set of templates. It's an all or nothing deal. For awhile we did allow individual inheritance, but it confused the hell out of people and was a bit too heavyweight during page load since the code had to go looking in multiple locations in order to put a theme together. That's why I call themes that inherit templates from other themes "stylesheet only" themes. They don't have any templates of their own.

  15. danweasel
    Member
    Posted 9 years ago #

    So if all I want to keep constant throughout different themes is one page (sidebar.php), which option is better: delete everything from the default directory except my version of sidebar.php (basically hacking default so that it is no longer a theme, but more an "abstract class" in Java terminology) or explicitly including sidebar.php from a newly created "static" theme as desk003 suggests?

  16. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    By the way, template loading is fully pluggable. There are filter hooks called category_template, single_template, page_template, etc. Through a plugin, template loading can be completely customized. You can implement full inheritance or even pull templates out of the database.

  17. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    I wouldn't change anything in the default theme directory since that directory can be overwritten during upgrade. We encourage everyone to make a copy of the default theme in another theme directory and edit things there. That point aside, I'd probably go with the suggestion of desk003.

  18. danweasel
    Member
    Posted 9 years ago #

    If I were more comfortable with plugin design, I might go that route; try to design something to this (and other) purposes. Any thoughts on a place to go to get introed to working with plugins?

  19. ardamis
    Member
    Posted 7 years ago #

    I'd like to create a theme using less than a full set of template files. When WP needs a file that doesn't exist in the theme folder, it would fall back on either a specified theme or the default theme. In this way, my theme would inherit future updates to the default theme for any files that it didn't contain.

    Has anyone come up with a way to implement this? The stylesheet-only method isn't quite enough, but the full-template-files method is more than I need or want.

    The stylesheet-or-everything approach also means that most people are going to have unneccessarily outdated themes.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.