When adding scripts to WordPress, you will inevitably run into a small, but painful, issue of localization.
Localizing a plugin or theme is relatively straightforward, but JavaScript presents its own difficulties since we can’t easily call the PHP functions necessary (which is one reason authors embed JavaScript in PHP files).
Since embedding JavaScript in PHP files is never a good technique, we use localization to save the day.
With JavaScript localization, you can use PHP magic to build your localized strings, and then use JavaScript to read/parse those strings. What you do with them is only limited to your imagination.
Furthermore, if you display anything with JavaScript, chances are your users will want the strings to be localized.
Fortunately, WordPress provides the ultra-handy wp_localize_script function.
wp_localize_script
The wp_localize_script takes three arguments:
handle
object_name
l10n
Handle
The handle argument will be the same handle you use for your script name.
For example, if you have a handle of my_script, you would use the […]
Original post by Ronald Huereca
Technorati Tags: blog
Starting in WordPress 2.1 (if I remember correctly), the awesome folks at Automattic gave us the even awesomer function of wp_enqueue_script.
Before that, it was every plugin or theme author for himself. If you wanted to add in a script, it was hard-coded in.
As you might imagine, this presented a ton of problems. Scripts were loaded twice, out of order, or even when they weren’t needed at all.
Furthermore, some themes and plugins had the JavaScript embedded within the plugin’s or theme’s PHP file just to capture a few PHP variables. Not good! In order to add scripts properly to JavaScript, you must always keep your PHP and JavaScript separate. And by separate, I mean separate files. There’s just no excuse anymore (I’ll get into this in Part 2 of this series).
The wp_enqueue_script function is the first step in loading your scripts properly. Not only […]
Original post by Ronald Huereca
Technorati Tags: blog
A neat way to spice up your WordPress search page is to highlight search terms within your search results. I’ve seen some tutorials on the net on how to do this, but I haven’t found one that highlights both title and post content and is a drop-in modification for WordPress. Today I will bring you this drop-in hack for highlighting search terms on your WordPress blog.
Installation
1. Copy and paste the following code into your theme’s functions.php file:
function hls_set_query() {
$query = attribute_escape(get_search_query());
if(strlen($query) > 0){
echo ‘
<script type=”text/javascript”>
var hls_query = “‘.$query.’”;
</script>
‘;
}
}
function hls_init_jquery() {
wp_enqueue_script(’jquery’);
}
add_action(’init’, ‘hls_init_jquery’);
add_action(’wp_print_scripts’, ‘hls_set_query’);
If you are having issues with copy-paste from this blog, here is a link to the same code in […]
Original post by Thaya Kareeson
Technorati Tags: blog
This article contains 10 visual tutorials intended for web designers and newbies on how to apply Javascript effects with jQuery. In case you don’t know about jQuery, it is a "write less, do more" Javascript library. It has many Ajax and Javascript features that allow you to enhance user experience and semantic coding. Since these tutorials are focused on jQuery, I’m not going to get into the details of the CSS.
Note: the version used in this article is jQuery 1.2.3
View jQuery Demos
Download Demo ZIP (more…)
Technorati Tags: blog, rss
Original post by Nick La
Technorati Tags: blog, rss