Access WordPress features on a separate plugin page

How to allow a standalone page to access WordPress features without including / needing wp_load or wp_config ?

I am developing a plugin and should allow access to the WP database on a separate php page. This page is just a curl request that returns the full HTML page to display. For context, the plugin is a list of tasks from the API, which then must refer to the full job announcement (full HTML page).

The code I currently have is this (on the 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 I extracted from each answer I found on how to do this. Including in own wordpress.org forums. From this stackoverflow answer, for example: Using WPDB in a standalone script?

It works fine, but when I tried to send the plugin to the WP directory, the plugin was rejected to enable wp_load.php this way. It makes sense, why not, but I can't find another way to get the file to work in WordPress.

The result I need

From the list generated by the short code, each element has a link that will return the full HTML page. This HTML page is returned as a cURL response, not a URL (or I can simply specify that each channel bind an iframe). For context, I create a 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> 

So calling jobad.php will run the cURL function for the correct job_id and display the cURL response. I can run cURL as AJAX and avoid this problem, but since it is a full HTML page, I cannot just return cURL to a div. The iframe is awkward and uncomfortable, and I would prefer not to use it (and not succeed in trying).

For clarification, the answer is from WordPress:

Including WP-config.php, WP-blog-header.php, including-load.php, or almost any other WordPress> main file that you must call directly through the included is not a good idea, and we don’t can> approve a plugin that does this if it does not have a good reason to download the file (s). This> crash tendency, because not all WordPress installations have the same file structure.

+4
source share
3 answers

I implemented a couple of plugins that provide AJAX answers, and so I also needed to have access to WordPress features with a PHP script. I solved this, including the wp_config.php file, until the main developer indicates how to solve it correctly.

Since this method is not limited to AJAX calls, I also use to display the page for a Facebook application that needs to have access to WordPress features. So here is how you do it (just put this in your functions.php file or in 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' ); 

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

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

Note:. The action in the URL must match the end of the first parameter of the action hooks (lines after wp_ajax_ and wp_ajax_nopriv _)

+7
source

I think you will 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 links.

+1
source

What 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; ?> 
0
source

Source: https://habr.com/ru/post/1492269/


All Articles