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:activeden
  • active-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:activeden
  • new-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,graphics
  • popular - Returns the popular files for a particular site. Requires a site paramater, e.g. popular:activeden
  • new-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,themeforest
  • random-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:activeden
  • search - 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.json
  • user - Shows username, country, number of sales, location and image for a user. Requires a username, e.g. collis
  • releases - 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.

/*
 * Plugin Name: Paulund Envato Shortcodes
 * Version: 1
 * Plugin URI: http://www.paulund.co.uk
 * Description: Create a number of shortcodes to be used in your WordPress themes
 * Author: Paul Underwood
 * Author URI: http://www.paulund.co.uk
 */

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.

// Enable shortcodes in widget areas
add_filter( 'widget_text', 'do_shortcode' );

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.

/**
 * 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 '<pre>';
			print_r($this->atts);
			echo '</pre>';
		}
		
		// 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 '<pre>';
			print_r($json);
			echo '</pre>';
		}
		
		// 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 '<pre>';
			print_r($this->atts);
			print_r($parsed_items);
			echo '</pre>';
		}
		
		// 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 '<pre>';
			print_r($parsed_items);
			echo '</pre>';
		}
		
		// 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 '<pre>';
			print_r($this->items);
			echo '</pre>';
		}

		// 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"];
			
			?><div class="paulund_envato"><?php
			
			// Display the title
			if($atts["displaytitle"] == "true"){
				$title = NULL;
				
				if(isset($item["item"])){
					$title = $item["item"];
				}
				
				if(isset($item["title"])){
					$title = $item["title"];
				}
				
				if($title != NULL){
					?><h2 class="paulund_envato_header"><?php echo $title;?></h2><?php	
				}
			}
			
			// Display image
			if($atts["displayimage"] == "true"){
				$image = NULL;
				
				if(isset($item["live_preview_url"]) && $image == NULL){
					$image = $item["live_preview_url"];
				}
				
				if(isset($item["image"]) && $image == NULL){
					$image = $item["image"];
				}
				
				if(isset($item["thumbnail"]) && $image == NULL){
					$image = $item["thumbnail"];
				}
				
				if($image != NULL){
				?>
				<a href="<?php echo $url;?>" target="_blank">
					<img class="paulund_envato_image" 
						src="<?php echo $image;?>" 
							alt="<?php echo $item["item"];?>" />
					</a>
				<?php
				}
			}
			
			// Display sales
			if($atts["displaysales"] == "true"){
				?><p class="paulund_envato_sales">Total Sales: <?php echo $item["sales"];?></p><?php
			}
			
			// Display ratings
			if($atts["displayrating"] == "true"){
				?><p class="paulund_envato_rating">Rating: <?php echo $item["rating"];?> out of 5</p><?php
			}
			
			// Display the cost
			if($atts["displaycost"] == "true"){
				?><p class="paulund_envato_cost">Price: $<?php echo $item["cost"];?></p><?php
			}
			
			// Display the tags
			if($atts["displaytags"] == "true"){
				?><p class="paulund_envato_tags">Tags: <?php echo $item["tags"];?></p><?php
			}
			
			// Display the category
			if($atts["displaycategory"] == "true"){
				?><p class="paulund_envato_category">Category: <?php echo $item["category"];?></p><?php
			}
			
			// Display the number of files
			if($atts["displaynumberfiles"] == "true"){
				?><p class="paulund_envato_number_files">Number Of Files: <?php echo $item["number_of_files"];?></p><?php
			}
					
			// Display the button
			if($atts["displaybutton"] == "true"){
				?>
				<a href="<?php echo $url; ?>" 
					target="_blank" 
					class="button paulund_envato_button">
					<?php echo $atts["buttontext"]; ?>
					</a>
				<?php	
			}
			
			?>
			
			</div>
			
			<?php
		}
	}
}

Envato Shortcodes

Now we can use shortcodes in widgets we can add shortcodes anywhere in the theme.

In this plugin we are going to make shortcodes for the following.

  • Popular Files
  • Featured Files
  • New Files
  • New Files by certain Author
  • Random Files
  • Specific Files
  • All files from a specific user
add_shortcode( 'paulund_envato_popular', 'paulund_envato_popular_function' );
add_shortcode( 'paulund_envato_features', 'paulund_envato_features_function' );
add_shortcode( 'paulund_envato_new_files', 'paulund_envato_new_files_function' );
add_shortcode( 'paulund_envato_new_files_from_user', 'paulund_envato_new_files_from_user_function' );
add_shortcode( 'paulund_envato_random_new_files', 'paulund_envato_random_new_files_function' );
add_shortcode( 'paulund_envato_item', 'paulund_envato_item_function' );
add_shortcode( 'paulund_envato_user', 'paulund_envato_user_function' );

Each of these shortcodes will run a PHP function, from this function we can call the above class to get exactly the items we want.

Copy the following into your PHP file outside of the Envato Wrapper Class.

/**
 * Get popular envato files
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_popular_function( $atts, $content = null ){									
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("popular");
	$envato->Set_Category($atts["category"]);
	$envato->Display_Items();
}

/**
 * Get featured envato files
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_features_function( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("features");
	$envato->Set_Category($atts["category"]);
	$envato->Display_Items();
}

/**
 * Get new envato files
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_new_files_function( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("new-files");
	$envato->Display_Items();
}

/**
 * Get new envato files for user
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_new_files_from_user_function( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("new-files-from-user");
	$envato->Display_Items();
}

/**
 * Get random new envato files
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_random_new_files_function( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("random-new-files");
	$envato->Display_Items();
}

/**
 * Get specific envato file item
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_item_function ( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("item");
	$envato->Display_Items();
}

/**
 * Get envato files for specific user
 * 
 * @param $atts - Attributes in the shortcode
 * @param $content - Content in shortcode can be null
 */
