Author: admin
Making an API Endpoint with WordPress
Most third party services in today’s day and age are required to leverage some sort of server and authentication if you want to leverage their API. Take for instance Klaviyo for email marketing. Klaviyo has a few ways outlined in their API documentation for how you can make secure calls. Usually it’s with a server side language like PHP.
So let’s say – leveraging Klaviyo – you want to ditch their out of the box solution. This solution is where you typically add a third party script tag, dump a chunk of HTML, JavaScript and CSS so that a signup form is live on your website instantly. The solution I am providing is where you skip all of the extra code that Klaviyo is rendering and just make a direct connection with some javascript and PHP of your own. Typically this ends up saving a large chunk of unused javascript, HTML and then you can just add the contact form CSS to your own CSS code base.
So first, you need to think about the main server-side connection. In this tutorial, I am going to leverage PHP. In addition, I am also going to leverage WordPress. WordPress has a handy ability to fire a function leveraging an action hook. This action is usually prefixed with ‘wp_ajax_’. I will guide you on making one and showing you how you can trigger it manually just by hitting the URL.
<pre class=”prettyprint”>
// Goes in the functions.php page of WordPress
function submitKlaviyoNewsletterSignup(){
}
add_action(‘wp_ajax_submitKlaviyoNewsletterSignup’, ‘submitKlaviyoNewsletterSignup’);
add_action(‘wp_ajax_nopriv_submitKlaviyoNewsletterSignup’, ‘submitKlaviyoNewsletterSignup’);
</pre>
Now technically, if this is live on WordPress on a domain, you can reach it by going to:
https://[your-domain]/wp-admin/admin-ajax.php?action=submitKlaviyoNewsletterSignup