Question

Turns out I'm trying to integrate witei's CRM into a website. According to the witei documentation, I must indicate a url (webhook), where I want to receive the information of the properties that has changed, so that I can keep the properties of my website updated.

The information is sent through the POST method and the service works because it returns the data in JSON format, testing through the https://beeceptor.com/ service (which generates a temporary endpoint).

The fact is that according to what I have read (or I understood), I have to create an endpoint in my Wordpress, following the steps indicated in the wordpress documentation: https://developer.wordpress.org/rest- api/extending-the-rest-api/adding-custom-endpoints/

My question is: Am I in the right direction? If so ... With my endpoint created, do I have to create any extra file? or just including the function 'register_rest_route', dont need to make anything else? I have tried following instructions but I always get error 404.

enter image description here

The code involved for that is:

//Testing webhook
add_action( 'rest_api_init', function () {
  register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    'methods'  => 'POST',
    'callback' => 'my_awesome_func',
  ) );
} );

function my_awesome_func( $request ) {
     $data = $request->get_json_params();
     error_log( print_r( $data, true ) );
}

And in Witei CRM I get this result:

enter image description here

Can anyone help me with this? or just drive me in the right direction? Thank you!

Was it helpful?

Solution

Am I in the right direction?

Yes, and I'd also use the REST API if I were you.

I have tried following instructions but I always get error 404.

That is actually normal if the request method is not POST — e.g. using the browser to manually access/visit the endpoint at example.com/wp-json/real-estate-lite/v1/endpoint whereby the (HTTP) request method is (or defaults to) GET.

And why it's normal is because the methods of your endpoint is set to POST only (i.e. 'methods' => 'POST'), hence all other request methods like GET are not allowed and therefore WordPress throws the error 404 when the endpoint is accessed through GET or another disallowed (HTTP) method.

So if for example you want GET method to be allowed, then set the methods to an array like so:

register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    'methods'  => [ 'POST', 'GET' ], // allowed request methods
    'callback' => 'my_awesome_func',
) );

/* The above code is equivalent to:
register_rest_route( 'real-estate-lite/v1', '/endpoint', array(
    // Endpoint 1, for POST request method.
    array(
        'methods'  => 'POST',
        'callback' => 'my_awesome_func',
    ),

    // Endpoint 2, for GET request method.
    array(
        'methods'  => 'GET',
        'callback' => 'my_awesome_func', // * the same callback
    ),
) );
*/

But of course, you wouldn't want to do that in your case since the webhook is using the POST method to connect to your custom WordPress REST API endpoint. For testing purposes though, you can temporarily allow GET requests (and then just go to the endpoint using your browser).

And as you can see in the example, we can have two or more endpoints at a route, and each request method should have its own callback.

BTW, sorry for not really understanding your issue/question earlier. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top