0

Creating WordPress Shortcodes For Envato Marketplace API

If your a regular reader here then you will know every month I display a list of Free items that you can get from the Envato marketplace.

The Envato marketplace is a set of different websites which are all dedicated to sell premium standard files on all types of files you will need for design and development.

You can get WordPress themes, code scripts, graphics, photos and much more.

In this tutorial we are going to look at how to use the API to get public information and how to turn this into a WordPress plugin to display item information on your Website. We are going to team up the API with your account name to easily earn money from the affiliate account by referring new users to the marketplace.

Envato Marketplace Sites

Currently there are nine sites which make you the Envato marketplace, all providing different things to your design or development projects.

Activeden

At ActiveDen you can buy and sell royalty-free Adobe Flash and Flex files for just a few dollars. Items are priced on the complexity, quality and use of the file. The site is home to a bustling community of Flash designers and developers and is the largest Flash library on the web.

Activeden

AudioJungle

At AudioJungle you can buy and sell royalty-free music and sound effects that are completely Podsafe for just a few dollars. The site is home to a bustling community of audio composers and producers.

Audio Jungle

Themeforest

At ThemeForest you can buy and sell HTML templates as well as themes for popular CMS products like WordPress, Joomla and Drupal with prices ranging from just $5 up to $40. Items are priced on the complexity, quality and use of the file. The site is home to a bustling community of web designers and developers and is the biggest marketplace of its kind.

Themeforest

Videohive

At VideoHive you can buy and sell royalty-free footage and motion graphics as well as After Effects Project files starting at just a few dollars. Items are priced on the complexity, quality and use of the file. The site is home to a bustling community of motion graphics junkies!

Video Hive

Graphic River

At GraphicRiver you can buy and sell royalty-free, layered Photoshop files, vectors, icon packs, Adobe add-ons and design templates for just a few dollars. Items are priced on the complexity, quality and use of the file. The site is home to a bustling community of graphic designers and illustrators.

Graphic River

3D Ocean

At 3DOcean you can buy and sell royalty-free 3D models, textures, concepts, and materials and shaders. Items are priced on the complexity, quality and use of the file.

3D Ocean

Codecanyon

At CodeCanyon, you can purchase and sell scripts and components for a variety of languages and frameworks, currently including JavaScript, PHP, ASP.NET, and Java. Items are priced between $3 and $25; we review complexity, quality, and overall appeal when determining the specific value to assign to an item.

Code Canyon

Tutorials

At the Tuts+ Marketplace you can buy and sell tutorials and eBooks on topics like Design, Web Development and Computer Graphics. Items are priced based on depth and complexity. The site is home to a bustling community of people learning and teaching across all skill levels and is the biggest marketplace of its kind.

Tuts+ Marketplace

Photodune

At PhotoDune you can buy and sell royalty-free photographs and images starting at just $1. Items are priced on the size/megapixels of each file. The site is home to a bustling community of photographers and digital imaging experts.

Photodune

Envato Marketplaces API

All of the websites listed above can be accessed using the Envato API, making it easy for outside applications to use the data stored in the API. An API is a set of routines, data structure and object classes used to access data.

Using the API you can get to data on any of the marketplaces, these are set into 2 sections…public sets and user sets. The public set consists of everything which is publicly available such as new items, popular items, blog posts, users. The user sets consist of everything for a specific user and you need an API key to get to this information.

In this tutorial we are going to look at the public set data and how to create a WordPress plugin to get at this data.

Public Sets API Data

The data which is available for the public via the API is.

blog-posts – A list of blog posts for a particular site. Requires a site paramater, e.g. blog-posts:activedenactive-threads – Threads with the most recent messages activity. Requires a site parameter, e.g. active-threads:activeden.number-of-files – Shows the number of files in the major categories of a particular site. Requires a site paramater, e.g. number-of-files:activedennew-files – New files, recently uploaded to a particular site. Requires site and category paramaters, e.g. new-files:themeforest,site-templates or new-files:graphic-river,graphicspopular – Returns the popular files for a particular site. Requires a site paramater, e.g. popular:activedennew-files-from-user – Shows the newest 10 files a user has uploaded to a particualr site. Requires username and site paramaters, e.g. new-files-from-user:collis,themeforestrandom-new-files – Shows a random list of newly uploaded files from a particular site (i.e. like the homepage). Requires a site paramater, e.g. random-new-files:activedensearch – Search for items, users or forum messages. Returns only the first 50 results. First parameter is site (e.g. activeden, audiojungle), second parameter is type (e.g. site-templates, music, graphics, for a full list of types, look at the search select box values on the particular marketplace), third paramater is the search term (e.g. xml, preloader, dance, sky). First and second parameters are optional, third is required. Examples: search:themeforest,site-templates,xml.json search:,,collis.json search:audiojungle,,happy.jsonuser – Shows username, country, number of sales, location and image for a user. Requires a username, e.g. collisreleases – Returns Release and Set information for the API (used to generate the documentation)

The above are the parameters you need to send to the API to get the data you want. If you want to get the most popular products on CodeCanyon then use the following URL.

Popular Items On CodeCanyon Return As JSON

http://marketplace.envato.com/api/edge/popular:codecanyon.json

Popular Items On CodeCanyon Return As JSON

Popular Items On CodeCanyon Return As XML

http://marketplace.envato.com/api/edge/popular:codecanyon.xml

Popular Items On CodeCanyon Return As XML

Each of these parameters will return different nodes but if you are working with JSON or XML it is very easy to get at the data that we want.

