Overwriting WordPress plugin javascript files - createIT

Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    Overwriting WordPress plugin javascript files

    Overwriting WordPress plugin javascript files

    CHALLENGE: change the content of the .js file without editing plugin source code

    SOLUTION: use the script_loader_tag hook

    WordPress is a popular CMS that uses plugins to extend its functionality. Sometimes, we need to customize the way the plugin works. For the PHP part – we can often use filters and actions prepared by the author of the plugin. The customization of plugin behavior by using filters ensures future compatibility with new plugin versions.

    Deregistering the file

    To customize the .js file, which is included in the plugin, we can follow a typical solution: dequeue and deregister the plugin file and enqueue our new file (with new content). The downside of this solution is that now the order of the .js file can be different. Also, it might not work correctly if the plugin used wp_localize_script for translating javascript strings.

    Overwriting javascripts

    We’re going to force the plugin to use our .js file instead of the originally enqueued file. Using the script_loader_tag filter, we can check when the file loads and replace the path to the file. The new file will be stored in our child-theme assets directory. We need to know 2 things: script handle (used in the wp_enqueue_script function) and script original path.

    /**
     * Way of replacing original wordpress plugin .js file
     * ( to overwrite file code without editing plugin source code )
     */
    add_filter("script_loader_tag", "ct_script_loader_tag3", 90, 2);
    function ct_script_loader_tag3($tag, $handle){
        if('contact-form-7-js' !== $handle){
            return $tag;
        }
        $new_file =  'themes/child-theme/assets/js/custom-contact-form-7.js';
        return str_replace( 'plugins/contact-form-7/includes/js/index.js', $new_file, $tag );
    }

    Troubleshooting

    • I use cache / minify / autoptimize plugin for JS concatenation and the ‘script_loader_tag’ filter doesn’t work
    • Try to change filter priority or add a particular javascript file to the ‘ignored list’ / excluded files in cache plugin settings.

    Make sure you follow us for other useful tips.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    How to Get and Use the ChatGPT API
    • Dev Tips and Tricks

    How to Get and Use the ChatGPT API

    April 25, 2024 by createIT
    eCommerce growth – is your business ready?
    • Services
    • Trends

    eCommerce growth – is your business ready?

    April 8, 2024 by createIT
    Digital marketing without third-party cookies – new rules
    • Technology
    • Trends

    Digital marketing without third-party cookies – new rules

    February 21, 2024 by createIT
    eCommerce healthcheck
    • Services
    • Trends

    eCommerce healthcheck

    January 24, 2024 by createIT
    Live Visitor Count in WooCommerce with SSE
    • Dev Tips and Tricks

    Live Visitor Count in WooCommerce with SSE

    December 12, 2023 by createIT
    Calculate shipping costs programmatically in WooCommerce
    • Dev Tips and Tricks

    Calculate shipping costs programmatically in WooCommerce

    December 11, 2023 by createIT
    Designing a cookie consent modal certified by TCF IAB
    • Dev Tips and Tricks

    Designing a cookie consent modal certified by TCF IAB

    December 7, 2023 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us