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.
You just need to include the file wp-load. php in your external file. The wp-load. php file is located in root of your WordPress installation.
If you want to use $wpdb in an external PHP file inside/outside WordPress, include the wp-blog-header. php file first. In the following example code, the $wpdb is accessed from a PHP file which is placed in the WordPress theme directory. require_once('../../../wp-blog-header.
To make it useful, you need to add your custom function to the PHP file and then upload the folder to the plugin directory of your WordPress site, usually wp-content/plugins/. If you need to add new WordPress functions, you can simply overwrite the old version with your changes.
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_).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With