从 WordPress 插件目录获取插件版本号

Posted

技术标签:

【中文标题】从 WordPress 插件目录获取插件版本号【英文标题】:Get plugin version number from WordPress plugin directory 【发布时间】:2013-04-24 19:56:48 【问题描述】:

谁能指出我如何通过其网页从 WordPress 插件目录中检索插件的最新版本号?

例如,在http://wordpress.org/extend/plugins/advanced-custom-fields/

我要抢4.0.1

【问题讨论】:

【参考方案1】:

您可以与 WordPress 存储库 API 交互:

WordPress Repository API 是用于获取插件和主题信息的 API。

http://wp.tutsplus.com/tutorials/creative-coding/interacting-with-wordpress-plug-in-theme-api/

【讨论】:

谢谢,这很有用!【参考方案2】:

我在我的插件中使用以下内容。详情请查看 cmets。

/**
 * Function used to print the data
 */
function b5f_print_repository_info( $echo = true )

    // Grab data and do nothing if fail
    $plugin_data = b5f_get_repository_info();
    if( false === $plugin_data )
        return;

    // Custom function used to format the rating
    $total_downloads = number_format_i18n( $plugin_data['total_downloads'] );
    $rating          = b5f_format_rating( $plugin_data['rating'] / 20 );
    $updated         = date_i18n( get_option( 'date_format' ), strtotime( $plugin_data['updated'] ) );
    $num_rating      = number_format_i18n( $plugin_data['num_ratings'] );
    $version         = $plugin_data['version'];

    if( $echo ) 
        echo 'Your stuff using the variables above.';
    else 
        return 'Your stuff using the variables above.';


/** 
 * Call WP API and return the data
 */ 
function b5f_get_repository_info()

    $plugin_url = 'http://wpapi.org/api/plugin/advanced-custom-fields.json';

    // Cache
    $cache = get_transient( 'my_plugin_transient' );
    if( false !== $cache )
        return $cache;

    // Fetch the data
    if( $response = wp_remote_retrieve_body( wp_remote_get( $plugin_url ) ) )
    
        // Decode the json response
        if( $response = json_decode( $response, true ) )
        
            // Double check we have all our data
            if( !empty( $response['added'] ) )
            
                set_transient( 'my_plugin_transient', $response, 720 );
                return $response;
            
        
    
    return false;


/**
 * Auxiliary function to format the Rating
 */
function b5f_format_rating( $number, $cents = 1 )

    // Check if value can be dealt with
    if( !is_numeric( $number ) )
        return $number;

    if( !$number ) 
        $rating = ($cents == 2) ? '0.00' : '0';
    
    else 
        if( floor( $number ) == $number ) 
            $rating = number_format( $number, ($cents == 2 ? 2 : 0 ) );
        
        else 
            $rating = number_format( round( $number, 2 ), ($cents == 0 ? 0 : 2 ) );
        
    
    return $rating;

以下是响应的缩短版,descriptionstats 字段真的很大。

Array
(
    [added] => 2011-03-25
    [author] => Array
        (
            [name] => Elliot Condon
            [url] => http://www.elliotcondon.com/
            [profile] => http://profiles.wordpress.org/elliotcondon
        )

    [average_downloads] => 1415.61
    [contributors] => Array
        (
            [contributor-Elliot Condon] => 
        )

    [download_link] => http://downloads.wordpress.org/plugin/advanced-custom-fields.zip
    [hits] => 0
    [homepage] => http://www.advancedcustomfields.com/
    [last_update_details] => 2013-04-30 17:36:06
    [last_update_stats] => 2013-04-30 17:36:05
    [name] => Advanced Custom Fields
    [num_ratings] => 905
    [rating] => 98
    [requires] => 3.0.0
    [sections] => Array
        (
            [description] => <p>Advanced Custom Fields is 
        )

    [slug] => advanced-custom-fields
    [stats] => Array
        (
            [2011-11-09] => 683
        )

    [tags] => Array
        (
            [tag-admin] => admin
            [tag-advanced] => advanced
            [tag-custom] => custom
            [tag-custom-field] => custom field
            [tag-edit] => edit
            [tag-field] => field
            [tag-file] => file
            [tag-image] => image
            [tag-magic-fields] => magic fields
            [tag-matrix] => matrix
            [tag-more-fields] => more fields
            [tag-post] => Post
            [tag-repeater] => repeater
            [tag-simple-fields] => simple fields
            [tag-text] => text
            [tag-textarea] => textarea
            [tag-type] => type
        )

    [tested] => 3.5.1
    [total_days] => 539
    [total_downloads] => 763012
    [type] => plugin
    [updated] => 2013-04-30
    [version] => 4.1.0
)

【讨论】:

以上是关于从 WordPress 插件目录获取插件版本号的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress-如何禁用插件更新

Wordpress-如何禁用插件更新

防止在错误版本的 PHP 或 WP 上激活 Wordpress 插件

SAE+wordpress邮箱问题,WP MAIL STMP插件配置但无效解决的方法

Wordpress 插件如何要求安装最低 Wordpress 版本?

cordova 跨平台APP版本升级