• Resolved beaversagency

    (@beaversagency)


    Hello,

    How can I remove useless scripts and css files from every pages that don’t use a form ?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Greetings @beaversagency

    You can force specific scripts/styles to be dequeued. A sample code for doing so would be:

    
    // For styles
    add_action( 'wp_print_styles', 'dequeue_evf_styles' );
    function dequeue_evf_styles() {
        wp_dequeue_style( 'everest forms stylesheet id you wish to dequeue' );
        // ... 
    }
    
    // For scripts
    add_action( 'wp_print_scripts', 'dequeue_evf_script' );
    function dequeue_evf_script{
        wp_dequeue_style( 'everest forms javascript id you wish to dequeue' );
        // ... 
    }
    

    Please note that this has to be done in a very controlled manner, with care so the de-queue don’t hamper everest forms from operating as intended. And quite often than not, the performance improvement you gain by having selectively remove essential css/js enqueues is almost always not worth the effort put into optimizing enqueues. I wish you all the best!

    Thread Starter beaversagency

    (@beaversagency)

    Hi @nobeld ,

    Thank you for your answer, it’s what I needed πŸ˜‰

    Thread Starter beaversagency

    (@beaversagency)

    hi @nobeld ,

    There is no id for scripts. Only for stylesheet.
    So I don’t know what put into the function for scripts.
    Can you help please ?

    Thanks

    Greetings @beaversagency

    For sure. Here are some that are the names of the registered scripts under frontend scripts:

    inputmask
    flatpickr
    mailcheck
    jquery-validate
    everest-forms
    everest-forms-text-limit
    everest-forms-ajax-submission
    

    Do note that these may change as the everest forms potentially assimilates new features. In order to find out what they are from time to time, you’ll need to refer to the EVF_Frontend_Scripts class in class-evf-frontend-scripts.php.

    Hope that helps, happy tinkering! πŸ™‚

    Thread Starter beaversagency

    (@beaversagency)

    Hi @nobeld ,

    I did what you tell me to do but it’s not working.
    Here what I test :

    add_action( 'wp_print_scripts', 'dequeue_evf_script' );
    function dequeue_evf_script() {
      wp_dequeue_style( 'inputmask' );
    }
    add_action( 'wp_print_scripts', 'dequeue_evf_script' );
    function dequeue_evf_script() {
      wp_dequeue_script( 'inputmask' );
    }

    Can you help ?

    FYI, everything is working for stylesheets πŸ˜‰

    Tks

    Greetings @beaversagency

    I believe it’s because you have the same callback for 2 different added hooks.

    Try something like

    add_action( 'wp_print_styles', 'dequeue_evf_STYLE' );
    function dequeue_evf_STYLE() {
      wp_dequeue_style( 'inputmask' );
    }
    
    add_action( 'wp_print_scripts', 'dequeue_evf_SCRIPT' );
    function dequeue_evf_SCRIPT() {
      wp_dequeue_script( 'inputmask' );
    }

    Note that the functions should be different, as they both are supposed to do 2 different things, and they are supposed to be done at particular WordPress events, namely 2 – printing styles, or printing scripts.

    Also, do keep in mind that you might have to deregister the script as well, in extreme cases. wp_deregister_script( string $handle );, this should work out if you add it in the same function as the one where you dequeue evf scripts.

    Thread Starter beaversagency

    (@beaversagency)

    Hi,

    Yeah I know it. What I meant, it’s that I tested these 2 part of code separately and none works.

    Only the style is removed by :

    function dequeue_evf_styles() {
        wp_dequeue_style('everest-forms-general');
        wp_dequeue_style('everest-forms-pro-frontend');
    }
    add_action('wp_print_styles', 'dequeue_evf_styles');

    But not scripts :/

    Tks

    Greetings @beaversagency

    wp_deregister_script( string $handle ); should help you out in that.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Remove Useless script and css’ is closed to new replies.