增强你的主题:集成Envato WordPress工具包插件

Wordpress8个月前发布 SUYEONE
1.4K 0 0

As WordPress theme authors on ThemeForest, our goal is to satisfy customers by occasionally providing bug fixes and enhancements to our themes. A key challenge we face is notifying users when updates are avAIlable for download. In the past, each of us had to code our own theme-specific update notification systems. Although there’s now a checkbox in Envato Market to enable project update notifications, users still need to manually activate it for each theme and perform updates indiVidually.

Wouldn’t it be more convenient if update notifications appeared directly in the WordPress admin dashboard, allowing for immediate updates? Fortunately, we now have the Envato WordPress Toolkit plugin and library to address this issue. In this Series. you’ll learn how to integrate these tools into your themes.

In this tutorial, we’ll implement the Envato WordPress Toolkit plugin and library in our theme. When the theme is activated, users will be prompted to install and activate the toolkit plugin. Once activated, the theme will regularly check for updates. If an update is found, a notification will guide users to the plugin to update their theme.

The tutorial is divided into two parts:

1. Part 1 – Integrate the TGM Plugin Activation class to require the Envato WordPress Toolkit plugin when using our theme.
2. Part 2 – Implement the Envato WordPress Toolkit library in our theme to allow checking and updating to new theme versions.

To avoid confusion, let’s clarify the two forms of the Envato WordPress Toolkit:

– Toolkit Plugin: This is a standalone plugin that any Envato customer can install on their WordPress site. After activation, all previously purchased themes and theme updates can be downloaded directly from the admin area.
– Toolkit Library: Authors can include code in their WordPress themes, enabling the theme to check for theme version updates and self-update using the Envato Marketplace API.

### Step 1: Include Required Documentation

First, include some documentation in your project. We’ll bundle the Toolkit plugin with our theme and use TGM Plugin Activation to install and activate it.

1. Download TGM Plugin Activation and place the main class script in your theme’s `inc` folder. The path should be: `my-theme/inc/class-tgm-plugin-activation.php`.
2. Download the Envato WordPress Toolkit plugin ZIP file and place it in a new folder named `plugins` within your theme. The path should be: `my-theme/plugins/envato-wordpress-toolkit-master.zip`.

**Note:** You can change the file locations as needed, or download the complete source code from the link at the top of this article.

### Step 2: Hooking up TGM

Now that we have the necessary files, let’s start coding. Include the TGM Plugin Activation class in `functions.php` and hook it to a custom WordPress Action. Here, we’ll set up some TGM settings and define the plugins to include.

“`php
/**
* Load TGM Plugin Activation class to notify users to install Envato WordPress Toolkit plugin
*/
require_once get_template_directory() . ‘/inc/class-tgm-plugin-activation.php’;

function tgmpa_register_toolkit() {
// Code here
}
add_action(‘tgmpa_register’, ‘tgmpa_register_toolkit’);
“`

### Step 3: Specify the Toolkit Plugin

Next, configure the parameters required to include the Toolkit plugin. Within the `tgmpa_register_toolkit` function, add the following code. Change the `source` parameter if you specified a different plugin folder in Step 1.

“`php
// Specify Envato Toolkit plugin
$plugins = array(
array(
‘name’ => ‘Envato WordPress Toolkit’,
‘slug’ => ‘envato-wordpress-toolkit-master’,
‘source’ => get_template_directory() . ‘/plugins/envato-wordpress-toolkit-master.zip’,
‘required’ => true,
‘version’ => ‘1.5’,
‘force_activation’ => true,
‘force_deactivation’ => false,
‘external_url’ => ”,
),
);
“`

You can also add more plugins by adding adDiTional arrays to the `$plugins` variable.

### Step 4: Configure TGM and Set TGM Options

Still within the `tgmpa_register_toolkit` function, add the following code below the previous step to configure TGM. I won’t explain each setting in detail; for more information, visit the TGM Plugin Activation Website, which explains every detail.

“`php
// i18n for translation purposes
$theme_text_domain = ‘default’;

// TGM configuration
$config = array(
‘domain’ => $theme_text_domain,
‘default_path’ => ”,
‘parent_menu_slug’ => ‘admin.php’,
‘parent_url_slug’ => ‘admin.php’,
‘menu’ => ‘install-required-plugins’,
‘has_notices’ => true,
‘is_automatic’ => true,
‘message’ => ”,
‘strings’ => array(
// … (all the strings)
),
);

// Change $theme_text_domain to the text domain you’re using, or keep the default value.
“`

### Step 5: Initialize TGM

Lastly, initialize TGM just before the end of the `tgmpa_register_toolkit` function.

“`php
tgmpa($plugins, $config);
“`

Save your `functions.php`. Now, try activating your theme. If the Envato WordPress Toolkit plugin isn’t installed or activated, you should see a notification similar to the one shown below:

With what we’ve covered so far, users can indeed update their themes from within the WordPress admin. However, they’ll only see updates in the Toolkit management panel. In the second part of this tutorial, you’ll learn how to integrate the Envato WordPress Toolkit library and display admin notifications when theme updates are available on ThemeForest.

© 版权声明

相关文章

暂无评论

暂无评论...
☺一键登录开启个人书签等功能!