4

How do I listen for and receive a third party HTTP POST in Wordpress so I can process the data in the post inside Wordpress?

Not looking for the answer with code but rather the method by which Wordpress would receive that HTTP POST.

6
  • Oh I think you can. Somebody here knows how :)
    – James
    Jan 22 '17 at 7:42
  • If you think that is possible, than edit the question and add the relevant references Jan 22 '17 at 7:48
  • 1
    Let's see if others answer okay? I already use a plugin that takes PayPal's instant payment notification into Wordpress and lets me do whatever I want with that data. So I know it can be done. I'm just looking for positive direction here. As my dad used to say: "Can't never could. Can always can."
    – James
    Jan 22 '17 at 7:56
  • hmm I think I misunderstood the question.... but none of the answers is actually the right way to do it for wordpress 4.4 and above Jan 22 '17 at 10:37
  • So what IS the right way?
    – James
    Jan 22 '17 at 10:38
4

Easy! You have a couple of options to do so.

The easiest one, but less safe, is to create a brand new PHP file in the root folder of WordPress. Let's say we will call it get_post.php and add WordPress functionality to it.

Like so:

<?php 
    require_once('wp-load.php'); // add wordpress functionality
    $post = $_POST;

    if ( $something ) // security check
    update_post_meta( $post['post_id'], 'post_data', $post );
?>

The API link will be yourdomain.com/get_posts.php

Another option is to create a brand new page template inside of your template directory. And create a page using that template within your WordPress dashboard.

<?php 
    /* Template Name: Api Template */ 

    if ( ! defined( ‘ABSPATH’ ) ) {
        exit;
    }

    $post = $_POST;

    update_post_meta( $post['post_id'], 'post_data', $post );
?>

The API link will be: yourdomain.com/newly-created-page-permalink.

1
  • 1
    I believe this needs sanitization and might be vulnerable? Jun 29 '20 at 22:32
3

Since 4.4 and above wordpress has an official end point for "API" type of access - wp-json, which you can extend by defining you own extension point and handler with register_rest_route, never tried to send a non json payload that way, but I assume it should be possible, or your front end can easily encode it as json.

The advantage over other answers here is that it lets wordpress know that the request is an API request and not a front end one.

(in older versions you could have used rewrite rules... but it is too messy to even mention)

1
1

You should start by making a new page template starting from a template you already have in the theme (edit a template, change the name of the template on the top of the page, save as a different PHP file).

Then add the PHP code to process the HTTP post to the new file.

The last step is to go to the dashboard and create a new page using the new template.

2
  • I'm digesting what you've written here...the new page then "listens" for the HTTP POST from outside Wordpress?
    – James
    Jan 22 '17 at 8:06
  • You should send the Http post from the third party website to the new page you have created and handle the values inside the new php file created. The http post should not be sent to the php file but to the url of the page created in the dashboard
    – Cdorob
    Jan 22 '17 at 8:19
1

I would like to give an example to @Mark Kaplun's answer. You can register an endpoint to process your http post requests using register_rest_route like this:-

/** first create a function for processing your request*/
function process_my_request(WP_REST_Request $request){
   $request_body = $request->get_body_params();
   if(update_post_meta( $request_body['post_id'], 'post_data', $request_body )){
      $response = new WP_REST_Response(array('message'=>'Successful'));
      $response->set_status(200);
      return $response;
   }
   else{
        return new WP_Error('invalid_request', 'Something went wrong', array('status'=>403));
   }
}

/** then create a register route callback function */
function register_my_route(){
   register_rest_route('myplugin/v1', '/myroute', array(
        'methods'=>'POST',
        'callback'=>'process_my_request'
   ));
}
add_action('rest_api_init', 'register_my_route');

Once you have done that, now you can post your requests to the following path

site_url('wp-json/myplugin/v1/myroute');

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.