Did you know that WordPress.org has a JSON API that you can use to query and get data?

Here's a video explanation

 

Well, we’re in 21st century (I think) so it’s normal and that's good.

As always, let's start with why.

What do you do with the data though?

In my case I needed it for auto suggest. One of my businesses is qSandbox. It’s a WordPress staging platform which allows people to set up a WordPress site or import (paid plan feature) an existing one in seconds. In one of the screens there’s a section where people can install plugins and themes. The installation can be done either via the plugin (https://wordpress.org/plugins/orbisius-support-tickets) or theme’s link (https://wordpress.org/themes/oceanwp)  OR a link to a .zip file.

I wanted to be super easy for my customers. They needed just type and get suggestions to pop up rather than having to find and paste links. Not everybody is that organized. Saving a few seconds here and there can add up and keep you focused.

Demo usage of WordPress.org API p4477

In the first implementation I did it so inefficiently but was working. Kind of. The search was bad as it only searched by a link (in WP terms slug). To get to the list I had to parse the SVN repository for plugins and themes in order to get  to plugin or theme’s links. I've even created a crawler that would fetch all the readme files of each plugin. It's so time consuming and wastes resources both mine and those of WordPress.org.

I am glad WordPress.org has an API and the implementation turned our pretty nice.

Oh, if you’re doing implementation within the WordPress context you can check how WordPress itself uses the its API.

Try searching within WordPress’s code for plugins_api() and themes_api() functions.

In my cases I only needed a small number of fields to be returned by the API. Whoever worked on the WordPress.org API realized something cool and it is not everybody will need all the fields. So they have a fields parameter which you can use to specify which fields you’d like returned by the API. This results in faster queries and the response because less data is going to be returned and transferred across networks. Smart. I am going to use this idea in one of my next REST API services.

Code

You can find the most up-to-date version of the code on this link.

https://github.com/orbisius/sample_wporg_api_client

 

<?php
/**
 * This code shows how to fetch data using WordPress.org API to search for plugins and phemes
 * Blog post link: https://orbisius.com/p4477
 * @author Svetoslav Marinov (SLAVI) | https://orbisius.com
 * @copyright (c) All rights reserved.
 * @license GPL
 */

$api_params = [
	'user_agent' => 'Orbisius_wporg_API_tutorial/1.0',
	'request[search]' => 'orbisius', /// <<== searched keyword
	'request[page]' => 1,
	'request[per_page]' => 8,

	// This is a great idea to only fetch fields that are needed.
	'request[fields]' => [
		'name' => true,
		'author' => true,
		'slug' => true,
		'downloadlink' => true,

		// we don't care about these at all so we want less data for faster transfer
		'rating' => false,
		'ratings' => false,
		'downloaded' => false,
		'description' => false,
		'active_installs' => false,
		'short_description' => false,
		'donate_link' => false,
		'tags' => false,
		'sections' => false,
		'homepage' => true,
		'added' => false,
		'last_updated' => false,
		'compatibility' => false,
		'tested' => false,
		'requires' => false,
		'versions' => false,
		'support_threads' => false,
		'support_threads_resolved' => false,
	],
];

/////////////////////////////////////////////////////////////////////////
// Fetching plugins
$plugin_search_api_url = 'http://api.wordpress.org/plugins/info/1.1/?action=query_plugins';
$api_url = $plugin_search_api_url;
$packaged_params = $api_params;
$packaged_params['request']['search'] = 'orbisius';
$packaged_params = http_build_query($packaged_params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $packaged_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // hmm?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // hmm?
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$content_json_maybe = curl_exec($ch);
$error = curl_error($ch);

if ( !empty( $error ) ) {
	echo "Error: $error\n";
	$result = curl_getinfo($ch);
} else {
	$result = json_decode( $content_json_maybe, true );
}

var_dump($result);

curl_close($ch);
/////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////
// Fetching themes
$theme_search_api_url = 'http://api.wordpress.org/themes/info/1.1/?action=query_themes';
$api_url = $theme_search_api_url;
$packaged_params = $api_params;
$packaged_params['request']['search'] = 'ocean';
$packaged_params = http_build_query($packaged_params);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $packaged_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // hmm?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // hmm?
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$content_json_maybe = curl_exec($ch);
$error = curl_error($ch);

if ( !empty( $error ) ) {
	echo "Error: $error\n";
	$result = curl_getinfo($ch);
} else {
	$result = json_decode( $content_json_maybe, true );
}

var_dump($result);

curl_close($ch);
/////////////////////////////////////////////////////////////////////////


exit(0);

 

The result

This is what the API would return based on the keyword search output of the API

API output

 

How do you use WordPress.org API? What would you use for?

 

Related