### [使用 Envato WordPress 工具包增强您的主题:库](https://www.prosaas.cn/article/5532) **Published:** 2023-09-02T04:41:01 **Author:** 5201314 **Excerpt:** 在本教程的最后一部分中,我们学习了如何使用 TGM 插件激活类来在任何时候都需要 Envato WordPre…  在本教程的最后一部分中,我们学习了如何使用 TGM 插件激活类来在任何时候都需要 Envato WordPress 工具包插件我们的主题正在使用中。该插件允许用户在管理员中安装和更新购买的主题。 下一部分将教您如何实现 Envato WordPress 工具包库,以便我们可以使用 Envato Marketplace API 定期检查我们的主题何时有可用更新。 当有更新可用时,我们将在管理员中显示通知,并引导用户访问插件进行更新。 * * * ## 1. 包括工具包库 我们首先需要将工具包库包含在我们的项目中。下载 Envato WordPress 工具包库 ZIP 文件。解压缩并将文件夹 **envato-wordpress-toolkit-library** 复制到主题中的 **inc** 文件夹中。您最终应该得到这些路径: - **mytheme/inc/envato-wordpress-toolkit-library/class-envato-protected-api.php** - **mytheme/inc/envato-wordpress-toolkit-library/class-envato-wordpress-theme-upgrader.php** _**注意:**您可以更改上述文件的位置以满足您的需要。或者,您可以从本文顶部的下载链接下载完整源代码。_ * * * ## 2. 管理员挂钩函数 现在我们可以开始编码了。我们将挂钩 `admin_init` 操作。将以下代码添加到您的 **functions.php** 中: /\*\* \* Load the Envato WordPress Toolkit Library check for updates \* and direct the user to the Toolkit Plugin if there is one \*/ function envato\_toolkit\_admin\_init() { // Include the Toolkit Library include\_once( get\_template\_directory() . '/inc/envato-wordpress-toolkit-library/class-envato-wordpress-theme-upgrader.php' ); // Add further code here } add\_action( 'admin\_init', 'envato\_toolkit\_admin\_init' ); * * * ## 3. 使用工具包插件信息 工具包库需要 Envato 用户名和 API 密钥才能工作。由于我们在上一个教程中需要 Toolkit 插件,因此我们可以使用在其设置中找到的用户名和 API 密钥字段的输入值。如果这些字段未填写,我们可以显示一条通知,要求用户在 Toolkit 插件中输入它们。 // Use credentials used in toolkit plugin so that we don't have to show our own forms anymore $credentials = get\_option( 'envato-wordpress-toolkit' ); if ( empty( $credentials\['user\_name'\] ) || empty( $credentials\['api\_key'\] ) ) { add\_action( 'admin\_notices', 'envato\_toolkit\_credentials\_admin\_notices' ); return; } 我们需要在外部添加相应的钩子函数来显示我们的管理通知: /\*\* \* Display a notice in the admin to remind the user to enter their credentials \*/ function envato\_toolkit\_credentials\_admin\_notices() { $message = sprintf( \_\_( "To enable theme update notifications, please enter your Envato Marketplace credentials in the %s", "default" ), "Envato WordPress Toolkit Plugin" ); echo "
{$message}
{$message}