外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>如何自动提醒WordPress主题使用者安装必要插件

如何自动提醒WordPress主题使用者安装必要插件

作为WordPress主题开发者,如果你的主题的某些功能需要借助某些插件才能实现,那你需要提醒主题使用者安装这些插件。在倡萌看来,最合理的提醒方法,就是启用主题后,在后台顶部提醒安装,如下图所示:

wpdaxue.com-201303364

如何在WordPress后台顶部添加错误提醒信息或升级提醒信息 中已经介绍了通过 admin_notices 挂钩 显示提醒信息的方法,那么接我们只需要借助 is_plugin_active() 函数来检测所需的插件是否已安装并启用,如果没有安装就进行提醒。

is_plugin_active() 函数简介

is_plugin_active() 函数是专门用来检测插件是否已经安装并启用的,使用的方法很简单,只需要添加对应的插件的主文件路径即可:

if(!is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ))
	{
	echo '需要显示的内容';
	}

上面的代码的作用就是:如果没有启用 WordPress Popular Posts,就显示一段提醒文字。’wordpress-popular-posts/wordpress-popular-posts.php’ 就是 WordPress Popular Posts 插件的主文件的路径。

提示安装必要插件

只需要在主题的 functions.php 中添加类似代码,就可以达到本文配图的效果:

add_action('admin_notices', 'showAdminMessages');
function showAdminMessages()
{
	$plugin_messages = array();
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	// Download the Yoast WordPress SEO plugin
	if(!is_plugin_active( 'wordpress-seo/wp-seo.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the Yoast WordPress SEO plugin, <a href="https://wordpress.org/extend/plugins/wordpress-seo/">download it from here</a>.';
	}
	// Download the Disqus comment system
	if(!is_plugin_active( 'disqus-comment-system/disqus.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the Disqus comment system plugin, <a href="https://wordpress.org/extend/plugins/disqus-comment-system/">download it from here</a>.';
	}
	// Download the WordPress popular posts plugin
	if(!is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the WordPress Popular Post plugin, <a href="https://wordpress.org/extend/plugins/wordpress-popular-posts/">download it from here</a>.';
	}
	if(count($plugin_messages) > 0)
	{
		echo '
<div id="message" class="error">';
			foreach($plugin_messages as $message)
			{
				echo '
<strong>'.$message.'</strong>
';
			}
		echo '</div>
';
	}
}

参考资料:http://www.paulund.co.uk/theme-users-required-plugins

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

如何在WordPress后台顶部添加错误提醒信息或升级提醒信息

2013-3-7 6:44:00

WordPress开发

WordPress函数:get_extended(获取扩展信息)

2013-3-19 20:40:00

4 条回复 A文章作者 M管理员
  1. 路劲 => 路径

    • 已修正,谢谢提醒

  2. 这个功能很实用啊

  3. 我不太喜欢要用插件的主题,最好多多集成

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索