HTTPS custom endpoint for Wordpress

I have been able to create a custom endpoint following the code below for wordpress

function handle_woocommerce_keys($request){
    $user_id = $request[user_id];
    $consumer_key=$request[consumer_key];
    $consumer_secret=$request[consumer_secret];
    $key_permissions=$request[key_permissions];

/* search user_id in db and store the keys as meta_data */


    $response = new WP_REST_Response();
    $response->set_status(200);

    return $response;
}

add_action('rest_api_init', function () {
    register_rest_route( 'village/v1', 'authkeys',array(
        'methods'  => 'POST',
        'callback' => 'handle_woocommerce_keys'
    ));
});

Unfortunatly it's only working using HTTP. I use Postman and it behave has expected. However, I need HTTPS to be supported. The reason is that the endpoint URL is provide as param to a server and used by this server to send a POST to this https URL.

Any idea how to make HTTPS endpoint supported on Wordpress ? Thanks

1 answer

  • answered 2020-10-01 07:26 Diego

    There are a few things to do to convert your WP from http to https. The suggestion is:

    1.List item (https://wp-cli.org/it/) with the replace command (https://github.com/interconnectit/Search-Replace-DB) to change all your DB occurrences from http://example.com to https://example.com. The full command should be something like:

    wp search-replace http://example.com https://example.com --precise --all-tables-with-prefix
    

    2.Edit your .htaccess inside your root dir and add this at the top:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    

    3.Edit your wp-config.php and add this (OPTIONAL):

    define('FORCE_SSL_ADMIN', true);
    

    After you did all of this you should be ready to use HTTPS, as long as you have a valid certificate :)