Call PHP file within a plugin to activate function

So, the following might be absolutely against WordPress standards & best practices, but I’m in the process of learning.

Currently, I am calling a PHP file to generate a PDF through a third party.

The way I call the file is by visiting: https://example.com/wp-content/plugins/eventix-woocommerce/generate.php?order_id=1553&key=d5df1b20-15a9-11e9-ad15-c704673daa98

The code for generate.php:

require_once(“../../../wp-load.php”);
nocache_headers();

$orderId = $_GET[‘order_id’];
$key = $_GET[‘key’];

$eventixOrderId = get_post_meta( $orderId, ‘eventix_order_id’, true );

if($eventixOrderId != $key){
echo “These are not the tickets you’re looking for.”;
exit;
}

$accessToken = get_option(‘eventix_access_token’);
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CUSTOMREQUEST => “GET”,
CURLOPT_URL => ‘https://api.eventix.io/3.0.0/order/’.$eventixOrderId.’/downloadlink’,
CURLOPT_USERAGENT => ‘example.com’,
CURLOPT_HTTPHEADER => array(
‘Content-Type: application/json’,
‘Authorization: ‘ . $accessToken)
));

$ticketPdf = curl_exec($curl);
curl_close($curl);
update_post_meta( $orderId, ‘eventix_pdf_ticket’, $ticketPdf );
header(“Location: $ticketPdf”);
exit;

Now, for the most part, this actually works excellent. But in a few cases this script messes up, throws me to the 404 page of example.com and doesn’t load the PDF. On the other hand, if I visit the link incognito; no problems.

My first thought was that it might be caused by header cache. I tried numerous ways to clear header cache, but didn’t work.

Read more here:: Call PHP file within a plugin to activate function

Leave a Reply

Your email address will not be published. Required fields are marked *