Posted In Internet

Make Alex King’s Share This Plugin Play Nice With Unwakeable

Fixed. Unwakeable 2.0 will include this fix.

My Unwakeable WordPress theme doesn’t play nicely with Alex King’s Share This WordPress plugin. When Share This is being used, the livesearch feature of Unwakeable doesn’t work in Internet Explorer 6 or 7. I’m willing to bet K2 has the same issue. I know for a fact that Redoable had this problem at one point.

Livesearch breaks because prototype.js gets loaded twice, first by Unwakeable, then again by Share This. Now, Share This uses the prototype.js that will be included in WordPress 2.1, located at wp-includes/js/prototype.js. We use a custom prototype.js file for Unwakeable. The prototype.js file in Unwakeable will still provide all the functionality needed by Share This.

The prototype.js included in Unwakeable is located at wp-content/themes/unwakeable-1.2/js/prototype.js.php. It has a .php extension because there’s some PHP code at the top that needs processed before doing anything else. The PHP code tells the browser to cache the prototype.js file, it also sends correct content-type headers so the browser knows it’s dealing with a javascript file after all is said and done.

I’ve spent a few days thinking of possible solutions that could be implemented from within Unwakeable. That’s not possible though, unfortunately. Well, it is possible, but would require filtering all the HTML output by WordPress before it’s sent to the browser so we could strip out the prototype.js included by Share This. Doing something like that would probably result in a fairly dramatic decrease in performance, so it’s not an option.

Fortunately, it’s extremely easy to modify Share This to not load prototype.js. Here’s what you need to do:

  1. Open wp-content/plugins/share-this/share-this.php
  2. Go to line 352:
  3. Delete all of line 352 (code above) and you should be left with this on line 351 to line 354
  4. Save share-this.php and upload it to wp-content/plugins/share-this/

That’s all there is to do to stop Share This from loading prototype.js. It sorta sucks having to ignore the prototype.js that’s already included with WordPress 2.1. I will probably start working on making Unwakeable work with the prototype.js included in WordPress 2.1.

Does anyone know if it’s possible to determine if a javascript file has been loaded, from within javascript? I ask because I think Share This will still load a second prototype.js, even if I make Unwakeable work with prototype.js from WordPress 2.1 (the one used by Share This already).

I imagine Alex will come up with a method to determine if prototype.js has already been loaded. Unless javascript won’t allow identical .js files to be loaded, in which case determining if prototype.js has already been loaded would be pointless. If that’s the case then I should be good simply making Unwakeable work with prototype.js from WordPress 2.1.

Sorry for the scattered thoughts, this has really been bugging me lately. Anyway, you should be able to make your livesearch work with Share This in Internet Explorer now. This probably applies to most K2 based WordPress themes, but I’m not sure.

Well, now what?

Work with Me

I'm available for hire and always taking new clients, big and small. Got a project or an idea you'd like to discuss? Startup plan but no developer to make it happen? Just get in touch, I'd love to see if I can help you out!

Leave some Feedback

Got a question or some updated information releavant to this post? Please, leave a comment! The comments are a great way to get help, I read them all and reply to nearly every comment. Let's talk. :) is proudly hosted by DigitalOcean

About these ads
  • It’s relatively trivial for you to solve this in your theme – I don’t understand why you’re trying to solve it by altering plugins.

  • Right on Alex. I was over-thinking the whole thing way too much. Someone else mentioned to me they had to remove that line from Share This, so I guess I just assumed it was more difficult than it really was.

    I’ve modified Unwakeable to use wp-includes/js/prototype.js as the default prototype file for WordPress versions greater than or equal to 2.1. If 2.0 is still in use it’ll use the old prototype.js.php file from K2.

    Basically, what I did was remove_action(‘wp_head’, ‘akst_head’). With that gone, I’m free to include wp-content/plugins/share-this/share-this.php?akst_action=js and wp-content/plugins/share-this/share-this.php?akst_action=css at my will.

    So, yah, seems to be a pretty good fix. Everything about Share This still seems to work, even the pages.

  • That’s definitely an improvement, but I still think you’re looking at this the wrong way. Coding in exceptions for individual plugins isn’t a solution; it’s a hack and it isn’t forward-compatible.

    Plugins don’t have some of the luxuries that themes do. One of these is the ability to see what other JS files have been included[1]. Since themes can see this data and plugins can’t, it only makes sense for the *theme* to deal with the issue – not by modifying plugins.

    Here is some sample code.

    [1] Note, WP 2.1 includes a mechanism for plugins to avoid this – yay 2.1!

  • Alex, I love you. That code is pretty much exactly what I was looking for. I will work on implementing something similar in Unwakeable. Thanks for all your help Alex!

  • Mosey

    Thank you for addressing this issue!:D Although I’m not using your theme, I also use a theme (not K2)which already calls upon prototype.js so calling it twice just wouldn’t work.
    With regards to Alex’s code – where would I place this in the theme?

  • Mosey, if you’re using WordPress 2.1, you should remove the code in you theme that includes prototype.js and replace it wp_enqueue_script(‘prototype’). wp_enqueue_script is a new function in WordPress 2.1 that will make sure scripts such as prototype only get loaded once.

    Now, you may have to move some other things around, because your theme probably needs prototype for livesearch or other things. Loading prototype.js with wp_enqueue_script might cause some other scripts that use prototype to break. This happens when these scripts are loaded before prototype.js is included. So, you may have to move some of your included javascript down so it loads after prototype.js gets loaded up.

    Let me know if you’ve got any questions.

  • Mosey

    Hi Tyler,
    Thank you for your helpful reply ๐Ÿ™‚ I am using FoliageMod 1.0.2 – a little dated I know but I liked it lots when I first started using it ๐Ÿ™‚ I attempted to replace the javascript include for prototype.js with the function as you suggested; but for whatever reason, my installation of 2.1 has decided to completely ignore it (there’s justa blank space where the function should be if I view source) Its about time for me to make a move on with the theme now I guess, but I’m puzzled as to why it doesn’t load at all with that function
    Thanks again! ๐Ÿ˜€ Your help has been really appreciated – wp_enqueue_script is a seriously useful new addition ๐Ÿ™‚