How to avoid double insertion of code using wp_head hook? (10 posts)

  1. Roy Tanck
    Posted 9 years ago #

    I'm working on several plugins that insert flash movies (video/audio players, a type of message board, etc) into posts and pages. The best way to do this (imho) is to to use SWFObject (http://blog.deconcept.com/swfobject/).

    This requires code to be added to the head section of the page, for which I'd like to use the wp_head hook. My problem is however that when using all my plugins together, this would insert the javascript link several times, which might result in errors.

    Is there any way to check whether specific code has already been inserted?

  2. Not really, no. I would suggest having a global flag which you set when you insert the code and then have every plugin check for that global flag before inserting it.

  3. Alex King
    Posted 9 years ago #

    I wonder if the enqueue_script function in WP 2.1 can handle this, I think it should.

  4. Roy Tanck
    Posted 9 years ago #

    I've tried, and having each plugin (two currently) insert the code works just fine on my blog. I'd hate to have to force users to edit their template to get the plugin working.

    A global flag migth be a good idea, but my current theme (Tranquility, which I also made) also uses the same javascript, and so might others.

  5. So just make your theme use the same global flag that the plugins use. Or make the javascript smart enough to recognize that it's already been run.

  6. Roy Tanck
    Posted 9 years ago #

    My point is that soon the whole world will (hopefully) be using this technology. Adobe itself already is :). I can avoid double insertion only for my own projects...

  7. Well, okay. In theory you can add an action hook to the 'init' action. In that function, you need to make a call to ob_start();. This will enable the output buffering. You'll also have to hook the 'shutdown' action hook and call ob_end_flush(); at that function, so the output will be sent at the end.

    Then, in your plugin, wherever you do the output, you can call ob_get_contents() to get back a string containing all output made thus far. You can then search that string to see if your javascript has already been inserted into the html.

    This will work, but it's not particularly advisable. It will force the webpage output to buffer in memory until all processing is complete. This is not a particularly big deal most of the time, but if you have every plugin doing it, then that's a lot of unnecessary memory usage. Using a flag is better.

    Also, using Javascript to insert Flash? Bad idea. Won't work with anybody who disables javascript by default, a number which is increasing all the time. At least with more proper approaches (like an OBJECT tag), you'll get something to display. The browser should handle rendering content. Doing it with javascript is a poor approach.

  8. Alex King
    Posted 9 years ago #

    "Won't work with anybody who disables javascript by default, a number which is increasing all the time."

    Do you have stats on this?

  9. Roy Tanck
    Posted 9 years ago #

    I agree that using javascript to embed flash movies is far from ideal. It is however the lesser of several evils at this point. The Active-X activation issue that Microsoft introduced a while back (which forces users to click movies before interacting with it) is forcing Flash developers to resort to javascript tricks or suffer a major usability setback.

    Geoff Stearns's SWFObject method is a brilliant piece of code that handles plugin detection and degrades gracefully on older systems or systems without flash.

    Luckily it also has built in protection for double initialization. I think I'll settle for double insertion for now.

  10. Do you have stats on this?

    Not as such, no. I recall that the number of browsers with javascript simply disabled in 2001 was over 20%, however I've seen nothing since then.

    However, one of the more popular extensions for Firefox is NoScript, which uses a whitelist approach to javascripting, only allowing it on specified sites.

Topic Closed

This topic has been closed to new replies.

About this Topic