Sharedaddy buttons are broken
-
Hey. On version 2.3.2 and 2.3.3, Sharedaddy buttons don’t open up new windows. Instead, you’re redirected to the corresponding social sharing page (e.g. twitter.com/intent…).
-
The problem is with the file below:
jetpack/modules/sharedaddy/sharing-service.php
The 420th line was like this on v2.3.1:
wp_print_scripts( 'sharing-js' );
The 420th line was changed with this with v2.3.2 and the following lines were added:
wp_enqueue_script( 'sharing-js' ); $recaptcha__options = array( 'lang' => get_base_recaptcha_lang_code() ); wp_localize_script('sharing-js', 'recaptcha_options', $recaptcha__options);
I think when you changed wp_print_scripts() to wp_enqueue_script(), jQuery is loaded after the following lines, causing them to fail:
<script type="text/javascript"> jQuery(document).on( 'ready post-load', function(){ jQuery( 'a.share-facebook' ).on( 'click', function() { window.open( jQuery(this).attr( 'href' ), 'wpcomfacebook', 'menubar=1,resizable=1,width=600,height=400' ); return false; }); }); </script> <script type="text/javascript"> jQuery(document).on( 'ready post-load', function(){ jQuery( 'a.share-twitter' ).on( 'click', function() { window.open( jQuery(this).attr( 'href' ), 'wpcomtwitter', 'menubar=1,resizable=1,width=600,height=350' ); return false; }); }); </script> <script type="text/javascript"> jQuery(document).on( 'ready post-load', function(){ jQuery( 'a.share-google-plus-1' ).on( 'click', function() { window.open( jQuery(this).attr( 'href' ), 'wpcomgoogle-plus-1', 'menubar=1,resizable=1,width=600,height=600' ); return false; }); }); </script> <script type="text/javascript"> jQuery(document).on( 'ready post-load', function(){ jQuery( 'a.share-linkedin' ).on( 'click', function() { window.open( jQuery(this).attr( 'href' ), 'wpcomlinkedin', 'menubar=1,resizable=1,width=580,height=450' ); return false; }); }); </script>
Hope it helps ??
Do you use any other plugins on your site that may cause jQuery to load in the footer instead of in the head?
We stopped using
wp_print_scripts
as it was recommended for all plugin authors to make the switch.Nope.
Could you post your site URL here, so I can have a look?
If you want it to remain private, you can also contact us via this contact form:
https://jetpack.me/contact-support/Hey. I can’t really recreate the error (since it effects my blog in a bad way and I don’t want to lose visitors because of that) but I can paste the HTML sources of this URL:
v2.3.1 source (the page with no errors):
https://sharetext.org/EgIVv2.3.2+ source (the page with 4 JS errors):
https://sharetext.org/qQXG(I just changed the file
jetpack/modules/sharedaddy/sharing-service.php
to recreate the error temporarily, so don’t mind that both sources show the version number 2.3.3.)As you can see, in the new versions, the required external JS files load after the inline JS. I’m not very good with JS, could this be the error?
Thank you!
The jQuery library is loaded in the footer in both versions. By default, jQuery should be loaded in the head. One of your plugins, or your theme, is forcing jQuery to be loaded in the footer.
Could you search for the following function in your theme folder (most probably in functions.php):
wp_enqueue_script( 'jquery' )
It may include a
true
parameter to force the library to load in the footer. If so, could you change that parameter tofalse
?If your theme doesn’t include such function, you will need to locate it in one of your other plugins, and make the change there.
Let me know if it helps.
Actually, I’m not sure but I think it’s Jetpack’s Sharedaddy that loads jQuery in the footer – I don’t even have any reference to jQuery in my theme :). When I check out
jetpack/modules/sharedaddy/sharing-service.php
, I see that it registers the ‘sharing-js’ script with a dependancy to jQuery (line 593), then adds an action to ‘wp_footer’ for thesharing_add_footer
function (line 594) which enqueues the ‘sharing-js’ script (and jQuery, since ‘sharing-js’ depends on it) in the footer (line 420).Can you check if it’s correct?
Wait, I think you’re right – I tried another theme with v2.3.3 and it works, loads the scripts in the <head>. Gotta check my theme again ??
Just wondering why you would think that loading jquery in the head would be neccessary. In fact its quite opposite. Its nearly always best to load it in the footer unless you need it to render the html for the page to display properly.
https://stackoverflow.com/questions/2105327/should-jquery-code-go-in-header-or-footer
https://samsaffron.com/archive/2012/02/17/stop-paying-your-jquery-tax
and the list goes on.
The proper way to do it would be to simply wrap your JS in a $(document).ready() function and add them to the footer below the printed_scripts.
I have developed many WP sites, several jQuery Plugins both for wp and in general and if you look at any starter frameworks jQuery is always just before the closing /body tag.
I think you should simply update the JS for these functions and wrap them with doc.ready. Would be much more elegant and not require a user to modify a working theme. In fact that should be the standard in general for WP plugins.
Just a tip. But since Jetpack seems to be hugely popular it would be in the best interest of your users and other developers for this to be updated.
Thanks for the great work youve done so far.
Just wondering why you would think that loading jquery in the head would be neccessary. In fact its quite opposite.
Jetpack uses WordPress’ recommended way of including jQuery, via
wp_enqueue_script()
(#). This method will include jQuery in the<head>
.
If you don’t usewp_enqueue_script()
on the sites you develop, you’re bound to run into conflicts with other plugins at one point or another. You can read more about it here:
https://pippinsplugins.com/why-loading-your-own-jquery-is-irresponsible/If you’d prefer WordPress to start including jQuery in the footer instead, you could bring up your ideas and discuss about this on core.trac.www.ads-software.com. That’s actually something that’s been discussed in the past here:
https://core.trac.www.ads-software.com/ticket/22244
- The topic ‘Sharedaddy buttons are broken’ is closed to new replies.