启用WordPress主题提醒必须使用的插件

启用WordPress主题提醒必须使用的插件

作者 : 资源客 发布时间: 2019-11-12

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

我们只需要借助 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="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 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 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://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » 启用WordPress主题提醒必须使用的插件