• Resolved A WP Life

    (@awordpresslife)


    How to load Plugin’s Jquery in the header.

    I am facing an issue with Jquery in some themes that are not using Jquery.

    I have loaded Jquery in my plugin When I am the plugin on a theme that is not using Jquery Page showing an error that Jquery is not defined.

    So how to load Jquery in the header so I will not see this error

    I am using this code in the plugin –

    wp_enqueue_script('jquery');

    Please help

Viewing 7 replies - 1 through 7 (of 7 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I would search how many times jquery itself is loaded into your webpage by looking at the source code. That error can occur when jQuery is loaded in multiple times.

    Thread Starter A WP Life

    (@awordpresslife)

    I am using the “Twenty Nineteen” theme and this theme not using any jquery.

    And also I am using portfolio plugin and i have checked for output, I have loaded jquery only one time.

    Please see the screenshots.

    1. https://prnt.sc/p3sie7
    2. https://prnt.sc/p3siys

    Thanks

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Can you check the page source still for the order of when jQuery is loaded? There’s a big difference between jQuery isn’t loading at all, and jQuery not loading in the correct order.

    Moderator bcworkz

    (@bcworkz)

    You say you use wp_enqueue_script('jquery');, which is correct. Are you calling this function from within a callback hooked to “wp_enqueue_scripts”? It is required to do so. As long as other plugins and themes follow the same procedure, jQuery should be only loaded once no matter how many different modules enqueue it.

    Thread Starter A WP Life

    (@awordpresslife)

    We tried this and it’s worked.

    
    add_action( 'plugins_loaded', array( $this , '_load_plugin_scripts' ) );
    public function _load_plugin_scripts() {
    	//js
    	wp_enqueue_script('jquery');
    	wp_enqueue_script('awplife-custom-js', MY_PLUGIN_URL .'assets/js/my-custom.js', array('jquery'), '1.0.6' , true);
    }
    

    We were using the js with shortcode file, It wasn’t working on with theme Twenty Nineteen, Seventeen, and other authors.

    Thanks and Appreciated.

    Moderator bcworkz

    (@bcworkz)

    I’m glad it’s working. I still advise using “wp_enqueue_scripts” instead. “plugins_loaded” could be too early for some situations. But it’s your site, not mine. you may do as you please (within legal limits ?? ). There is also the “if it works don’t fix it” maxim.

    Thread Starter A WP Life

    (@awordpresslife)

    Sorry! I was using the same wp_enqueue_scripts. It’s code copy-paste mistake. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Load Plugin’s Jquery in header’ is closed to new replies.