Wordpress 在特定页面/帖子上禁用插件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6774424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress Disable Plugin on Specific Pages/Posts
提问by Gabriel
Does anyone know a really effective method for disabling a plugin (that is active) on a specific page? There are some plugins that are not really needed in some pages of the website and they have lot of CSS and JavaScript files that are slowing the loading speed of the website and sometimes might conflict with other files.
有谁知道在特定页面上禁用插件(处于活动状态)的真正有效方法吗?有些插件在网站的某些页面中并不是真正需要的,它们有很多 CSS 和 JavaScript 文件,这些文件会减慢网站的加载速度,有时可能会与其他文件发生冲突。
I know that I can mess with the plugin's code etc. but it's not really effective.
我知道我可以弄乱插件的代码等,但它并不是很有效。
Any ideas?
有任何想法吗?
Thanks in advance!
提前致谢!
回答by Jesse B
try "Plugin Organizer" Wordpress plugin by Jeff Sterup. You have to enable "Selective Plugin Loading" under it's settings (make sure to follow their directions given for enabling it)
试试 Jeff Sterup 的“插件管理器”Wordpress 插件。您必须在其设置下启用“选择性插件加载”(确保按照他们给出的说明启用它)
Then in your post/page editor there is a box below the compose window with tickboxes to disable whichever particular plugin for that page
然后在您的帖子/页面编辑器中,撰写窗口下方有一个带有复选框的框,用于禁用该页面的任何特定插件
took me probably 20+ Google and Wordpress plugins repository searches to finally find a simple solution. Hope it works for you too!
我大概花了 20 多个 Google 和 Wordpress 插件库搜索,最终找到了一个简单的解决方案。希望它也适用于你!
回答by mainpart
这是想法。
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
function lg_disable_cart66_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/store/') === FALSE && !is_admin() ) {
$key = array_search( 'cart66/cart66.php' , $plugins );
if ( false !== $key ) unset( $plugins[$key] );
}
return $plugins;
}
p.s. Some people sayyou might need to put this in mu-plugins
folder.
ps 有人说你可能需要把它放在mu-plugins
文件夹中。
回答by James Hymanson
I know it's old but this thread was exactly what I needed.
我知道它很旧,但这个线程正是我需要的。
The only caveat to numediaweb's answer is that remove action requires the same priority as the add action
对 numediaweb 答案的唯一警告是删除操作需要与添加操作相同的优先级
Hooks in the plugin
插件中的钩子
add_action('wp_print_styles', 'easy_fancybox_enqueue_styles', 999);
add_action('wp_enqueue_scripts', 'easy_fancybox_enqueue_scripts', 999);
add_action('wp_head', 'easy_fancybox', 999);
Code to remove hooks
删除钩子的代码
function remove_easy_fancybox() {
global $post;
$ids = array(12,34,55);
if(in_array($post->ID,$ids)):
remove_action('wp_print_styles', 'easy_fancybox_enqueue_styles', 999);
remove_action('wp_enqueue_scripts', 'easy_fancybox_enqueue_scripts',999);
remove_action('wp_head', 'easy_fancybox', 999);
wp_dequeue_script( 'jquery.fancybox' );
wp_dequeue_script( 'jquery.easing' );
wp_dequeue_script( 'jquery.mousewheel' );
wp_dequeue_script( 'jquery.metadata' );
endif;
}
add_action('wp_head', 'remove_easy_fancybox', 1);
From http://codex.wordpress.org/Function_Reference/remove_action
来自http://codex.wordpress.org/Function_Reference/remove_action
Important:To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.
重要提示:要删除钩子,添加钩子时 $function_to_remove 和 $priority 参数必须匹配。这适用于过滤器和操作。移除失败时不会发出警告。
I've included my if statement to only run the action on specific post ids, thought it might be helpful.
我已经包含了我的 if 语句,只对特定的帖子 ID 运行操作,认为这可能会有所帮助。
回答by numediaweb
First check, if the plugin you want to remove doesn't have an option menu where you set pages to exclude.
首先检查,如果您要删除的插件没有设置要排除的页面的选项菜单。
Second is, look for your plugin action hooks for ex:
其次,寻找你的插件动作钩子,例如:
add_action('wp_head', 'easy_fancybox', 999);
This is an example from easy fancybox plugin that hooks to wordpress header. To remove it, I placed this function in your functions.php
and before any instance of wp_head();
is called:
这是一个简单的fancybox插件的例子,它挂钩到wordpress标题。为了删除它,我将这个函数放在你的functions.php
和wp_head();
被调用的任何实例之前:
function remove_easy_fancybox() {
remove_action('wp_head', 'easy_fancybox_enqueue_styles');
remove_action('wp_head', 'easy_fancybox_enqueue_scripts');
remove_action('wp_head', 'easy_fancybox');
wp_dequeue_script( 'jquery.fancybox' );
wp_dequeue_script( 'jquery.easing' );
wp_dequeue_script( 'jquery.mousewheel' );
wp_dequeue_script( 'jquery.metadata' );
}
add_action('wp_head', 'remove_easy_fancybox', 1);
回答by Jose
you can now use the free plugin Freesoul Deactivate Plugins to deactivate specific plugins on specific pages, posts, custom posts and archives, the settings page is really simple
您现在可以使用免费插件 Freesoul Deactivate Plugins 来停用特定页面、帖子、自定义帖子和档案上的特定插件,设置页面非常简单