Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active December 14, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/23b15c76f0d31dd17f8f236e9e8cda0b to your computer and use it in GitHub Desktop.
Save Shelob9/23b15c76f0d31dd17f8f236e9e8cda0b to your computer and use it in GitHub Desktop.
Using a WordPress REST API route to proxy request to thrid party API, so api key isn't needed in client https://twitter.com/UpTheIrons1978/status/1602938132244094976
$.post( url_of_wp_endpoint, {email: 'roy@roysivan.com' } );
<?php
/**
* Endpoint for adding contacts to CRM
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'whatever/v1', '/add-to-crm', array(
'methods' => 'POST',
'callback' => function($request){
$resoponse = wp_remote_post( $url_for_crm, [
'body' => [
//Pass auth token defined in a constant in wp-config
'token' => AUTH_TOKEN,
'email' => $request['email']
]
]);
//Return a response...
},
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment