I feel like I am missing something simple here, but can't figure it out
I need to have my plugin build links to a page that is built entirely via a cURL response.
I am building a plugin that interfaces with an external API to pull data and build a dynamic list (of jobs) that is displayed via shortcode. Each list item needs to link to the job advertisement. Through this API, these advertisements are provided (via another cURL) as completed HTML.
For each cURL request, a client_id is required which is stored in the wordpress database. The other thing required is a simple ID referring to the job. I originally created a PHP file that had the cURL as a function and pulled the ID from a query string.
The link was built with like this:
$_ad_link = plugins_url( 'includes/ad.php' , dirname(__FILE__) );
But the cURL depends on access to the database in order to pull the client_id (serves as the password). This file doesn't seemed to be recognized as a wordpress file and so does not know what to do with get_option
in that case.
I am not supposed to pass the client_id in any way through the browser via javascript or anything like that. Else I would simply include it in the query string or use AJAX (though I have no experience in AJAX, such as if there is a way to give it the client_id securely).
Is there any way to include this file within Wordpress in some way and link to it properly as I hope to do? Or is there another way to take the cURL response (a full HTML page) and display is from a click on a list element?
The flow should be:
- User views list of jobs (on the front-end of the site).
- User clicks on a job (link).
- User sees job advertisement (a full HTML file returned from a cURL request).
I have a client_id that I need from the wp database and the id of the job (that I can pass through the link). I would like to display the job advertisement as its own page, rather than fooling with an iframe or something of that sort.
EDIT:
As mentioned in the comments below, I would ideally like to output the cURL response in a new "page" that has only that content.
I originally tried to do this by creating a page that had only the cURL request as a function that then output the response to the page. The job-list items would then link to this page which would call the cURL. I need that page (the cURL request) to have access to the wp database, however, and function properly within wordpress. As it was, it would error with get_option. (I am pulling the right id for the job from a query string)
$job_id = $_SERVER["QUERY_STRING"];
parse_str($job_id);
if(isset($jobid)) {
build_jobad();
}
function build_jobad() {
global $jobid;
$sg_ad_url = 'curl-url-goes-here';
$cm = curl_init($curl_url);
$options = get_option('plugin_options');
$clientid = $options['client_id'];
curl_setopt($cm, CURLOPT_USERPWD, $clientid);
curl_setopt($cm, CURLOPT_HTTPHEADER, Array('Content-Type: application/json;charset=utf-8'));
$jobs_ad_output = curl_exec($cm);
curl_close($cm);
}
Is there someway to do it this way? Is there something I am missing to allow this php page to function within wordpress? I assumed it would not work unless it were a template page, which I do not want to create. The cURL response needs to be the only html output on the page.