Get plugin data from WordPress.org API

All of our free WordPress plugins that are listed on this website are also published on the WordPress.org plugin repository here: https://profiles.wordpress.org/pluginsclub/#content-plugins

The WordPress.org API provides an easy way for plugin developers to get and display their plugin data from the repository.

We have integrated the API to automatically display up-to-date data from WP.org on our website.

What’s new?

  • Changelog – Plugin version is displayed and Changelog from wp.org is shown when clicked.
  • Star rating – Displays the number of reviews for that plugin on wp.org and links to the reviews page.
  • Compatible with – Displays up to which WP version the plugin has been tested and is compatible with.
  • Last update – The date when the plugin was last updated.

For anyone who wants to experiment with the WordPress.org API, here is the code that We used:

We created a simple WordPress plugin that uses shortcodes:

<?php

// Define the shortcode
function plugin_data_shortcode( $atts ) {

    // Get plugin slug from the shortcode
    $atts = shortcode_atts( array(
        'plugin' => ''
    ), $atts );
    
    // Check for plugin slug
    if ( empty( $atts['plugin'] ) ) {
        return 'Please provide a plugin slug.';
    }
    
    // Get data from WordPress.org API
    $response = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slug]=' . $atts['plugin'] );
    
    // Check the API call
    if ( is_wp_error( $response ) ) {
        return 'Error retrieving data from WordPress.org API.';
    }
    
    // Parse the API response
    $data = json_decode( wp_remote_retrieve_body( $response ) );
    
    // Check if the plugin exists
    if ( empty( $data ) || $data->response == 'false' ) {
        return 'Plugin not found on WordPress.org.';
    }
    
    // Construct the output HTML
$output = '<p class="btns"><span class="version"><a href="#" style="color:white;text-decoration:none;" onclick="showChangelog(\''.esc_js($data->sections->changelog).'\')">Version: ' . $data->version . '</a></span> <span class="reviews"><a href="https://wordpress.org/support/plugin/' . $atts['plugin']  . '/reviews" target="_blank" style="color:white;text-decoration:none;">⭐⭐⭐⭐⭐(' . $data->num_ratings . ')</a></span> <span class="wpversion">Compatible with WordPress ' . $data->tested . '</a></span> <span class="last-update">Last update: ' . date( 'F j, Y', strtotime( $data->last_updated ) ) . '</span></p>';

// Add JavaScript function to show changelog in a popup
$output .= '<script>
function showChangelog(changelogText) {
    var popup = window.open("", "Changelog", "width=600,height=400,scrollbars=yes,resizable=yes");
    popup.document.write("<html>" + changelogText + "</html>");
}
</script>';

return $output;
}

// Register the shortcode
add_shortcode( 'data', 'plugin_data_shortcode' );

Then add the shortcode anywhere to display the data:

[data plugin="toolbar-links"]

Custom CSS to style the data:

.last-update, .version, .wpversion, .reviews{
	background-color:#0073aa;
	padding-left:10px;
	padding-right:10px;
	padding-top:5px;
	padding-bottom:5px;
	margin: 0 5px 0 5px;
	border-top-left-radius:10px;
	border-bottom-left-radius:0px;
	border-bottom-right-radius:10px;
	color:#ffffff;
}

/* 782px and smaller screen sizes */
@media (max-width:782px){

	.last-update, .version, .wpversion, .reviews{
		background-color: black;
		display: block;
		margin: 10px;
	}
	
}

Result:


Posted

in

by

Tags:

Comments

Leave a Reply

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

Categories: