WordPress wp_deQueue_script删除所有其他脚本

时间:2021-05-08 作者:Hung PD

我想重写子主题中的函数以删除特定脚本,这些脚本是在父主题的类中定义的。

Parent 主题functions.php

class Theme_Assets extends Theme_Base {

    /**
     * Hold data for wa_theme for frontend
     * @var array
     */
    private static $theme_json = array();

    /**
     * [__construct description]
     * @method __construct
     */
    public function __construct() {

        // Frontend
        $this->add_action( \'wp_enqueue_scripts\', \'dequeue\', 2 );
        $this->add_action( \'wp_enqueue_scripts\', \'register\' );
        $this->add_action( \'wp_enqueue_scripts\', \'enqueue\' );

        self::add_config( \'uris\', array(
            \'ajax\'    => admin_url(\'admin-ajax.php\', \'relative\')
        ));
    }

    /**
     * Unregister Scripts and Styles
     * @method dequeue
     * @return [type]  [description]
     */
    public function dequeue() {

    }

    /**
     * Register Scripts and Styles
     * @method register
     * @return [type]   [description]
     */
    public function register() {

        $this->script( \'bootstrap\', $this->get_vendor_uri( \'bootstrap/js/bootstrap.min.js\' ), array( \'jquery\' ) );
        $this->script( \'intersection-observer\', $this->get_vendor_uri( \'intersection-observer.js\' ), array( \'jquery\' ) );
        $this->script( \'jquery-lazyload\', $this->get_vendor_uri( \'lazyload.min.js\' ), array( \'jquery\' ) );
        $this->script( \'imagesloaded\', $this->get_vendor_uri( \'imagesloaded.pkgd.min.js\' ), array( \'jquery\' ) );
        $this->script( \'jquery-vivus\', $this->get_vendor_uri( \'vivus.min.js\' ), array( \'jquery\' ) );
        $this->script( \'splittext\', $this->get_vendor_uri( \'greensock/utils/SplitText.min.js\' ), array( \'jquery\' ) );
        $this->script( \'scrollmagic\', $this->get_vendor_uri( \'scrollmagic/ScrollMagic.min.js\' ), array( \'jquery\' ) );
        $this->script( \'jquery-tinycolor\', $this->get_vendor_uri( \'tinycolor-min.js\' ), array( \'jquery\' ) );
        
        $deps = array(
            \'bootstrap\',
            \'intersection-observer\',
            \'imagesloaded\',
            \'scrollmagic\',
        );
        
        // LazyLoad
        $enable_lazyload = theme_helper()->get_option( \'enable-lazy-load\' );
        if( \'on\' === $enable_lazyload ) {
            array_push( $deps,
                \'jquery-lazyload\'
            );
        }
        // Header Js
        $enable_header = theme_helper()->get_option( \'header-enable-switch\' );
        if( \'on\' === $enable_header ) {
            array_push( $deps,
                \'jquery-tinycolor\'
            );
        }

        if( is_page() ) {
            array_push( $deps,
                \'splittext\',
                \'jquery-tinycolor\'
            );
        }
    }

    /**
     * Enqueue Scripts and Styles
     * @method enqueue
     * @return [type]  [description]
     */
    public function enqueue() {

        if ( is_singular() && comments_open() && get_option( \'thread_comments\' ) ) {
            wp_enqueue_script( \'comment-reply\' );
        }
    }
    
    // Register Helpers ----------------------------------------------------------
    public function script( $handle, $src, $deps = null, $in_footer = true, $ver = null ) {
        wp_register_script( $handle, $src, $deps, $ver, $in_footer);
    }

    public function style( $handle, $src, $deps = null, $ver = null, $media = \'all\' ) {
        wp_register_style( $handle, $src, $deps, $ver, $media );
    }

    // Uri Helpers ---------------------------------------------------------------

    public function get_theme_uri($file = \'\') {
        return get_template_directory_uri() . \'/\' . $file;
    }

    public function get_child_uri($file = \'\') {
        return get_stylesheet_directory_uri() . \'/\' . $file;
    }

    public function get_css_uri($file = \'\') {
        return $this->get_theme_uri(\'assets/css/\'.$file.\'.css\');
    }

    public function get_elements_uri( $file = \'\' ) {
        return $this->get_theme_uri( \'assets/css/elements/\' . $file . \'.css\' );
    }

    public function get_js_uri($file = \'\') {
        return $this->get_theme_uri(\'assets/js/\'.$file.\'.js\');
    }

    public function get_vendor_uri($file = \'\') {
        return $this->get_theme_uri(\'assets/vendors/\'.$file);
    }
}

new Theme_Assets;
我想删除\'splittext\' 和\'jquery-tinycolor\' 通过继承创建脚本dequeue 在父主题中运行,但它会删除所有其他脚本

这里是Child theme\'s 代码输入functions.php

add_action( \'after_setup_theme\', function() {

   class D extends Theme_Assets{

   function __construct(){
      $this->add_action( \'wp_enqueue_scripts\', \'dequeue\', 20 );
   }
       
      public function dequeue(){
         wp_dequeue_script(\'jquery-tinycolor\');
         wp_deregister_script(\'jquery-tinycolor\');
         wp_dequeue_script(\'splittext\');
         wp_deregister_script(\'splittext\');
      }
   }

   new D();
});
任何帮助都将不胜感激。

2 个回复
SO网友:Abhik

您需要在它排队后将其出列。

function wpse387889_dequeue_scripts() {

    wp_dequeue_script(\'jquery-tinycolor\');
    wp_deregister_script(\'jquery-tinycolor\');
    wp_dequeue_script(\'splittext\');
    wp_deregister_script(\'splittext\');
    
}
add_action( \'wp_enqueue_scripts\', \'wpse387889_dequeue_scripts\', 99 );

SO网友:Buttered_Toast

使用类和操作/过滤器时,回调有点不同。

通常是这样的

add_action(\'wp_enqueue_scripts\', \'my_callback_func\');
function my_callback_func () {}
但是在类中,您需要添加实例(如果使用new) 或类名(如果将方法用作静态)

在你的情况下

$this->add_action( \'wp_enqueue_scripts\', \'dequeue\', 20 );
需要替换为此

add_action( \'wp_enqueue_scripts\', [$this, \'dequeue\'], 20 );
我的猜测是,wordpress找不到出列回调函数,默认情况下,会将所有脚本出列。或者一开始可能从未加载任何脚本/样式。

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']