作為WordPress主題開發(fā)者,如果你的主題的某些功能需要借助某些插件才能實現(xiàn),那你需要提醒主題使用者安裝這些插件。在倡萌看來,最合理的提醒方法,就是啟用主題后,在后臺頂部提醒安裝,如下圖所示:

在 如何在WordPress后臺頂部添加錯誤提醒信息或升級提醒信息 中已經(jīng)介紹了通過 admin_notices 掛鉤 顯示提醒信息的方法,那么接我們只需要借助 is_plugin_active() 函數(shù)來檢測所需的插件是否已安裝并啟用,如果沒有安裝就進行提醒。
is_plugin_active() 函數(shù)簡介
is_plugin_active() 函數(shù)是專門用來檢測插件是否已經(jīng)安裝并啟用的,使用的方法很簡單,只需要添加對應(yīng)的插件的主文件路徑即可:
if(!is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ))
{
echo '需要顯示的內(nèi)容';
}
上面的代碼的作用就是:如果沒有啟用 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





路勁 => 路徑
已修正,謝謝提醒
這個功能很實用啊
我不太喜歡要用插件的主題,最好多多集成