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:
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?
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.
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.
*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";
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.