getButterfly Logo getButterfly

Note: This function has been updated to use one transient only. Scroll down to see the updated version.

This code will retrieve a list of plugins from the WordPress plugin repository and it will cache the results for 24 hours, using the plugin API. Use the {wordpress-items slugs="quick-event-calendar, youtube-playlist-player, wp-perfect-plugin, mobilize, ecards-lite, marketplace-items"} shortcode to display them. Note that some CSS styling might be necessary.

<?php
/*
 * WordPress Plugins Shortcode
 * 
 * Displays your WordPress plugins inside a post or a page
 * 
 * @author Ciprian Popescu
 * @copyright 2016, 2017 Ciprian Popescu
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

function whiskey_wordpress_items($atts) {
    extract(shortcode_atts(array(
        'slugs' => '',
    ), $atts));

    $url = 'https://api.wordpress.org/plugins/info/1.0/';

    $data = '';

    $slugs = explode(',', $slugs);
    $slugCount = count($slugs);
    if ($slugCount > 0) {
        $data .= '<ul class="columnar">';
        for ($i = 0; $i < $slugCount; $i++) {
            $args = (object) array('slug' => trim($slugs[$i]));
            $request = array('action' => 'plugin_information', 'timeout' => 15, 'request' => serialize($args));

            /*
             * Set plugin data transient if it does not exist.
             * If it exists, retrieve it from the database
             */
            $slug_transient = get_transient('wp-plugin-' . strtolower(trim($slugs[$i])));

            if($slug_transient === false) {
                $response = wp_remote_post($url, array('body' => $request));
                $plugin_info = unserialize($response['body']);

                $slug = $plugin_info->slug;
                $name = $plugin_info->name;
                $downloaded = $plugin_info->downloaded;
                $author = $plugin_info->author;
                $version = $plugin_info->version;

                set_transient (
                    'wp-plugin-' . strtolower(trim($slugs[$i])),
                    $slug . '|' . $name . '|' . $downloaded . '|' . $author . '|' . $version,
                    24 * HOUR_IN_SECONDS
                );
            } else {
                $plugin_info = explode('|', $slug_transient);

                $slug = $plugin_info[0];
                $name = $plugin_info[1];
                $downloaded = $plugin_info[2];
                $author = $plugin_info[3];
                $version = $plugin_info[4];
            }
            //

            $data .= '<li>
                <div class="package-widget package-wordpress">
                    <div class="package-details">
                        <h3><a class="name" href="https://wordpress.org/plugins/' . $slug . '/">' . $name . '</a></h3>
                        <p class="description"><code>' . $version . '</code></p>
                        <p class="author quiet">downloaded ' . number_format($downloaded) . ' times | ' . $author . '</p>
                    </div>
                </div>
            </li>';
        }
        $data .= '</ul>';
    }

    return $data;
}

add_shortcode('wordpress-items', 'whiskey_wordpress_items');

This is how it looks on my website:

WordPress Plugins - API

UPDATE:

I have updated the code to use one transient instead of multiple ones, based on the number of plugins. See the updated code below. It will safely replace the one above.

function whiskey_wordpress_items($atts) {
    extract(shortcode_atts(array(
        'slugs' => '',
    ), $atts));

    $data = get_transient('whiskey_plugins');
    if ($data === false) {
        $url = 'https://api.wordpress.org/plugins/info/1.0/';

        $data = '';

        $slugs = explode(',', $slugs);
        $slugCount = count($slugs);
        if ($slugCount > 0) {
            $data .= '<ul class="columnar">';
                for ($i = 0; $i < $slugCount; $i++) {
                    $args = (object) array('slug' => trim($slugs[$i]));
                    $request = array('action' => 'plugin_information', 'timeout' => 15, 'request' => serialize($args));

                    $response = wp_remote_post($url, array('body' => $request));

                    $plugin_info = unserialize($response['body']);

                    $slug = $plugin_info->slug;
                    $name = $plugin_info->name;
                    $downloaded = $plugin_info->downloaded;
                    $author = $plugin_info->author;
                    $version = $plugin_info->version;

                    $data .= '<li>
                        <div class="package-widget package-wordpress">
                            <div class="package-details">
                                <h3><a class="name" href="https://wordpress.org/plugins/' . $slug . '/">' . $name . '</a></h3>
                                <p class="description"><code>' . $version . '</code></p>
                                <p class="author quiet">downloaded ' . number_format($downloaded) . ' times | ' . $author . '</p>
                            </div>
                        </div>
                    </li>';
                }
            $data .= '</ul>';
        }

        set_transient('whiskey_plugins', $data, 24 * HOUR_IN_SECONDS);
    }

    return $data;
}

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.



If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a Reply

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