在您的网站上展示 WordPress.org 插件的信息

2023-12-01 0 1,002

在您的网站上展示 WordPress.org 插件的信息

在本文的第一部分中,我们讨论了如何使用内置函数与 WordPress.org 进行通信并检索插件详细信息。

在本教程中,我们将把理论付诸实践,创建一个简单的插件,该插件将允许我们使用简码在我们的 WordPress 网站上显示 WordPress.org 上托管的任何插件的详细信息。 p>


开始使用

我假设您是插件开发人员并且了解基础知识,但如果有疑问,我建议阅读以下两篇文章:

  • 开发 WordPress 插件的两种方法:函数式编程
  • 开发 WordPress 插件的两种方法:面向对象编程

我们在做什么?

通过这个插件,我们想要创建一个短代码,例如 [mpi slug=\’my-plugin-information\’ field=\’version\’] ,它可以接受两个属性:“slug”和“field”,然后基于然后,我们检索并显示 WordPress.org 存储库中托管的任何插件的信息。

创建插件库

让我们首先在 wp-content/plugins 目录中创建一个名为 my-plugin-information 的文件夹。在其中创建一个名为 my-plugin-info.php 的文件,并将以下代码粘贴到其中:

<?php
/*
Plugin Name: My Plugin Info
Plugin URI: https://myplugininfo.com
Description: Communicate with WordPress.org Plugins API to retrive your Plugin Information
Version: 0.1
Author: Harish
Author Email: mye@email.com
License: GPL3
*/
if ( ! defined( \’ABSPATH\’ ) ) exit; // Exit if accessed directly

if ( ! class_exists( \’DOT_MyPluginInfo\’ ) )
{

class DOT_MyPluginInfo {

/**
* Constructor
*/
function __construct() {

//Hook up to the init action
add_action( \’init\’, array( &$this, \’init_my_plugin_info\’ ) );

}

/**
* Runs when the plugin is initialized
*/
function init_my_plugin_info() {

// Register the shortcode [mpi slug=\’my-plugin-info\’ field=\’version\’]
add_shortcode( \’mpi\’, array( &$this, \’render_mpi\’ ) );

}

function render_mpi($atts) {

}

} // end class

new DOT_MyPluginInfo();
}
?>

我们做了什么?

在上面的代码中,我们创建并初始化了插件类 DOT_MyPluginInfo。其中包含任何插件的通用块,例如 __construct() 方法。

函数 init_my_plugin_info 与 init 操作挂钩,以便它在加载 WordPress 之后但在发送任何标头之前运行。在函数 init_my_plugin_info 中,我们使用 add_shortcode 函数注册了我们的短代码。

注意:要了解有关 add_shortcode 的更多信息,请查看 Codex。

上面的插件现在有足够的代码可以被 WordPress 从插件仪表板识别。如果您已经按照说明创建了文件,您现在可以访问插件页面并激活此插件。


设置简码

由于我们希望灵活地选择要显示有关插件的信息,因此我们创建了一个具有两个属性的短代码。第一个称为“slug”将用于指定需要检索哪个插件的数据。第二个属性“field”将用于指定我们需要显示的插件的具体信息。

例如,如果我们想显示该插件的下载次数,我们只需在帖子编辑器下方添加文本,最终结果应该是“下载了 100 次”之类的内容。

Downloaded [mpi slug=\’my-plugin-information\’ field=\’downloaded\’] times.

使用 add_shortcode 我们注册了我们的短代码,以便每当在帖子内容中找到短代码时,都会调用函数 render_mpi() 来处理它。从现在开始,其余代码将放置在该函数内来处理我们的短代码。

使用 render_mpi() 处理短代码

要显示插件信息,我们首先需要处理短代码以获取属性。在 render_api 函数中添加以下代码:

// get our variable from $atts
extract( shortcode_atts( array(
\’slug\’ => \’\’, //foo is a default value
\’field\’ => \’\’
), $atts ) );

这会提取两个属性“slug”和“field”(如果提供)。在继续之前,我们首先检查“slug”和“field”的值是否存在,如果不存在,则停止进一步处理。

/**
* Check if slug exists
*/
if ( ! $slug ) {
return false;
}

/**
* Check if field exists
* Return value based on the field attribute
*/
if ( ! $field ) {
return false;
} else {

} // $field check

上面的代码将检查“slug”是否存在,如果不存在,则返回 false。如果“slug”确实存在,它将继续检查“field”属性。由于我们只是创建一个短代码来显示有关插件的特定信息,因此在进一步处理之前检查这两个属性是否存在将节省对 WordPress.org 插件 API 的不必要的调用。

