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_optionin 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.


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"];
  if(isset($jobid)) {

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);

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.

  • Found this as a simpler solution than AJAX in this case. Answer provided below also works, but displaying the content was a problem. Using WPDB in standalone script – robooneus Jul 15 '13 at 14:46

To make this work with WordPress use the AJAX API. While that technically refers to Javascript it will handle any request if the appropriate values are sent.

You would create a callback like this one form the Codex:

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    global $wpdb; // this is how you get access to the database

    $whatever = intval( $_POST['whatever'] );

    $whatever += 10;

        echo $whatever;

    die(); // this is required to return a proper result

Then send a POST request with the my_action argument set. Notice how that is hook name wp_ajax_my_action minus wp_ajax_. You will need to use wp_ajax_nopriv_my_action for users that are not logged-- presumably your cURL script isn't logged in.

To display the cURL content...

  1. Create a page template to display your cURL. It should look something like:

    /* Template Name: cURL Content */
    // code for your curl data
    // no other code at all
  2. Go to the backend to wp-admin->Pages and create a page. Select the template you created as the "Template"

Done. That page should display only your cURL data. It will have no internal WordPress functionality, though, as far as script or style loading, and a number of other things, because you are not including wp_head and wp_footer.

  • So with this I would be able to trigger the function on the same page with the use of a javascript trigger on the list item -- the function would then return the HTML from the cURL script which I could then display in a modal or something on the page, correct? To be clear: this is a list on the front-end for users to browse jobs. On click of a link, the user needs to see the (full screen, full HTML) job ad. Best would be to open it in a completely new page, hence wanting to use a link to a different page dynamically created. – robooneus Jul 12 '13 at 15:47
  • Yes, this will work with any request whether that request originate from Javascript, or cURL, or from an ordinary form, so long as you send the right POST argument to trigger that callback. What it does not work with is a GET request. – s_ha_dum Jul 12 '13 at 15:52
  • Is there any way to open the results of the cURL in another page, though? Ideally I would want the job list item to open a new tab with just the results of the cURL making up the page (as I mentioned, the cURL returns a full HTML page). – robooneus Jul 15 '13 at 7:55
  • You will have to use Javascript to open a new tab, but it isn't going to be 100% reliable and I'd consider it very unfriendly. – s_ha_dum Jul 15 '13 at 13:02
  • I have successfully implemented everything up to actually displaying the results... Now I have a full HTML page (complete with its own CSS and JS) to display. Any thoughts on how best to display that to a user? Definitely would prefer not to use javascript to open a new tab... needs to be pretty flexible. – robooneus Jul 15 '13 at 13:10

Your Answer

