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

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

我们只需要借助 is_plugin_active() 函数来检测所需的插件是否已安装并启用,如果没有安装就进行提醒。

is_plugin_active() 函数简介

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

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

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

提示安装必要插件

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

  1.     add_action('admin_notices', 'showAdminMessages');
  2.  
  3.     function showAdminMessages()
  4.  
  5.     {
  6.  
  7.     $plugin_messages = array();
  8.  
  9.     include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  10.  
  11.     // Download the Yoast WordPress SEO plugin
  12.  
  13.     if(!is_plugin_active( 'wordpress-seo/wp-seo.php' ))
  14.  
  15.     {
  16.  
  17.     $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>.';
  18.  
  19.     }
  20.  
  21.     // Download the Disqus comment system
  22.  
  23.     if(!is_plugin_active( 'disqus-comment-system/disqus.php' ))
  24.  
  25.     {
  26.  
  27.     $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>.';
  28.  
  29.     }
  30.  
  31.     // Download the WordPress popular posts plugin
  32.  
  33.     if(!is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ))
  34.  
  35.     {
  36.  
  37.     $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>.';
  38.  
  39.     }
  40.  
  41.     if(count($plugin_messages) > 0)
  42.  
  43.     {
  44.  
  45.     echo '
  46.  
  47.     <div id="message" class="error">';
  48.  
  49.     foreach($plugin_messages as $message)
  50.  
  51.     {
  52.  
  53.     echo '
  54.  
  55.     <strong>'.$message.'</strong>
  56.  
  57.     ';
  58.  
  59.     }
  60.  
  61.     echo '</div>
  62.  
  63.     ';
  64.  
  65.     }
  66.  
  67.     }
© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
相关推荐
  • 暂无相关文章
评论 抢沙发