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

属于自己的风景,从来不曾错过;不是自己的风景,永远只是路过。天地太大,人太渺小,不是每一道亮丽的风景都能拥有。一辈子,只求有一道令自己流连忘返,不离不弃的风景就已足够。人生的风景,是物也是人。陪自己到最后的,才是自己的风景。

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

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

is_plugin_active() 函数简介

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

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

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 中添加类似代码,就可以达到本文配图的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 rel="nofollow noopener noreferrer" href="http://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 rel="nofollow noopener noreferrer" href="http://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 rel="nofollow noopener noreferrer" href="http://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>
';
	}
}

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 rel="nofollow noopener noreferrer" href="http://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 rel="nofollow noopener noreferrer" href="http://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 rel="nofollow noopener noreferrer" href="http://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

到此这篇关于如何自动提醒WordPress主题如何使用者安装必要插件就介绍到这了。心绷得太紧,就容易烦躁。尝试给自己一份松弛的柔软,对暂时不顺心的事,抱以理解与包容。当你的心不再那么紧绷,你会发现你与他人之间多了几分宽和,与世界也会多了几分契合。更多相关如何自动提醒WordPress主题如何使用者安装必要插件内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?