WordPress.org

Ready to get started?Download WordPress

Forums

wp-mpdf
[resolved] Custom page templates not being used (8 posts)

  1. Jason Judge
    Member
    Posted 11 months ago #

    I have a child theme with a custom page template, and have selected that custom page template for the page that I want to provide a PDF version of.

    What I am finding is that the custom page template is not being used at all.

    What I would like to be able to do, is to render the page in the PDF document, using the template selected specifically for that page, in the child theme.

    Is there a way to get this working, or do I need to copy the entire contents of my custom template into default.php?

    http://wordpress.org/extend/plugins/wp-mpdf/

  2. Jason Judge
    Member
    Posted 11 months ago #

    Of course, WP simply does not work like that! A page template includes (by default, with a plugin to handle it differently) side bars, header, footer etc. which no decent framework would ever do, sigh).

    So I need to take the central body of my page template, put it into a separate include file, then include it from default.php after doing a few checks to ensure it is appropriate.

    In short: create my own extension to default.php to do the equivalent to the custom page template.

  3. Jason Judge
    Member
    Posted 11 months ago #

    What would be nice is if the mpdf template area acted like a mirror of the standard site theme, so if I selected template 'custom-page-x' for a page, showing it as a PDF document would look for 'custom-page-x.php' in the mpdf template area first and use that to render the content (but again, *just* the content, and not the whole wrapper around it with headers and footers etc.

    Themes in WP are just not structured enough, not layered in a way that makes building pages easy, and I realise that does make this kind of flexibility an awful lot more difficult to implement than it ever should be. But that's a rant for another day ;-)

  4. fkrauthan
    Member
    Plugin Author

    Posted 11 months ago #

    Hi,

    I am not completly sure what you mean. In general the pdf templates have nothing todo with the wordpres site templates. And there is no way to use the normal site templates.

    What I can offer is adding a method to select a custom pdf template on a per page base. So you can create a second pdf template and select that for special posts for example. Would that help?

    Thanks,
    Florian

  5. Jason Judge
    Member
    Posted 11 months ago #

    Let me think it through and see what I can come up with. I realised as I typed that the PDF generator could not use the standard custom WP templates. It is a shame really, and is the fault of WP - the way WP has grown over time makes some things much more difficult and cumbersome.

    However, I can create a *core* custom template script that just delivers the *content* of my custom page. That script can then be used by both the PDF template and the standard WP theme (I hate code duplication - I like to write things once ;-)

    What I was wondering, is if there was some way to handle custom templates in a better way, so the PDF generator could see that a page uses a different template in the normal WP theme and so could see if there is a similarly-named template in the PDF theme and use that.

    What I am using my custom templates for is to generate content - not a list of pages or a single page with a short-code in - but real unique content.

    Now, just writing that, I wonder if my best approach will be to pull all my custom content-generating code out of the custom template, and put it into a short-code instead? That way my content would appear within the standard PDF default page rendering. I'll try that. Thanks for listening :-)

    For interest, this is the page:

    http://www.millhillsupplies.co.uk/catalogue/

    All the tabulated data here comes from a custom post type, and is divided into a series of tables according to a custom taxonomy. With ALL of that simply hidden behind a short code, it should work like a trooper with your PDF plugin.

  6. fkrauthan
    Member
    Plugin Author

    Posted 11 months ago #

    I think the best solution for your idea would be if you create a custom pdf template (A pdf template is just a standard php file which has to define some php global variables). And within that template instead of filling it with html use your custome code to load the shared template code and process it. This way you have a clean way of implementing your template system with the pdf plugin without any hacks.

  7. Jason Judge
    Member
    Posted 11 months ago #

    I went for the shortcode approach, and it works really well.

    So everything that was in my custom template in the child theme, has been moved out to functions.php and implemented as a shortcode. So now I can display a price list for any section of the site:

    Lathes:
    [pricelist sections="lathes,lathe-packages"]
    Mills:
    [pricelist sections="vertical-milling-machines,mill-packages"]
    etc.

    Or list list all the sections:

    [pricelist]

    The shortcode automatically puts in a TOC if there is more than one section. It would be nice to get the links in the TOC working in the PDF document too.

    This is it in operation (it needs a little tidying up, but works):

    http://www.millhillsupplies.co.uk/catalogue/

    This is actually much more flexible than the original page template. It also allows me to use exactly the same content-generating code for both the HTML theme and the PDF document. In addition, I can extend the short code with display options, and other categories to select different useful sets of products (e.g. all products that would be of use to clock-makers).

    I can provide some code to show how this works if needed, but essentially, this is just an ordinary shortcode, and your plugin handles it like a charm.

  8. Jason Judge
    Member
    Posted 11 months ago #

    I'll withdraw my original question, as the shortcode is a nice workaround.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.