TGM Plugin Activation用于主题开发提示用户安装哪些插件

TGM Plugin Activation一个主题和插件开发提示用户安装哪些插件的PHP库,它与Codestar Framework或者Redux Framework框架配合起来开发主题是在何时不过来。

下载TGM Plugin Activation

先到TGM Plugin Activation官网下载

TGM Plugin Activation下载
TGM Plugin Activation下载

TGM Plugin Activation如何集成到主题?

先在WordPress主题目录新建文件夹incfunctions.php,在把上一步下载的压缩文件解压后,重命名为 TGM-Plugin-Activation 放到 inc 文件夹中。

在主题 functions.php 中添加:

require_once(get_template_directory().'/inc/TGM-Plugin-Activation/class-tgm-plugin-activation.php');

add_action( 'tgmpa_register', 'my_theme_register_required_plugins' );

/**
 * Register the required plugins for this theme.
 *
 *  <snip />
 *
 * This function is hooked into tgmpa_init, which is fired within the
 * TGM_Plugin_Activation class constructor.
 */
function my_theme_register_required_plugins() {
	/*
	 * Array of plugin arrays. Required keys are name and slug.
	 * If the source is NOT from the .org repo, then source is also required.
	 */
	$plugins = array(
		array(
			'name'      => 'WordPress SEO Plugin – Rank Math',
			'slug'      => 'seo-by-rank-math',
			'required'  => true,
		),
	);

	/*
	 * Array of configuration settings. Amend each line as needed.
	 *
	 * TGMPA will start providing localized text strings soon. If you already have translations of our standard
	 * strings available, please help us make TGMPA even better by giving us access to these translations or by
	 * sending in a pull-request with .po file(s) with the translations.
	 *
	 * Only uncomment the strings in the config array if you want to customize the strings.
	 */
	$config = array(
		'id'           => 'tgmpa',                 // Unique ID for hashing notices for multiple instances of TGMPA.
		'default_path' => '',                      // Default absolute path to bundled plugins.
		'menu'         => 'tgmpa-install-plugins', // Menu slug.
		'parent_slug'  => 'themes.php',            // Parent menu slug.
		'capability'   => 'edit_theme_options',    // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
		'has_notices'  => true,                    // Show admin notices or not.
		'dismissable'  => true,                    // If false, a user cannot dismiss the nag message.
		'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
		'is_automatic' => false,                   // Automatically activate plugins after installation or not.
		'message'      => '',                      // Message to output right before the plugins table.
		/*
		'strings'      => array(
			'page_title'                      => __( 'Install Required Plugins', 'theme-slug' ),
			'menu_title'                      => __( 'Install Plugins', 'theme-slug' ),
			// <snip>...</snip>
			'nag_type'                        => 'updated', // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
		)
		*/
	);

	tgmpa( $plugins, $config );

}

然后回到WordPress后台就可以看到:

TGM Plugin Activation提示安装插件
TGM Plugin Activation提示安装插件
TGM Plugin Activation提示安装插件
TGM Plugin Activation提示安装插件

如何配置提示安装哪些插件?

注意上一步中的代码:

/*
 * Array of plugin arrays. Required keys are name and slug.
 * If the source is NOT from the .org repo, then source is also required.
 */
$plugins = array(
    array(
        'name'      => 'WordPress SEO Plugin – Rank Math',
        'slug'      => 'seo-by-rank-math',
        'required'  => true,
    ),
);

这是一个配置参数:

  • name 提示安装的插件名称,通常用wp官方展示的名称
  • slug 插件安装后的文件夹
  • required 是否必须安装

我们以Rank Math为示例。先访问WP官方的Rank Math这个插件地址看一看插件的参数。

配置参数
配置参数

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注