There are generally two ways to think of WordPress relationships with external APIs: theme integration and plugin integration. With a little WordPress experience, both processes are relatively simple and can be completed in a short amount of time. Here are a few things you can do every time you integrate a plugin API to make sure the process goes smoothly.
Always start with the basics
The first thing you want to do is code the basic elements of the plugin. This forms the framework for the rest of the API on which you build the functionality you want. Before doing this, however, make sure that you (of course) have a WordPress site and an API account. Both are easy and free, though WordPress has paid options.
When you’re done, rename the file (e.g. “new-plugin.php”) and move it to your WordPress installation folder: wp-content> plugins. Once that’s done, you can activate it by going to your WP administrator dashboard and clicking on Plugins. You are now reading to continue with the configuration and get your API key for the API you have created.
Configure and test your API with the Postman app
Consult the documentation for information on the API you want to integrate. In the “Request Formats” section you can choose between XML-RPC, SOAP and REST. Choose REST. Once you click on it, you will see an example of the REST API endpoint of the selected plugin.
The REST-API of the plugin you have chosen allows you to publish, retrieve and delete the data you have chosen. Copy the sample endpoint provided and paste it into Postman. Make sure your request type is “GET”.
Using the Postman app is not a mandatory step in this process. However, it makes it easier to connect and test your API as you proceed, and may make it more intuitive for beginners. The best part is: it’s free. If you don’t like it then you don’t have to use it.
Find your API key
To create an API key, an API account must be registered. Once you have that, you can request an API key. Many API providers have various terms and conditions for the use of their API, including restricted access and commercial or non-commercial use.
Once you’ve initiated your API request, in no time you should receive your key (which indicates it is you) and a secret personal code that you can use to prove you are who you say you are. You are now ready to begin your API innovation journey.
Define your requirement parameters
The next step in the integration process is choosing your method of requesting data. The API documentation often offers a variety of options to choose from. Sometimes certain methods require authentication.
It is a good idea to set your data format to “Auto” so that Postman can choose which data format to use. Postman offers a number of different format options including JSON, XML, text, and HTML.
Finally, create and integrate your widget
Paste the widget code into your plugin’s primary PHP file. In your WP dashboard, go to Appearance> Widgets and add the widget you want to the widget area you want. If you look at it from the front end you will see the title but nothing specific yet.
Go back to your PHP file and configure the widget output. At this point, you have everything you need – the method, format, and API key. Here you can synthesize your final REST API endpoint URL using the multiple arguments you’ve gathered.
The API integration in WordPress takes some getting used to, but once you master the steps you can build what you need and be up and running in no time. Familiarize yourself with the above process and enjoy almost endless customization in WordPress.