function paulund_envato_user_function( $atts, $content = null ){
	$envato = new Paulund_Envato($atts);
	
	$envato->Set("user");
	$envato->Display_Items();
}

Using The Shortcodes

With all the code for your WordPress plugin finished we can discover how to use these shortcodes.

Popular Files

The envato popular files API return 3 types popular files.

  • Popular items last week
  • Popular items of the last three months
  • Popular Author of last month

Because for these three categories we need to use both the set and category attributes in the shortcode

Popular items last week shortcode

[ paulund_envato_popular marketplace="codecanyon" category="items_last_week" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" limit="5" ]

Popular items of the last three months shortcode

[ paulund_envato_popular marketplace="codecanyon" category="items_last_three_months" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" limit="5" ]

Popular Author of last month shortcode

[ paulund_envato_popular marketplace="codecanyon" category="authors_last_month" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" limit="5" ]

Featured Files

The featured call will return the current featured files on the marketplace.

  • Featured File
  • Featured Authors
  • Free File

Featured File shortcode

[ paulund_envato_features marketplace="codecanyon" category="featured_file" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

Featured Authors shortcode

[ paulund_envato_features marketplace="codecanyon" category="featured_author" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

Free File shortcode

[ paulund_envato_features marketplace="codecanyon" category="free_file" displaysales="true" displayrating="true" displaytags="true" buttontext="Get Your Free File" ]

New Files

To get the new files for a marketplace you also need to pass the category you want to get the new files from.

To do this use the following shortcode.

[ paulund_envato_new_files marketplace="themeforest,site-templates" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

New Files by certain Author

Get the new files for a certain user.

[ paulund_envato_new_files_from_user marketplace="collis,themeforest" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

Random Files

Get random new files for a marketplace.

[ paulund_envato_random_new_files marketplace="themeforest" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

Specific Files

Get specific files by the item ID.

[ paulund_envato_item id="372656" displaysales="true" displayrating="true" displaytags="true" buttontext="Buy Now" ]

All files from a specific user

Get all the files from a specific user.

[ paulund_envato_user id="collis" displaysales="true" displayrating="" displaytags="" displaycost="" buttontext="Buy Now" ]

Demo

To see what these shortcodes can create view these pages.

Advertise here 50% Off

Comment