Results 1 to 8 of 8

Thread: Creating page templates via a plugin?

  1. #1
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default Creating page templates via a plugin?

    I've been trying not nut out how to add new page templates to WordPress via a plugin (instead of hard coding template files).

    Any idea if this is possible? I've been looking into get_page_templates() but I can't figure out a way to filter it.

    It's simple enough to check for the existence of a page template via is is_page_template() but creating one seems more complex.



    Wondering why I need to know? ... I want to do this, so that I can give the end-user the ability to change the label of the page template via the admin panel. At the moment this is controlled via the file itself, so if I can create the templates via a plugin (actually via functions.php - but it's easier to explain this way), then it should be possible to bypass the file and create it dynamically.

  2. #2
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Well, you could manually screw around with the $wp_themes global. That'd do it.

    Or you could just filter page_template, if you have some way of detecting when they need the page.

  3. #3
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    @Ryan, I've thought about doing this in the past but I have always talked myself out of it. I could never quite figure out how to determine the html structure that the theme is dependent on + the names of the id's and classes that it has defined.

  4. #4
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Yeah, plugins acting as themes is tricky and weird, at best. It's hard to do and I don't see any way to do it correctly.

    You can get the 80% case, but that remaining 20% is going to be a PITA.

  5. #5
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    Thanks for the input.

    @Otto - your suggestion looks like it would work, but it also looks like something that could break if they change the way that function works in the future. I'm a big fan of making stuff forwards compatible and jumping through hoops to do something like that isn't what I have in mind ... particularly if it means I need to periodically fix it.

    @mfields - for the particular theme I'm working on that isn't a problem as the themes HTML is stored in the database anyway.

    I guess I'll just stick with making the files and using generic names instead. It will work, it's just not as elegant as what I had in mind.

    I usually think of plugins and themes as being one in the same, just with a different name, but this is definitely an example of something which is not really doable in a convenient fashion via a plugin.

  6. #6
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    for the particular theme I'm working on that isn't a problem as the themes HTML is stored in the database anyway.
    Interesting. Is it a publicly available theme? If so, I would love to take a look at it and see how it works.

    I usually think of plugins and themes as being one in the same, just with a different name...
    IMO Themes are much more powerful than plugins for situations such as this. I'm currently learning the hard way how to create a plugin that creates dynamic template files. I've found a solution, but it's not pretty :)

  7. #7
    Ryan's Avatar
    Ryan is offline WordPress Legend
    Join Date
    Jan 2009
    Location
    New Zealand
    Posts
    2,797

    Default

    Quote Originally Posted by mfields View Post
    Interesting. Is it a publicly available theme? If so, I would love to take a look at it and see how it works.
    No. But I'll PM you a link so you can see how it works.

  8. #8
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    594

    Default

    Ryan have you checked Elastic? It sort of replaces the built in theme engine with its own if I remember correctly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •