我试图在Wordpress的首页上禁用一个插件,使其更轻一些。我只希望在显示首页时禁用它。
我在wp-content / mu-plugins /中放了这个工作代码,它在除指定的/ subpage /之外的所有页面上禁用了一个插件。这很棒,但这并不是我想要的。
如何仅在查看首页时更改此代码以禁用插件?
这是代码:
add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/subpage/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
$key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );
if ( false !== $key ) {
unset( $plugins[$key] );
}
}
return $plugins;
}
我还发现了另一个适用于其他页面的代码...。但是,由于相同的原因,我无法弄清楚如何使其适用于首页,仅适用于子页面**
$listener_term = '/subpage/';
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';
// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
add_filter( 'option_active_plugins', 'api_request_disable_plugin' );
}
function api_request_disable_plugin( $plugins ) {
$plugins_not_needed = array(
'backupwordpress/backupwordpress.php',
'wordfence/wordfence.php',
'contact-form-7-to-database-extension/contact-form-7-db.php',
'contact-form-7/wp-contact-form-7.php',
'wp-piwik/wp-piwik.php',
'simple-responsive-slider/simple-responsive-slider.php',
'google-sitemap-plugin/google-sitemap-plugin.php',
'category-page-icons/menu-compouser.php',
'easy-fancybox/easy-fancybox.php',
'business-owner-switch/business-owner-switch.php',
'wordpress-seo/wp-seo.php'
);
foreach ( $plugins_not_needed as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
return $plugins;
}
有任何想法吗?
我想我应该提到:
无论我如何尝试,它们都不适合首页/主页。
我没意识到您只希望它出现在首页上-代码段表明您也希望它出现在其他页面上。
之前我曾建议您可以使用is_home或is_front_page,但在此阶段我们尚未加载它们-因此,这里的另一种方法是加载REQUEST_URI并查看其是否为空白。如果为空白,则假定我们位于主页上-为了满足可能不是根目录的安装,我们将URL与站点的相对主页位置进行了比较-为了增加安全性,我们将其封装在htmlspecialcharacters中(可能没有必要) )。
也许尝试这样的事情(看,这可能不是最好的方法,但对我当然有效):
add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
$key = array_search( 'was-this-helpful-pro/was-this-helpful-pro.php' , $plugins );
if ( false !== $key ) {
unset( $plugins[$key] );
}
}
return $plugins;
}
如果要对多个插件执行此操作,则可以将代码更改为以下内容:
add_filter( 'option_active_plugins', 'lg_disable_plugin' );
function lg_disable_plugin($plugins){
if (htmlspecialchars(trim(wp_make_link_relative(get_site_url()),'/')) == htmlspecialchars(trim($_SERVER['REQUEST_URI'],'/'))) {
$plugins_not_needed = array ('was-this-helpful-pro/was-this-helpful-pro.php',
'pluginfolder/plugin-name.php');
foreach ( $plugins_not_needed as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}
}
return $plugins;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句