Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Find url to current version of a plugin from "outside" wordpress? WordPress

  • SOLVED

To simplyfy uploading wordpress to new sites from my somewhat unreliable internet-connection, I have made a simple single-file script that downloads and unzips the current version of WP directly to the server, together with the plugins I "always" use.

However - while getting the current version of WP is simple, many plugins use their version number in the filename for the zip, so I instead have to upgrade them after the install is done (I've hardcoded the urls to these zips into the script), and since I'd like to make this script public, I would like to do this smarter and get the current versions automatically.

Does anyone know how I could do this? Can it be done by checking the repository in some way?

(and yeah, I am planning to let people choose if they want to install a plugin or not)

I run this part of the script after WP has been downloaded and unzipped, so maybe its possible to use wordpress' own functions?

This is the function I use now:

function plugindownload($pluginname, $pluginurl)
{
$dir = dirname(__FILE__);
$pluginfile=$dir."/wp-content/plugins/".$pluginname.".zip";
$plugindir=$dir."/wp-content/plugins/".$pluginname;
download_remote_file($pluginurl, $pluginfile);
echo "<li>".$pluginname." installed</li>";
$zip = new ZipArchive;

$zipped = $zip->open($pluginfile);

if ( $zipped ) {

$zip->extractTo($dir."/wp-content/plugins");
unlink( $pluginfile );

$zip->close();
}
set_time_limit(25);
}


and then call it like this:
plugindownload('wp-super-cache', 'http://downloads.wordpress.org/plugin/wp-super-cache.1.1.zip');

Answers (3)

2012-07-17

Hai Bui answers:

I found the code to do that here http://wikiduh.com/869/get-the-latest-version-number-of-a-wordpress-plugin-with-php
<?php
function pull_tag( $plugin_uri, $tag = 'Stable tag' ) {
$trunk_readme = file( 'http://plugins.svn.wordpress.org/' . $plugin_uri . '/trunk/readme.txt' );
foreach( $trunk_readme as $i => $line )
if( substr_count( $line, $tag . ': ' ) > 0 ) return trim( substr( $line, strpos( $line, $tag . ': ' ) + strlen( $tag ) + 2 ) );
return NULL;
}

$latest_version = pull_tag( 'bbpress' );
?>

2012-07-17

Ross Wilson answers:

This should get you the current version number from the readme.txt file in the svn repo


<?php
function pull_tag( $plugin_uri, $tag = 'Stable tag' ) {
$trunk_readme = file( 'http://plugins.svn.wordpress.org/' . $plugin_uri . '/trunk/readme.txt' );
if($trunk_readme){
foreach( $trunk_readme as $i => $line )
if( substr_count( $line, $tag . ': ' ) > 0 ) return trim( substr( $line, strpos( $line, $tag . ': ' ) + strlen( $tag ) + 2 ) );
}
return NULL;
}


$plugin = 'wp-super-cache';
$latest_version = pull_tag( 'wp-super-cache' );

echo $plugin . " " . $latest_version;

?>


Ross Wilson comments:

I have tested it on a few plugins and you seem to be able to get the latest version with just using the plugin name like so

bbpress: http://downloads.wordpress.org/plugin/bbpress.zip
wp-super-cache: http://downloads.wordpress.org/plugin/wp-super-cache.zip


Torstein Opperud comments:

Now I'll be damned... it really seems to be that simple Ross :)

2012-07-17

Dbranes answers:

You can get the latest plugin version number from the readme.txt files (Stable tag: x.x.x)


$plugin_readme_url="http://plugins.svn.wordpress.org/{$plugin_name}/trunk/readme.txt";


For example in the case of wp-super-cache:

[[LINK href="http://plugins.svn.wordpress.org/wp-super-cache/trunk/readme.txt"]]http://plugins.svn.wordpress.org/wp-super-cache/trunk/readme.txt[[/LINK]]

Stable tag: 1.1

So you could fetch these readme.txt files for your plugins and parse out the Stable tag line.


Torstein Opperud comments:

yeah, but not all plugins use the version number in the filename... for instance - "All in one SEO pack" - http://wordpress.org/extend/plugins/all-in-one-seo-pack/ - there the filename is http://downloads.wordpress.org/plugin/all-in-one-seo-pack.zip, regardless of version numbers


Dbranes comments:


I wrote a very simple test case that works for me at least:

<?php
function get_plugin_version($name){

$readme_file="http://plugins.svn.wordpress.org/$name/trunk/readme.txt";
$readme_text=file_get_contents($readme_file);

$lines=explode("\n",$readme_text);
$needle="Stable tag:";

foreach($lines as $line ){
if( strpos($line,$needle) > -1 ){
$version= trim(str_replace($needle,"",$line));
break;
}
}

return $version;
}

echo get_plugin_version("wp-super-cache");

?>


This returns "1.1"


Dbranes comments:

Another approach would be to fetch the whole plugin page, fx

[[LINK href="http://wordpress.org/extend/plugins/all-in-one-seo-pack/"]]http://wordpress.org/extend/plugins/all-in-one-seo-pack/[[/LINK]]

and parse out the version number from the download link html tags

<p class="button"><a href='http://downloads.wordpress.org/plugin/all-in-one-seo-pack.zip'>Download Version 1.6.14.5</a></p>


Dbranes comments:

Third approach would be to use the same method as Wordpress

The code that Wordpress is using to check plugins is (see wp-includes/update.php)


$to_send = (object) compact('plugins', 'active');

$options = array(
'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
'body' => array( 'plugins' => serialize( $to_send ) ),
'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
);

$raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);

if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
return false;

$response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );

if ( is_array( $response ) )
$new_option->response = $response;
else
$new_option->response = array();

set_site_transient( 'update_plugins', $new_option );


so you could use api directly:

[[LINK href="http://api.wordpress.org/plugins/update-check/1.0/"]]http://api.wordpress.org/plugins/update-check/1.0/[[/LINK]]

and I found some info here about this api:

[[LINK href="http://dd32.id.au/projects/wordpressorg-plugin-information-api-docs/"]]http://dd32.id.au/projects/wordpressorg-plugin-information-api-docs/[[/LINK]]