Creating A WordPress Plugin

We are going to create a WordPress plugin which will create a number of different shortcodes to display the data we want in your WordPress posts.

We start off by defining the WordPress plugin by placing the following comments at the top of the file. This allows us to tell WordPress that this is a plugin and allows us to activate it and deactivate it from the plugin dashboard.

Enable Shortcodes

To start with we want to be able to use these shortcodes anywhere on the Website. By default you can’t use shortcodes in widgets so we are going to use the following code to allow us to use shortcodes in widgets.

Envato Wrapper API Class

Below is the class we are going to use to get all the items for a specific set and category. This is take the attributes from the shortcode to display exactly what we want. The benefit of using this class means that the code in the shortcodes is very minimal and the class will do all the work for us.

Copy the following into your PHP file.

[php]
/**
* The class wrapper for envato API
*/
class Paulund_Envato{

/**
* Shortcode attributes
*/
private $atts = FALSE;

/**
* The API URL
*/
private $url = FALSE;

/**
* All items
*/
private $items = FALSE;

/**
* Set debug mode
*/
private $debug = FALSE;

/**
* Constructor for wrapper class
*
* @param $atts – Shortcode attributes
*/
public function __construct( $atts ){
// Set default attributes
$this->atts = shortcode_atts(array(
‘marketplace’ => ”,
‘set’ => ”,
‘category’ => ”,
‘limit’ => ”,
‘id’ => ”,
‘user’ => ”,
‘search’ => ”,
‘ref’ => ‘paulund’,
‘buttontext’ => ‘Buy Now’,
‘displaytitle’ => ‘true’,
‘displayimage’ => ‘true’,
‘displaysales’ => ”,
‘displayrating’ => ”,
‘displaycost’ => ‘true’,
‘displaytags’ => ”,
‘displaycategory’ => ”,
‘displaynumberfiles’=> ”,
‘displaybutton’ => ‘true’,
‘debug’ => ‘false’), $atts);

// If debug mode is on set class field to true
if($this->atts[“debug”] == ‘true’){
$this->debug = TRUE;
}
}

/**
* Defines which set of files we want
*
* @param $set – Set of files
*/
public function Set( $set ){
$this->atts[“set”] = $set;
}

/**
* Defines which category we want from set of files
* Used on searches such as popular which return popular last week, popular last 3 months and popular authors
*
* @param $category – Defines category
*/
public function Set_Category( $category ){
$this->atts[“category”] = $category;
}

/**
* Works out the URL to call by the attributes sent
*/
private function Set_Url( $format = “.json”){
$url = “http://marketplace.envato.com/api/edge/”;

if( $this->atts[“set”] != “” ){
$url .= $this->atts[“set”];
}

if( $this->atts[“marketplace”] != “” ){
$url .= “:”.$this->atts[“marketplace”];
}

if( $this->atts[“id”] != “” ){
$url .= “:”.$this->atts[“id”];
}

$url .= $format;

$this->url = $url;
}

/**
* The main method for the class which will set the url, get the items, parse the items and display each items
*/
public function Display_Items(){

// Set the URL
$this->Set_Url();

// Display debug messages
if($this->debug){
echo ‘Start Display Items’;
echo ‘’;
print_r($this->atts);
echo ‘

‘;
}

// Extract the variables from array
extract($this->atts);

// Make sure that at least one of these are set
if( $id == ” && $marketplace == ” && $set == ”){
return;
}

// Get the items
$this->Get_Items();

// Parse the correct category
$this->Parse_Category();

// Displays each item
$this->Display_Each_Item();
}

/**
* Uses the WordPress remote get function to return the json items
*/
public function Get_Items(){
// Get items as json
$json = wp_remote_get( $this->url );

// Display debug messages
if($this->debug){
echo ‘Get Items’;
echo ‘URL – ‘.$this->url;
echo ‘’;
print_r($json);
echo ‘

‘;
}

// Set the items variable with the body of the json
$this->items = json_decode($json[“body”], true);
}

/**
* Parse the items to get exactly the data we want
*/
private function Parse_Category(){

// Display debug messages
if($this->debug){
echo ‘Parse Category’;
echo ‘’;
print_r($this->atts);
print_r($parsed_items);
echo ‘

‘;
}

// Set the items
$items = $this->items;

// Get the set of items we want
if($this->atts[“set”] != “”){
$parsed_items = $items[$this->atts[“set”]];
}

// Get the category of items we want
if($this->atts[“category”] != “”){
$parsed_items = $parsed_items[$this->atts[“category”]];
}

// Limit the items to the amount we want back
if($this->atts[“limit”] != “”){
$parsed_items = array_slice($parsed_items, 0, $this->atts[“limit”]);
}

// Display the parsed items
if($this->debug){
echo ‘End Parse Category Display Items’;
echo ‘’;
print_r($parsed_items);
echo ‘

‘;
}

// Set the items variable to the new parsed items
$this->items = $parsed_items;
}

/**
* Loop through all of the items to display each item
*/
private function Display_Each_Item(){

// Display the debug messages
if($this->debug){
echo ‘Display Each Items’;
echo ‘’;
print_r($this->items);
echo ‘

‘;
}

// Set the attributes variable
$atts = $this->atts;

// Make sure that the items are inside an array
if(!is_array($this->items[0])){
$this->items = array($this->items);
}

// Loop through all items to display on the page
foreach($this->items as $item){
$url = $item[“url”].”?ref=”.$atts[“ref”];

?>

 

Leave a reply