post-page

Adding Scripts Properly to WordPress Part 2 – JavaScript Localization

9
responses
by
 
on
May 7th, 2010
in
HOW-TO, WordPress FAQs
heading
heading
heading
9
Responses

 

Comments

  1. Konstantin (7 comments.) says:

    Nice post, I learned a lot.

    Is there any way though, to avoid having to write all localized strings in one variable?

    Context would be the TweetMeme Button, where I can’t assign personalized options, the wp_localize_script-way.

  2. Ronald huereca (32 comments.) says:

    Not that I can think of.

    What you could do is write a small script with localization. Queue Your script with TweetMeme as a dependency. Then in your script, do an onload (jQuery is good at this) and do a DOM replacement of the strings you want localized.

  3. Andrew@BloggingGuide (90 comments.) says:

    Thanks for the clear and easy to follow example. Learned a lot from this.

  4. Jason (1 comments.) says:

    This was a great find! Finally a WordPress way to cleanly separate PHP and javascript… Thanks!

  5. Dave Doolin (25 comments.) says:

    I found this after I bought the book! But I wouldn’t have found it without reading through Chapter 3 of WordPress and Ajax.

  6. Tabby (1 comments.) says:

    Javascript is a royal pain for me.. which isn’t great because advertisers like to have “interactive” banners on my site. They tend to break my website themes when I use it in the sidebar though. Is there anyway to contain script in a manner so it doesn’t mess with anything else like that?

  7. Joe Fitz (1 comments.) says:

    Localizing scripts adds a great way for php to insert variables into js. One thing to watch out for here is security. The localized js objects are global in scope and can easily be manipulated in the console. This creates big security risks and you should try to avoid putting sensitive data in there. But one best practice I have come up with, and I’m sure others have as well, is to use the localized object in a closure. So, for the js file you want the localized object to be used in, you should wrap the whole js file in a closure.

    (function(localized_object){
    /* all code goes here */
    })(localized_object);

    This is a self executing function which passes the localized_object in as a parameter. So any code in this object uses a copy and not the global instance. This prevents any chance of tampering.

  8. Anthony (1 comments.) says:

    I have read so many other articles, but this one is really great! Very clear, concise & straight to the point with relevant code examples in PHP & Javascript.

    There is one thing that I think, is worth mentioning. Please read the following excerpt from the official Codex:

    IMPORTANT!: wp_localize_script() MUST be called after the script it’s being attached to has been enqueued. It doesn’t put the localized script in a queue for later queued scripts.

    Reference: http://codex.wordpress.org/Fun.....ize_script

    So if you can include elaborate on the above point & include it within your article, I think that will help many of us about the caveats of using the wp_localize_script().

    Ronald, I really want to thank for putting up such a helpful article! Keep up the good work!



Trackbacks/Pingbacks

  1. […] Adding Scripts Properly to WordPress Part 2 – JavaScript Localization […]

Obviously Powered by WordPress. © 2003-2013

page counter
css.php