【问题标题】:Deregister all styles and scripts from a certain Wordpress plugin从某个 Wordpress 插件中注销所有样式和脚本
【发布时间】:2016-06-20 05:13:00
【问题描述】:

我想 wp_deregister 来自某个插件的所有样式和脚本。

例如

$plugin = getAllStyles('PLUGIN-NAME');      
foreach ($plugin as $value) {
    wp_deregister_style($value);
}

$plugin = getAllScripts('PLUGIN-NAME');      
foreach ($plugin as $value) {
    wp_deregister_script($value);
}

这可能吗?

【问题讨论】:

    标签: php wordpress codex


    【解决方案1】:

    是的,MU-PLUGINs 可以查看请求 url 并有选择地禁用插件。这是可能的,因为 MU 插件是在常规插件之前加载的。

    1) 在/wp-content/mu-plugins/Removeplugin.php 中创建新的 Mu-plugin

    2) 添加此代码

    /*
        Plugin Name: plugins remover
        Plugin URI: 
        Description:  Removes  plugin
        Author: Author name 
        Version: 1.0
        Author URI: 
    */
    

    3) 现在添加动作钩子option_active_plugins

    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
    function disable_plugin_callback_fun($plugins){
         $key = array_search( 'plugin_directory_name/plugin_filename.php' , $plugins );
         if ( false !== $key ) unset( $plugins[$key] );
    }
    

    如果您删除联系表格 7 插件 css 和 js 的示例

    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
        function disable_plugin_callback_fun($plugins){
            $key = array_search(  'contact-form-7/wp-contact-form-7.php', $plugins );
        if ( false !== $key ) { unset( $plugins[$key] );}
    
    }
    

    注意:此操作挂钩禁用站点插件,因此我建议从所选页面加载插件,如以下代码检查当前页面。

    $current_page = add_query_arg( array() );
    add_filter( 'option_active_plugins', 'disable_plugin_callback_fun' );
            function disable_plugin_callback_fun($plugins){
             $current_page = add_query_arg( array() );   
             if($current_page!='contact'){//contact us page url where to load plugin only 
             $key = array_search(  'contact-form-7/wp-contact-form-7.php', $plugins );
            if ( false !== $key ) { unset( $plugins[$key] );}
            }
        }
    

    更多信息请查看https://wordpress.stackexchange.com/questions/114345/load-plugin-selectively-for-pages-or-posts-etc

    编辑

    如果您只需要从插件中删除所有样式和脚本。需要找到所有的css和jquery,并一一放入bellow函数中。

    没有其他方法可以从插件中删除所有样式和脚本

    注意:仅当插件用于添加样式或使用 wp_enqueue_style() 和 wp_enqueue_script() 的 js 时才有效,否则此代码不起作用

    add_action('wp_print_scripts', 'disable_pluginsjs_selectively', 100 );
    function disable_pluginsjs_selectively( ) {
       wp_deregister_script( Name of the enqueued jQuery);
       wp_dequeue_style('Name of the enqueued stylesheet'); 
    }
    

    【讨论】:

    • 但是代码会完全禁用插件(在特殊页面上)。我只想从插件中删除所有样式和脚本,而不是插件本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多