5

How can I allow a standalone page to access WordPress functions without including/requiring wp_load or wp_config?

I am developing a plugin and need to allow access to the WP database on a standalone php page. This page is a simply curl request that returns a full HTML page to display. For context, the plugin is a list of jobs from an API that then need to link to the full job ad (a full HTML page).

The code I currently have is this (in a jobad.php page):

$root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
if (file_exists($root.'/wp-load.php')) {
  require_once($root.'/wp-load.php');
}

This is the code that I pulled from every answer I have found on how to accomplish this. Including from wordpress.org's own forums. From this stackoverflow answer, for example: Using WPDB in standalone script?

It works fine, but when I tried to submit the plugin to the WP directory, the plugin was rejected for including wp_load.php in this way. It makes sense why not to, but I cannot find any other way to make the file work within WordPress.

The outcome I need

From a list generated by a shortcode, each item has a link that will return a full HTML page. This HTML page is returned as a cURL response--not a URL (or I could just have each link drive an iframe). For context, I am building the link this way

$job_ad_link = plugins_url( 'includes/jobad.php' , dirname(__FILE__) );
$job_id = //id from database;
<a href="'.$job_ad_link.'?sgjobid='.$job_id.'">link</a>

Thus calling jobad.php will run the cURL function for the correct job_id and display the cURL response. I can run the cURL as AJAX and avoid this problem, but because it is a full HTML page, I cannot simply return the cURL in a div. In an iframe is awkward and iffy and I would rather not use it (and have had no success in trying).

For clarification, the response from WordPress:

Including wp-config.php, wp-blog-header.php, wp-load.php, or pretty much any other WordPress > core file that you have to call directly via an include is not a good idea and we cannot > approve a plugin that does so unless it has a very good reason to load the file(s). It is > prone to failure since not all WordPress installs have the exact same file structure.

Community
  • 1
  • 1
robooneus
  • 1,344
  • 8
  • 10

3 Answers3

7

I implemented a couple of plugins that provides AJAX responses and so I also needed to be able access WordPress functions from a PHP script. I solved this including the wp_config.php file until a core developer pointed out, how to solve it correctly.

As this way is not limited to AJAX calls, I also use to render a page for a Facebook App, that need to have access to WordPress functions. So here is how you do it (just put this into your functions.php or into a small plugin):

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

    // do what ever you want with the ability to access WordPress functions.
    // asuming that the one item is an option, just get it with get_option
    $value_from_db = get_option( 'curl_value' );
    // asuming that the value is in any other table, use some $wpdb function
    $value_from_db =  $wpdb->get_var( 'SELECT value FROM table' );
    // include the curl file echoing the response
    include( plugin_dir_path( __FILE__ ) . 'curl-script.php' );

    die(); // this is required to return a proper result
}
add_action( 'wp_ajax_full_job_ad_page', 'full_job_ad_page' );
add_action( 'wp_ajax_nopriv_full_job_ad_page', 'full_job_ad_page' );

Than you can use such a URL to get the response from the function:

http://example.com/wp-admin/admin-ajax.php?action=full_job_ad_page

Note: The action in the URL has to match the end of first parameter of the action hooks (the string after wp_ajax_ and wp_ajax_nopriv_).

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
  • As this is a plugin, I would simply include this in the main plugin file, correct? The only thing I need access to is one item from the database to pass to the cURL script. Would the entire cURL script then go here, or in the separate PHP file? And, if there, then how do I return the result within the PHP file? Sorry, still not seeing how the two connect. – robooneus Jul 19 '13 at 10:23
  • I've changed the code a bit and added a exmple URL to call this function. The function can be included into your mail plugin file. – 2ndkauboy Jul 19 '13 at 10:35
1

I think you'll find the answer in these articles:

AJAX in Plugins

5 tips for using AJAX in Wordpress

Also look at this question on WordPress Answers, where I found the links.

Community
  • 1
  • 1
Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
  • I have implemented AJAX to run the cURL before, but do not want to return the cURL request on the same page. I want the returned HTML to be shown on its own page. Running the request in AJAX on the other page doesn't give me any more access to the WP database than running it normally. – robooneus Jul 19 '13 at 10:14
  • You didn't read the articles. The answer by @Kau-Boy implements a solution as described in the links I gave you. – Hein Andre Grønnestad Jul 19 '13 at 10:16
0

how about?

<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('path_to_wp/wp-blog-header.php');
query_posts('page_id=63');
?>

<?php while (have_posts()): the_post(); ?>
  <h2><?php the_title(); ?></h2>
  <?php the_content(); ?>
<?php endwhile; ?>
Marty
  • 4,619
  • 2
  • 24
  • 35
  • That's not a good solution for the same reason as it's not recommended to import the wp_config or wp_load in any files. – 2ndkauboy Jul 19 '13 at 10:25
  • this is a standard method to include Wordpress functions in a static/standalone page! – Marty Jul 19 '13 at 10:39
  • 1
    A standard method that too many users do. But it's not recommended at all! – 2ndkauboy Jul 19 '13 at 10:54
  • WHY? Can you elaborate a little as to why its not recommended to use? – Marty Jul 22 '13 at 10:55
  • Because of (at least) reasons: 1. The saving path of the wp-config.php can vary (might be a folder above WP root directory). 2. The system will call too many hooks which are not needed to respond a AJAX call (or in this case a staic page). – 2ndkauboy Jul 22 '13 at 11:57
  • You can also read the first part of "5 tips for using AJAX in WordPress" linked in the answer from @Hein André Grønnestad. – 2ndkauboy Jul 22 '13 at 11:59