Last active
March 31, 2017 16:00
-
-
Save elliotcondon/e9dd5118e9eb32f0f0700f94c6349f78 to your computer and use it in GitHub Desktop.
Custom wordpress.org plugin download list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Thanks for sharing the code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi WP devs,
Please find above a solution to display a list of downloadable 'plugin versions' on your website.
This solution is needed while the new wordpress.org plugin repo no longer shows a list of plugin tags.
You can find a ticket regarding this issue here: https://meta.trac.wordpress.org/ticket/2365
Instructions
Using the above solution is simple:
get_my_plugin_downloads()
function into your functions.php fileNotes
The above code finds available 'tags' by looking at the readme.txt 'changelog'. This may result in returning tags that may not exist in the plugin svn repo. This could be improved by looking at the actual repo folder HTML page and using regex to find tags, however, this simple solution seems to work well.
I have a working example of this code on the ACF website here: https://www.advancedcustomfields.com/downloads/
Please let me know if you have any issues or can improve on the code.
Thanks
Elliot