functions – WordPress wp_dequeue_script remove all other scripts

Question

I want to override function in child theme to remove specific scripts, which is defined inside a class in parent theme.

Parent theme functions.php

class Theme_Assets extends Theme_Base {

    /**
     * Hold data for wa_theme for frontend
     * Yavar Mammadov 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() . "https://wordpress.stackexchange.com/" . $file;
    }

    public function get_child_uri($file="") {
        return get_stylesheet_directory_uri() . "https://wordpress.stackexchange.com/" . $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;

I want to remove ‘splittext‘ and ‘jquery-tinycolor‘ scripts by inheriting dequeue function in parent theme but it remove all other scripts

Here is Child theme’s code in 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();
});

Any helps are appreciate.

0
Hung PD 1 month 2021-05-08T00:25:43-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse