最受欢迎的插件已添加到WordPress中。组织API。3.5中有一个新功能,允许您从插件安装程序访问收藏夹。
看见http://core.trac.wordpress.org/ticket/22002 有关如何在core中使用它的信息。
API允许您检索包含每个插件的对象,该插件的名称、描述、作者、分级、最后更新日期、更改日志、稳定版本与wp版本一起检索对象http://api.wordpress.org/plugins/info/1.0/ 使用wp\\u remote\\u post传递一组参数,包括“query\\u plugins”操作和从中检索收藏夹的wp dot org用户名。
$request = wp_remote_post(\'http://api.wordpress.org/plugins/info/1.0/\', array( \'timeout\' => 15, \'body\' => array(\'action\' => $action, \'request\' => serialize($args))) );
在创建一个干净的对象之前,需要进行一些错误处理和其他解析。下面是一个示例函数,它将返回一个保存所有插件详细信息的干净对象。
function api( $action, $args ) {
if ( is_array( $args ) )
$args = (object) $args;
$request = wp_remote_post(\'http://api.wordpress.org/plugins/info/1.0/\', array( \'timeout\' => 15, \'body\' => array(\'action\' => $action, \'request\' => serialize($args))) );
if ( is_wp_error($request) ) {
$res = new WP_Error(\'plugins_api_failed\', __( \'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.\' ), $request->get_error_message() );
} else {
$res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
if ( ! is_object( $res ) && ! is_array( $res ) )
$res = new WP_Error(\'plugins_api_failed\', __( \'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.\' ), wp_remote_retrieve_body( $request ) );
}
return apply_filters( \'c3m_favorite_results\', $res, $action, $args );
}
用法此示例用法将为您提供一个无序的最喜爱插件列表,以及指向dot org上插件的链接、指向作者uri和星级的链接。
$api_data = api( \'query_plugins\', array( \'user\' => \'my_dot_org_username\' ) );
$api_plugins = $api_data->plugins;
echo \'<ul class="c3m-favorites">\';
foreach( $api_plugins as $plugin ) {
$name = $plugin->name; ?>
<li><strong><a target="_blank" href="http://wordpress.org/extend/plugins/<?php echo $plugin->slug ?>/"><?php echo esc_html( $name ); ?></a></strong><br>
<div class="star-holder" title="<?php printf( _n( \'(based on %s rating)\', \'(based on %s ratings)\', $plugin->num_ratings ), number_format_i18n( $plugin->num_ratings ) ); ?>">
<div class="star star-rating" style="width: <?php echo esc_attr( str_replace( \',\', \'.\', $plugin->rating ) ); ?>px"></div></div>
<em><?php _e(\'By: \') ?></em> <?php echo links_add_target( $plugin->author, \'_blank\' ). \'<br>\'; ?>
</li><?php
}
echo \'</ul>\';
结果
来自我最喜欢的插件的小部件屏幕截图小部件插件:http://wordpress.org/extend/plugins/favorite-plugins-widget/