SimonPadbury / b4st

A Bootstrap 4 Starter Theme, for WordPress
The Unlicense
311 stars 104 forks source link

jQuery includes after plugins #58

Closed kaplan512 closed 6 years ago

kaplan512 commented 6 years ago

Hi there, I downloaded your theme yesterday and it doesn't work with WordPress plugins which use jquery (fancybox for example), because this plugins scripts init before jquery init. I put the jquery script in the header and it helped, but it looks like not the best idea. Do you know how to solve this problem in more WordPress way?

SimonPadbury commented 6 years ago

Hi Kaplan,

Sorry for the delay.

I've heard that the correct way for plugin developers to enqueue their scripts is for them to use wp_enqueue_scripts but some of them use wp_init – that loads before wp_enqueue_scripts.

There is a way to replace the jQuery that comes with WordpPress, but it is not recommended – see https://wordpress.stackexchange.com/questions/55924/when-to-use-add-actioninit-vs-add-actionwp-enqueue-scripts

But if you must do this: delete (or comment out) the register & enqueue for jQuery 3.3.1 in the b4st enqueues file, and then add this (add it separately, not inside the wp_enqueue_scripts):

if ( ! function_exists('b4st_newer_jquery') ) {
    function b4st_newer_jquery() {
        if ( ! is_admin() ) {

            // Dequeue jQuery
            wp_dequeue_script( 'jquery' );

            wp_register_script('jquery-3.3.1', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js', false, '3.3.1', true);
            wp_enqueue_script('jquery-3.3.1');
        }
    }
}
add_action( 'init', 'b4st_newer_jquery', 1 );
khjohnson1972 commented 6 years ago

Is there a reason to load multiple versions of jQuery. It looks like you are loading 3.3.1 from cloudflare, without deregistering Wordpress's script. Do you think it would appropriate to add wp_deregister_script( 'jquery' ); to the functions/enqueues.php file?

SimonPadbury commented 6 years ago

Hi Kevin, the theme doesn’t register the WP jquery.

Are you seeing it on the front end when you are not logged in?

On 24 Oct 2018, at 17:36, Kevin Johnson notifications@github.com wrote:

Is there a reason to load multiple versions of jQuery. It looks like you are loading 3.3.1 from cloudflare, without deregistering Wordpress's script. Do you think it would appropriate to add wp_deregister_script( 'jquery' ); to the functions/enqueues.php file?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

khjohnson1972 commented 6 years ago

Hi Simon, Wordpress automatically loads jQuery on the front end. The lines in functions/enqueues.php created a duplicate version of jquery: wp_register_script('jquery-3.3.1', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js', false, '3.3.1', true); wp_enqueue_script('jquery-3.3.1');

One solution would be to unregister Wordpress' jQuery, and then load the 3.3.1 version with the tag 'jquery' (see below): wp_deregister_script( 'jquery' ); wp_register_script('jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js', false, '3.3.1', true); wp_enqueue_script('jquery');

SimonPadbury commented 6 years ago

Some advise never de-registering WP's jQuery — others say do it, and add the newest jQuery. Now I've ended up with two.

So what I will do is revert to simply enqueuing WP's jQuery. That will please most people.

If anyone wants do do their own thing, they can do it in their b4st or in a child theme.

khjohnson1972 commented 6 years ago

Sounds good - Thanks

SimonPadbury commented 6 years ago

Thanks for the other thing — meta tags

On 25 Oct 2018, at 14:58, Kevin Johnson notifications@github.com wrote:

Sounds good - Thanks

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SimonPadbury/b4st/issues/58#issuecomment-433061760, or mute the thread https://github.com/notifications/unsubscribe-auth/AFTFpVMfeMnghEwk3DgN7gbMmuvUAOgXks5uocOQgaJpZM4UYDLA.