Let\’s not recommend a plugin – for once. WordPress is really great for non-techies to build a site – and plugins especially make it so easy to extend your site!

In my opinion, the motto should be \”Do I really need this plugin\”, however fancy a plugin might be – with easy to setup templates, slick looking design and animations (like visual page builder), there is of course a price:

  1. Maintainability: Who can ensure the owners of the plugin will update it? Bug fixing might be late or never. And since it\’s their code, it\’s hard to dive into.
  2. Extendability: plugins have limited functionality. One specific request and your plugin is useless! Wrong choice and doesn\’t seem very agile!
  3. Speed: My favourite topic.. Count the milliseconds. Very few plugins take this into account. And since it needs to be pluggable and adjustable in the admin interface, it\’s never gonna be efficient. Think about \”inline JS/CSS\”, external scripts, or massive libraries that need to be loaded just in case!

This last reason was vital in my last project. The smashballoons Instagram feed – very fancy, with a lot of options for customizing was adding 3-4 different libraries of carousels or sliders. Well they combined it into one file, which was great! But, the one .js file was huge and was affecting the execution time of scripts of my project. Unacceptable! Especially since I only needed a simple instagram gallery.

So, by just looking up existing PHP libraries in github, I found the solution. Just load a small wrapper for the instagram feed. Include it in my theme or as a plugin and modify the returned data to display a gallery. Cache the requests to prevent overloading the api (200 requests per day maximum) and we are good to go!

Here is the class that contains the most important stuff. Note that I am saving the request as a JSON file, since I didn\’t want to store it in the database. The JSON file also gets cached funnily enough.

Btw I am using this wrapper.

<?php

require get_template_directory() . \'/vendor/autoload.php\';

use Vinkla\\Instagram\\Instagram;

class InstaFeed{

    private $access_token;

    public function __construct() {

        $this->access_token = \'access-token\';
    }

    public function get_instagram_posts($count)
    {

        $instagram = new Instagram($this->access_token);

        $result = get_transient( \'instagram\' );


        if ( false !== $result ) :

            // so there is a transient! Lets get it from the JSON

            $posts = $this->get_data();



        else :

            // no transient! New request

                $posts =  $instagram->media([\'count\' => $count]);

                //  after expiry we need  a new one

                $bool_response = set_transient( \'instagram\', true, 60*60*12 );

                if( false === $bool_response ) :

                    set_transient( \'instagram\', true, 60*60*12 );

                    else :

                    $this->save_data($posts);

                endif;

            endif;


        return $posts;
    }

    public function save_data($items){
        $arr = array();

        foreach($items as $item) :
            $arr[] = array(
                //\'title\' => $item->title,
                \'link\' => $item->link,
                \'image\' => $item->images->standard_resolution->url,
                \'low_res_image\' => $item->images->low_resolution->url
            );
        endforeach;

        $fp = fopen(get_template_directory() . \'/inc/lib/insta.json\', \'w\');
        fwrite($fp, json_encode($arr));
        fclose($fp);
    }

    public function get_data(){
        $json = file_get_contents(get_template_directory() . \'/inc/lib/insta.json\');
        $obj = json_decode($json);

        return $obj;
    }
}

The wrapper seems not to work anymore due to changes in Instagram / Facebook\’s SDK\’s or libraries. For now, I\’ll leave it as it is, but I will have a new way of pulling up Facebook or Instagram posts without a plugin! Updates coming.

Leave a Comment

Your email address will not be published. Required fields are marked *