现在,如果短代码中提供了“slug”和“field”属性的值,我们将首先清理这两个值。

// Sanitize attributes
$slug = sanitize_title( $slug );
$field = sanitize_title( $field );

在瞬态中存储插件数据

为了避免每次加载包含此短代码的页面时都向 WordPress.org 发送请求,我们需要在本地保存插件信息。这样,如果您放置了多个短代码来显示同一插件的不同详细信息,我们就可以通过显示您网站上本地保存的信息中的数据来加快这一过程。

但是如果插件更新并且我们继续显示旧数据怎么办?为了解决这个问题,最快的选择是使用 Transients API 保存我们的个人插件数据并设置到期日期数据。

另一个问题是,如果您有正在检索有关不同插件的数据的短代码。如果我们使用单个临时名称存储它们,结果可能会出乎意料。为了解决这个问题,我们使用“slug”属性为保存的瞬态提供一个唯一的名称。

为什么要经历这一切?

  • 单独保存每个插件的信息
  • 减少向 WordPress.org 发出的请求
  • 通过直接从您自己的网站提供数据来更快地加载数据

让我们首先创建一个变量 $mpi_transient_name 来保存基于“slug”属性的唯一瞬态名称。

// Create a empty array with variable name different based on plugin slug
$mpi_transient_name = \’mpi-\’ . $slug;

接下来我们检查瞬态是否已经存在:

/**
* Check if transient with the plugin data exists
*/
$mpi_info = get_transient( $mpi_transient_name );

如果瞬态存在,我们将继续根据“field”属性显示数据,否则我们使用 plugins_api 连接到 WordPress.org 并请求插件信息。

if ( empty( $mpi_info ) ) {

/**
* Connect to WordPress.org using plugins_api
* About plugins_api –
* https://code.tutsplus.com/tutorials/communicating-with-the-wordpress-org-plugin-api–wp-33069
*/
require_once( ABSPATH . \’wp-admin/includes/plugin-install.php\’ );
$mpi_info = plugins_api( \’plugin_information\’, array( \’slug\’ => $slug ) );

// Check for errors with the data returned from WordPress.org
if ( ! $mpi_info or is_wp_error( $mpi_info ) ) {
return false;
}

// Set a transient with the plugin data
// Use Options API with auto update cron job in next version.
set_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );

}

