Advertisement
  1. Code
  2. WordPress
  3. Plugin Development

Creating a Simple Instagram Plugin

Scroll to top
Read Time: 5 min

In the following tutorial we will create a plugin to get the popular pictures from Instagram's main feed.


1. The Plan

Our plugin will work with the [instagradam] shortcode. You can insert it anywhere where HTML content can go eg. template code, editor code, etc.

As a result about 10-15 thumbnail pictures will be shown with clickable links. The core of the plugin is based around a remote feed, which we will retrieve using the Function API of WordPress.

The raw data list functionally will look like this:

1
2
/*

3
  theluxurystyle --- https://distilleryimage8.s3.amazonaws.com/c4c876f4780a11e2a15422000a9f19a4_5.jpg

4
  loveobe --- http://distilleryimage3.s3.amazonaws.com/0c2d3b20781911e2b92122000a9e0727_5.jpg

5
  jaredleto --- http://distilleryimage8.s3.amazonaws.com/21d07bce781c11e2adc122000a1f9ace_5.jpg

6
  balloop --- http://distilleryimage11.s3.amazonaws.com/4fe04e66781411e2890222000a1fb0b2_5.jpg

7
  pinkshosho --- http://distilleryimage10.s3.amazonaws.com/abaef1b4781b11e2a2ce22000a1fa411_5.jpg

8
*/
This is our planThis is our planThis is our plan
We will pull in some pictures

2. Getting Client ID and Secret

This step is needed for every new plugin. Register it at Instagram to get a client_id and client_secret. The plugin name should be simple alphabetical characters in my experience (eg. johnsplugin).


3. Plugin Information

This is the place to describe your plugin's base data like plugin name, url, version number, and author.

1
2
/*

3
Plugin Name: Instagradam

4
Plugin URI: https://code.tutsplus.com

5
Description: A simple and fast Instagram shortcode plugin. Please use [instagradam] to pull main feed!

6
Version: 1.0

7
Author: Adam Burucs

8
Author URI: http://burucs.com/

9
*/

4. Registering the Shortcode

This will define the [instagradam] shortcode, which will work based on the instagradam_embed_shortcode function.

1
2
	// register shortcode

3
	add_shortcode( 'instagradam', 'instagradam_embed_shortcode' );

5. Defining Main Function for the Shortcode

This will describe our plugin's core operation. The $atts and $content should be defined as we see here, but we won't use them in this lesson.

1
2
	// define shortcode

3
	function instagradam_embed_shortcode( $atts, $content = null ) {
4
5
		// ...

6
7
	}

6. Making Variables

We need a helper variable for making an output for our function and a data retrieval which uses the Wordpress Function API. That is $str and $result, respectively.

1
2
		// define main output

3
		$str    = "";
4
		// get remote data

5
		$result = wp_remote_get( "https://api.instagram.com/v1/media/popular?client_id=3f72f6859f3240c68b362b80c70e3121" );

7. Handling Feed Errors

The main selection handles the feed error (in some cases we can get SSL errors, but there is a fix for that described later in this article).

If there is any error we echo it to the page: Something went wrong: ... .

1
2
	if ( is_wp_error( $result ) ) {
3
		// error handling

4
		$error_message = $result->get_error_message();
5
		$str           = "Something went wrong: $error_message";
6
	} else {
7
		// processing further

8
		// ...

9
	}

8. More Variables

Variable $result will contain the main data, for processing we make another helper called $main_data. We also need a counter for the iteration.

1
2
// processing further

3
$result    = json_decode( $result['body'] );
4
$main_data = array();
5
$n         = 0;

9. Looping, Part One

This loop will collect all usernames and thumbnails we need. Previously I analyzed the main feed (the structure of the feed), to discover how to get the data I want. So this is an important step and also let's not forget that Instagram can change this later
and we might need to modify $d->user->username and $d->images->thumbnail->url.

1
2
	// get username and actual thumbnail

3
	foreach ( $result->data as $d ) {
4
		$main_data[ $n ]['user']      = $d->user->username;
5
		$main_data[ $n ]['thumbnail'] = $d->images->thumbnail->url;
6
		$n++;
7
	}

10. Looping, Part Two

In the following lines, we create the HTML code which will contain the pictures and links from the Instagram main feed. The links will open in a new window, made with target="_blank". Note the space at the end of the main string, this is for basic separation.

1
2
	// create main string, pictures embedded in links

3
	foreach ( $main_data as $data ) {
4
		$str .= '<a target="_blank" href="http://instagram.com/'.$data['user'].'"><img src="'.$data['thumbnail'].'" alt="'.$data['user'].' pictures"></a> ';
5
	}

11. The Easy Part

This (shortcode) standard code will return our main content.

1
2
	return $str;

12. SSL Problems

In some cases the wp_remote_get function can work badly, to solve this we need to use this code before the main code sections.

1
2
	// fix SSL request error

3
	add_action( 'http_request_args', 'no_ssl_http_request_args', 10, 2 );
4
	function no_ssl_http_request_args( $args, $url ) {
5
		$args['sslverify'] = false;
6
		return $args;
7
	}

13. Complete Code

The finished code looks like this.

1
2
/*

3
Plugin Name: Instagradam

4
Plugin URI: https://code.tutsplus.com

5
Description: A simple and fast Instagram shortcode plugin. Please use [instagradam] to pull main feed!

6
Version: 1.0

7
Author: Adam Burucs

8
Author URI: http://burucs.com/

9
*/
10
11
	// fix SSL request error

12
	add_action( 'http_request_args', 'no_ssl_http_request_args', 10, 2 );
13
	function no_ssl_http_request_args( $args, $url ) {
14
		$args['sslverify'] = false;
15
		return $args;
16
	}
17
18
	// register shortcode

19
	add_shortcode( 'instagradam', 'instagradam_embed_shortcode' );
20
	
21
	// define shortcode

22
	function instagradam_embed_shortcode( $atts, $content = null ) {
23
		// define main output

24
		$str    = "";
25
		// get remote data

26
		$result = wp_remote_get( "https://api.instagram.com/v1/media/popular?client_id=3f72f6859f3240c68b362b80c70e3121" );
27
28
		if ( is_wp_error( $result ) ) {
29
			// error handling

30
			$error_message = $result->get_error_message();
31
			$str           = "Something went wrong: $error_message";
32
		} else {
33
			// processing further

34
			$result    = json_decode( $result['body'] );
35
			$main_data = array();
36
			$n         = 0;
37
38
			// get username and actual thumbnail

39
			foreach ( $result->data as $d ) {
40
				$main_data[ $n ]['user']      = $d->user->username;
41
				$main_data[ $n ]['thumbnail'] = $d->images->thumbnail->url;
42
				$n++;
43
			}
44
45
			// create main string, pictures embedded in links

46
			foreach ( $main_data as $data ) {
47
				$str .= '<a target="_blank" href="http://instagram.com/'.$data['user'].'"><img src="'.$data['thumbnail'].'" alt="'.$data['user'].' pictures"></a> ';
48
			}
49
		}
50
51
		return $str;
52
	}

Finished Look

This is a picture showing the plugin in action. For this, the shortcode was inserted in an article.

Finished plugin, how it looks in browserFinished plugin, how it looks in browserFinished plugin, how it looks in browser
How it looks in a browser
Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.