Using the Envato API with WordPress

Today we are going to discuss how to use the Envato API in WordPress and create a WordPress shortcode that promotes our Envato Marketplace Items inside our WordPress site. We will combine the powerful Envato API, WordPress' flexibility and a little bit of creativity, to build an amazing plugin for our site.


Let's Set Our Goal

In this tutorial we are going to focus on:

  • Some basic knowledge about the Envato API
  • How to use API result data inside WordPress
  • Build a WordPress Shortcode that promotes Envato Marketplace items in our WordPress site.

So let's get into the first one!


Step 1: Understanding the Envato API

Envato provides an API that allows developers to get some information about Envato Marketplace items, users info, popular projects and so on. All possible queries are listed in the official documentation. In this article we discuss the public API only.

The Envato Public API has the following structure.

The word set must to be replaced with an option listed in the set column of the API documentation. So if we want all information about a marketplace item we have to replace set with item:the_item_id. The final request URL will be:

You can try to insert the URL above in your web browser and see the returned data.

We can also concatenate more than one set option in a single request to get more data. For example we want the item data and its author information. So the previous URL will become:

The Envato API returns JSON, so in the next paragraph we are going to show how to manage it in WordPress.


Step 2: How to Use API Results in WordPress

In this tutorial we are not going to discuss how to create a WordPress plugin, but we are going to focus on some techniques to use the API in WordPress:

  • Send the API request
  • Manage the result data (the JSON string)

The function below fetches the data from the Envato server and returns a PHP array that contains all the informations we want.

We can improve the function above. To prevent stress on the Envato API server we can cache item data and request the info again after a timeout. WordPress offers us some functions to implement this feature. Let's add it.

Now the core function of our WordPress plugin is ready. We have used some WordPress functions that help us to save time. All information about them is explained in the official WordPress Codex.


Step 3: Build WordPress Shortcode

In the next steps we are going to code a useful WordPress plugin that allows us to display some informations about an Envato Marketplace item. All code below is well commented so you can easily understand each line. For more details about Writing a WordPress Plugin and the WordPress Shortcode API check out the online documentation in the WordPress Codex.

Let's start

Let's write the header informations for our plugin

Add the WordPress shortcode

Now we write the code to add the shortcode and its functionalities.

Star ratings function

The WPTP_add_shortcode() function above has the WPTP_get_stars() procedure that coverts the rating number to HTML stars. Let's implement it.

Include CSS

When the shortcode functions are completed, we have to include the style.css file that styles our plugin.


Step 4: Write CSS Rules

The style.css file is inside the same directory as the main plugin file and it contains all the CSS rules.


Conclusion

That's it, now we can upload the plugin to our Worpdress site and use the power of WordPress shortcodes to display some info about Envato Marketplace items. For more details about Writing a WordPress Plugin and the WordPress Shortcode API check out the online documentation on the WordPress Codex.

I'm Michele Ivani and I hope this tutorial was helpful for your WordPress development. Thanks so much for reading.

Tags:

Comments

Related Articles