在上面的代码中,我们做了三件事:

  • 我们连接到 WordPress.org 并请求插件信息。然后该请求被保存在名为 $mpi_info 的变量中
  • 然后我们进行错误检查,以确保返回的数据是否没有错误
  • 最后,我们创建了一个过期日期为一小时的新瞬态
  • 现在,如果 slug 属性的值为“my-plugin-information”,那么存储插件信息的瞬态名称将为“mpi-my-plugin-information”。

    注意:要了解有关 plugins_api 的更多信息,请参阅本系列的第一篇文章,如本文顶部所示。

    显示插件信息

    最后一步涉及根据“field”属性的值返回特定信息。为此,我们只需使用单独的检查即可。

    if ( $field == \”downloaded\” ) {
    return $mpi_info->downloaded;
    }

    if ( $field == \”name\” ) {
    return $mpi_info->name;
    }

    if ( $field == \”slug\” ) {
    return $mpi_info->slug;
    }

    if ( $field == \”version\” ) {
    return $mpi_info->version;
    }

    if ( $field == \”author\” ) {
    return $mpi_info->author;
    }

    if ( $field == \”author_profile\” ) {
    return $mpi_info->author_profile;
    }

    if ( $field == \”last_updated\” ) {
    return $mpi_info->last_updated;
    }

    if ( $field == \”download_link\” ) {
    return $mpi_info->download_link;
    }


    总结

    完整的插件代码:

    <?php
    /*
    Plugin Name: My Plugin Information
    Plugin URI: https://code.tutsplus.com
    Description: Communicate with WordPress.org Plugins API to retrive your Plugin Information
    Version: 0.1.1
    Author: Harish
    Author Email: me@email.com
    License:

    Copyright 2013 Harish

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 3, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

    */
    if ( ! defined( \’ABSPATH\’ ) ) exit; // Exit if accessed directly

    if ( ! class_exists( \’DOT_MyPluginInfo\’ ) )
    {

    class DOT_MyPluginInfo {

    /**
    * Constructor
    */
    function __construct() {

    //Hook up to the init action
    add_action( \’init\’, array( &$this, \’init_my_plugin_info\’ ) );

    }

    /**
    * Runs when the plugin is initialized
    */
    function init_my_plugin_info() {

    // Register the shortcode [mpi slug=\’my-plugin-info\’ field=\’version\’]
    add_shortcode( \’mpi\’, array( &$this, \’render_mpi\’ ) );

    }

    function render_mpi($atts) {

    // get our variable from $atts
    extract(shortcode_atts(array(
    \’slug\’ => \’\’, //foo is a default value
    \’field\’ => \’\’
    ), $atts));

    /**
    * Check if slug exists
    */
    if ( ! $slug ) {
    return false;
    }

    /**
    * Check if field exists
    * Return value based on the field attribute
    */
    if ( ! $field ) {

    return false;

    } else {

    // Sanitize attributes
    $slug = sanitize_title( $slug );
    $field = sanitize_title( $field );

    // Create a empty array with variable name different based on plugin slug
    $mpi_transient_name = \’mpi\’ . $slug;

    /**
    * Check if transient with the plugin data exists
    */
    $mpi_info = get_transient( $mpi_transient_name );

    if ( empty( $mpi_info ) ) {

    /**
    * Connect to WordPress.org using plugins_api
    * About plugins_api –
    * https://code.tutsplus.com/tutorials/communicating-with-the-wordpress-org-plugin-api–wp-33069
    */
    require_once( ABSPATH . \’wp-admin/includes/plugin-install.php\’ );
    $mpi_info = plugins_api( \’plugin_information\’, array( \’slug\’ => $slug ) );

    // Check for errors with the data returned from WordPress.org
    if ( ! $mpi_info or is_wp_error( $mpi_info ) ) {
    return false;
    }

    // Set a transient with the plugin data
    // Use Options API with auto update cron job in next version.
    set_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );

    }

    if ( $field == \”downloaded\” ) {
    return $mpi_info->downloaded;
    }

    if ( $field == \”name\” ) {
    return $mpi_info->name;
    }

    if ( $field == \”slug\” ) {
    return $mpi_info->slug;
    }

    if ( $field == \”version\” ) {
    return $mpi_info->version;
    }

    if ( $field == \”author\” ) {
    return $mpi_info->author;
    }

    if ( $field == \”author_profile\” ) {
    return $mpi_info->author_profile;
    }

    if ( $field == \”last_updated\” ) {
    return $mpi_info->last_updated;
    }

    if ( $field == \”download_link\” ) {
    return $mpi_info->download_link;
    }

    } // $field check

    } // render_mpi()

    } // end class
    new DOT_MyPluginInfo();

    }

    ?>

    此插件代码可在 GitHub 上找到,您也可以从 WordPress.org 下载


    付诸行动

    现在您只需转到帖子编辑器并添加一个短代码,例如:

    Downloaded [mpi slug=\’my-plugin-information\’ field=\’downloaded\’] times.

    它会显示:

    Downloaded 10 times.

    显示有关插件的其他信息的示例简码

    通过替换“field”属性的值,您可以显示不同的信息,例如:

    • 插件名称:[mpi slug=\’my-plugin-information\’ field=\’name\’]
    • 插件版本:[mpi slug=\’my-plugin-information\’ field=\’version\’]
    • 插件 Slug:[mpi slug=\’my-plugin-information\’ field=\’slug\’]
    • 插件作者(返回名称和链接):[mpi slug=\’my-plugin-information\’ field=\’author\’]
    • 作者简介(返回个人资料地址):[mpi slug=\’my-plugin-information\’ field=\’author_profile\’]
    • 最后更新:[mpi slug=\’my-plugin-information\’ field=\’last_updated\’]
    • 下载链接:[mpi slug=\’my-plugin-information\’ field=\’download_link\’]

    改进

    为了简单起见,我使用瞬态来保存插件信息。然而,瞬态从来就不是用来保存重要数据的。另一种方法是使用选项 API、add_options() 或作为 post meta 保存插件数据,然后安排一个 cron 任务每小时更新一次数据。


    接下来做什么?

    使用 plugins_api,我们已经演示了通信和检索 WordPress.org 上托管的任何插件的信息是多么容易。

    您可能还想查看其他插件,例如 Plugin Info(也使用 plugins_api 和 I Make Plugins,看看它们如何完成相同的任务。

    以上就是在您的网站上展示 WordPress.org 插件的信息的详细内容,更多请关注悠久资源其它相关文章!

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 Wordpress教程 在您的网站上展示 WordPress.org 插件的信息 https://www.u-9.cn/jiaocheng/wordpress-jiaocheng/16871.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务