Skip to content

Instantly share code, notes, and snippets.

@elliotcondon
Last active March 31, 2017 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotcondon/e9dd5118e9eb32f0f0700f94c6349f78 to your computer and use it in GitHub Desktop.
Save elliotcondon/e9dd5118e9eb32f0f0700f94c6349f78 to your computer and use it in GitHub Desktop.
Custom wordpress.org plugin download list
<?php
/*
* get_my_plugin_downloads
*
* This function will return an array of plugin download information
*
* @type function
* @date 31/3/17
* @since 5.5.10
*
* @param $slug (string)
* @return (array)
*/
function get_my_plugin_downloads( $slug = '' ) {
// vars
$plugin = array(
'version' => 0,
'download' => '',
'tags' => array()
);
$url = 'http://api.wordpress.org/plugins/info/1.0/' . $slug;
// connect
$request = wp_remote_post( $url );
// success
if( !is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
// unserialize
$obj = @unserialize($request['body']);
// version
$plugin['version'] = $obj->version;
$plugin['url'] = 'https://downloads.wordpress.org/plugin/'.$slug.'.zip';
// tags
preg_match_all('/<h4>(.+?)<\/h4>/', $obj->sections['changelog'], $matches);
// add tags
if( isset($matches[1]) ) {
foreach( $matches[1] as $tag ) {
$plugin['tags'][] = array(
'version' => $tag,
'url' => str_replace('.zip', '.'.$tag.'.zip', $plugin['url'])
);
}
}
}
// return
return $plugin;
}
?>
<?php
$plugin = get_my_plugin_downloads('advanced-custom-fields');
?>
<h2>Current Version</h2>
<ul>
<li><?php echo $plugin['version']; ?> - <a href="<?php echo $plugin['url']; ?>">download</a></li>
</ul>
<h2>Other Versions</h2>
<ul>
<?php foreach( $plugin['tags'] as $tag ): ?>
<li><?php echo $tag['version']; ?> - <a href="<?php echo $tag['url']; ?>">download</a></li>
<?php endforeach; ?>
</ul>
@lukecav
Copy link

lukecav commented Mar 31, 2017

@elliotcondon

Thanks for sharing the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment