Gravity PDF: Including a php file int he PDF template? [RESOLVED]

I’m using Gravity PDF to print a form and on the form the user selects a county. I take this selection and match it to an array of addresses and print the right address in the PDF template. I need this array outside of the template because it is needed by other forms and PDF templates. I’m trying to include this php file in my PDF template but it won’t include. I’m using the standard:

require content_url() . "/uploads/PDF_EXTENDED_TEMPLATES/address.php";

This doesn’t seem to be working but this is the correct file location. Is there a different way to include files in Gravity PDF that is a required format of something?

Have you checked with Gravity PDF support?

FWIW - I solved a similar use case by using GF HTML field containing the needed HTML markup plus a merge tag. Using the hook for merge tags, I wrote some PHP that would lookup the choice value, and swap out the merge tag with the matching value.

This would take care of your own use case if I am following it correctly. There’s prolly other ways to do what you are attempting, but would be as Chris says, a support question to that plugin’s vendor.

GL Avi.

TL;DR… Try this:

include __DIR__."/address.php";

Long winded but you have options:

The issue you’re running into is that content_url() returns http://www.example.com/wp-content which isn’t what you need to include PHP files. You have to provide an absolute path.

Since these functions are being used sitewide your best approach is to do a require_once or an include_once in the theme or a plugin. This way you don’t have to include those functions everytime you need to use them, they’ll just always be there at the ready.

A few different ways to handle this:

1: Create your own custom plugin with the functions that you need using the standard plugin protocol.

https://developer.wordpress.org/plugins/intro.

*This method is the most flexible as you can add it to other projects, but will require more setup time.

2: Add the functions to your theme and include them like this:

include_once get_stylesheet_directory().'/address.php';.

*This is pretty simple and allows you to access those functions sitewide.

3: I use a function in our themes functions.php file that loops through and auto includes all of the files in the lib directory.

foreach (glob(__DIR__ . '/lib/*.php' ) as $file ) {
   include $file;
}

*I really like this approach for my team because it keeps our functions.php file clean and all of the functions will “auto” include themselves when we create the new file. If you just have one file to include than just simplify that to:
include __DIR__."/address.php";

2 Likes

Thanks everyone. I got it working with your suggestions of using DIR instead. Also, Nick, thanks for the extra suggestions and I think I’m going to implement #3 because it makes a lot of sense.

As far as the content_URL(), I will do some more research because I still don’t understand why it didn’t actually work. DIR returns the exact directory so I just need “/address.php” but using content, I was also adding “/uploads/PDFS/address.php” which would complete the file path. Again, more for me to look into but it works well now and I thank you guys for that.

1 Like

content_url() and DIR are not the same at all.

Assuming we have a file in /wp-content and we were to echo content_url() and DIR.

This is how they would read: (using my local enviroment as the example)

content_url() => https://localhost.local/wp-content
_DIR_         => /Users/nickhempsey/Sites/localhost.local/wp-content

Notice that content_url is a URL and DIR is an absolute path on the server.

Hope that clears it up.

2 Likes