0

My plugin needs to make a POST request to another file within the plugin that will receive the request and store some of its data as a new post in wordpress.

The idea is that different sites that use this plugin as admin will be able to 'receive' posts via this file that will handle POST requests from the 'main' site.

All good but when I make a POST request with some data to a particular receiver php file, this file is not 'aware' that it's part of the plugin hence I can't use the data there within the plugin.

How do I handle this? Thanks

  • It's usually easiest to post to the same file that's making the call. So at the beginning of your PHP file, check if $_POST['yourvariable'] is empty. If it's empty, display the content that will post. If it's not empty, process the value. – WebElaine Jun 1 '17 at 14:06
1

You can't load plugin files directly, you need to target either a front-end or admin URL that bootstraps WordPress. An easy way to do this is the admin_post_(action) action:

add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
function prefix_admin_add_foobar() {
    // your code here
}

You can then POST your data to admin_url( 'admin-post.php' ) and set action to add_foobar in this example, which will trigger your hooked function.

  • Actually some plugins load WordPress directly (such as like-dislike-counter-for-posts-pages-and-comments) which uses direct access to plugin's php file to update the counter. But i assume it's not a good practice? – Jack Johansson Jun 1 '17 at 17:07
  • 1
    I assume you mean including wp-load.php? That is not good practice and any plugin that does that will be rejected from WP plugin repository. – Milo Jun 1 '17 at 17:33
  • Yeap. That's exactly what does it do. Also, yes, it doesn't exist on WP plugin repository. – Jack Johansson Jun 1 '17 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.