• I want to modify existing method from parent class in child theme without affect to other methods / methods in constructor to remove specific scripts

    Parent theme 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;

    I want to remove ‘splittext’ and ‘jquery-tinycolor’ scripts by inheriting dequeue function in parent class 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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    By extending the class and overriding its constructor, the Theme_Assets constructor never executes. Yours is not removing other scripts, they’re never added to start with.

    If you want to only remove specific scripts, don’t override the parent class method. Instead, just dequeue them through your own methods. It’s unlikely you really need to deregister as well unless you’re going to register a replacement script using the same handle.

    You must ensure your removal code executes after the scripts are first enqueued. Using the same hook as the parent, but with a larger priority argument ought to do that.

    Thread Starter ghostcoder

    (@ghostcoder)

    Hi @bcworkz

    Thank you for response. I tried with my own method but it still doesn’t work. Here what I did before overriding parent class

    function 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', 'dequeue_scripts', 99 );
    Moderator bcworkz

    (@bcworkz)

    The parent class isn’t actually enqueuing those scripts, only registering. It’s unclear when or how they are enqueued. If enqueuing still happens with enough information, deregistering will not be effective. Dequeuing certainly wouldn’t be effective if done too early. See if you can determine where/when the enqueuing actually happens and ensure your code runs after that.

    Or, instead of overriding the constructor or dequeuing, override the class’ register() method, registering all the other scripts you still want. If the class code you posted is the entire thing, you shouldn’t need any of the $deps code that occurs after the script() calls. $deps are not used anywhere in the method, nor passed anywhere. But once again, if subsequent enqueuing happens with enough information, removing registration code will be ineffective. You should find the enqueuing code to get a clear handle on how to best proceed.

    Thread Starter ghostcoder

    (@ghostcoder)

    Hi,

    Thank you @bcworkz for the clarify.

    I tried in few days but still doesn’t work, it stressed me a lot. Could you please provide example code? Thanks in advance.

    Have a nice weekend

    Regards,

    Moderator bcworkz

    (@bcworkz)

    I cannot suggest proper code with the existing information. The parent theme’s class is only enqueuing “comment-reply”, all the rest are merely registered at this point. The other scripts are enqueued elsewhere. We need to know where/when/how they are enqueued in order to successfully dequeue them.

    Thread Starter ghostcoder

    (@ghostcoder)

    Hi,

    May I paste full parent’s code and you can take a look?

    Regards,

    Moderator bcworkz

    (@bcworkz)

    I’m willing to take a quick look IF it is free open source software. Publishing paid proprietary source code will surely run afoul of copyright, terms of use, or some other restrictions on publishing intellectual property. If it’s open source, tell me what it is and I can access the source code for myself.

    I cannot guarantee I’ll find what we’re looking for during a quick look. Such things can sometimes be very difficult to find. Other times they practically jump out at you.

    I’ve another thought. There is an approach that could be fruitful. You should be able to hook “wp_print_scripts” early (small $priority arg) and dequeue the offending scripts before their tags are output. This action almost surely fires after all normal enqueuing. Might not hold true for deferred scripts though. Please give this a try before having me look over the source code.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to extend class and modify existing methods in parent class’ is closed to new replies.