<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Big Red Tin &#187; JavaScript</title>
	<atom:link href="http://bigredtin.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://bigredtin.com</link>
	<description>Thoughts about the web and business from the large pantry</description>
	<lastBuildDate>Tue, 07 Sep 2010 02:53:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>jQuery 1.5 as jQuery 1.5.0</title>
		<link>http://bigredtin.com/behind-the-websites/jquery-15-as-jquery-150/</link>
		<comments>http://bigredtin.com/behind-the-websites/jquery-15-as-jquery-150/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 02:53:48 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=802</guid>
		<description><![CDATA[<a title="When jQuery 1.4 was released" href="http://jquery14.com/day-01/jquery-14">When jQuery 1.4 was released</a>, the Google URL being publicised by the jQuery team was <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">http://.../jquery/1.4/jquery.min.js</a> - while Google had set it up as <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js">http://.../jquery/1.4.0/jquery.min.js</a>. I had problems with this seemingly minor difference.]]></description>
			<content:encoded><![CDATA[<p>In early 2009, I wrote a post on <a title="browser caching times" href="http://bigredtin.com/behind-the-websites/caching-on-the-google-ajax-libraries-api/">browser caching times</a> for the Google AJAX Libraries API.</p>
<p>The cheat notes are that three different URLs point to the current edition of jQuery and each URL is cached in the browser for a different length of time:</p>
<ul>
<li><a title="Latest in the version 1 tree" href="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">Latest in the version 1 tree</a>: 1 hour</li>
<li><a title="Latest in the version 1.4 tree" href="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">Latest in the version 1.4 tree</a>: 1 hour</li>
<li><a title="Version 1.4.2 exactly" href="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">Version 1.4.2 exactly</a>: <strong>1 year</strong></li>
</ul>
<p>The point of my post was that, when using Google&#8217;s AJAX Library to host jQuery, or any of the other libraries, it is best to specify the exact version to receive the full benefit of hosting on the same server as gazillions of other web sites, that being, your <a title="visitors don't need to download the library for every site they visit" href="http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/">visitors don&#8217;t need to download the library for every site they visit</a>. I was reminded of this when Dave Ward appeared on <a title="episode 32 of the official jQuery podcast" href="http://podcast.jquery.com/2010/08/27/episode-32-dave-ward/">episode 32 of the official jQuery podcast</a>.</p>
<p><a title="When jQuery 1.4 was released" href="http://jquery14.com/day-01/jquery-14">When jQuery 1.4 was released</a>, the Google URL being publicised by the jQuery team was <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">http://&#8230;/jquery/1.4/jquery.min.js</a> &#8211; while Google had set it up as <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js">http://&#8230;/jquery/1.4.0/jquery.min.js</a>. I had two problems with this:</p>
<ul>
<li>The publicised URL, coming from the official jQuery team, was only cached for 1 hour;</li>
<li>Anyone using the publicised URL would automatically be upgraded to jQuery 1.4.1 upon its release, regardless of their expectations.</li>
</ul>
<p><strong>My request to John Resig and the jQuery team is to avoid confusion by officially numbering the next version as jQuery 1.5.0 and publicising the URL that contains the full version number.</strong></p>
<p>That means that for most people the default version of jQuery they download will be updated least often. It means that the people building the websites have more control over which version of jQuery their end users download and when.</p>
<p>It may seem trivial now but if your visitors leave your site while they&#8217;re waiting for jQuery to download, you&#8217;ll think it less so.</p>
<p>I&#8217;d love to hear your thoughts in the comments below or via <a title="@bigredtin" href="http://twitter.com/?status=@bigredtin">@bigredtin</a> on Twitter. If you agree, why not let the jQuery team know <a title="via Twitter" href="http://twitter.com/?status=@jquery">via Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/jquery-15-as-jquery-150/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Localisation in WordPress</title>
		<link>http://bigredtin.com/behind-the-websites/javascript-localisation-in-wordpress/</link>
		<comments>http://bigredtin.com/behind-the-websites/javascript-localisation-in-wordpress/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:00:45 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_enqueue_script]]></category>
		<category><![CDATA[wp_localize_script]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=759</guid>
		<description><![CDATA[I was asked on Twitter recently if it's possible to pass WordPress data to JavaScript, <code>wp_localize_script()</code> is the tool to do it with.]]></description>
			<content:encoded><![CDATA[<p>Recently on Twitter <a title="@iamcracks" href="http://twitter.com/iamcracks/">@iamcracks</a> asked</p>
<blockquote><p>Attention WordPress Wizards &amp; Gurus. Is it possible to &#8220;get WordPress to write a custom field into a javascript variable&#8221;?</p>
<p>&#8211; <a title="source" href="http://twitter.com/iamcracks/status/22658211370">source</a></p></blockquote>
<p>While I wouldn&#8217;t be so bold as to claim I&#8217;m either a wizard or a guru, I happen to know the answer to @iamcracks question.</p>
<p>A while back I wrote a two part tutorial on using <a title="JavaScript the WordPress way" href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/">JavaScript the WordPress way</a>, the code below builds on that. The first step is to load the JavaScript in functions.php using <code>wp_enqueue_script()</code> as detailed in the earlier tutorial:</p>
<pre><code>&lt;?php
function brt_load_scripts() {
  if (!is_admin()) {
    wp_enqueue_script(
      'brt-sample-script', //handle
      '/path/2/script.js', //source
      null, //no dependancies
      '1.0.1', //version
      true //load in html footer
    );
  }
}

add_action('wp_print_scripts', 'brt_load_scripts');
?&gt;</code></pre>
<p>This outputs the html required for the JavaScript when <code>wp_footer()</code> is called in footer.php</p>
<p>Localising the script is done using the function <code>wp_localize_script()</code> it takes three variables:</p>
<ul>
<li><strong>$handle</strong> &#8211; (string) the handle defined when registering the script with wp_enqueue_script</li>
<li><strong>$javascriptObject</strong> &#8211; (string) name of the JavaScript object that contains the passed variables.</li>
<li><strong>$variables</strong> &#8211; (array) the variables to be passed</li>
</ul>
<p>To pass the site&#8217;s home page and the theme directory, we&#8217;d add this function call below the <code>wp_enqueue_script</code> call above:</p>
<pre><code>&lt;?php
...
wp_localize_script('brt-sample-script', 'brtSampleVars', array(
  'url' =&gt; get_bloginfo('url'),
  'theme_dir' =&gt; get_bloginfo('stylesheet_directory')
  )
);
...
?&gt;</code></pre>
<p>The output html would be:</p>
<pre><code>&lt;script type='text/javascript'&gt;
/* &lt;![CDATA[ */
var brtSampleVars = {
  url: "http://bigredtin.com",
  theme_dir: "http://bigredtin.com/wp-content/themes/bigredtin"
};
/* ]]&gt; */
&lt;/script&gt;
&lt;script type='text/javascript' src='/path/2/script.js?ver=1.0.1'&gt;&lt;/script&gt;</code></pre>
<p>Accessing the variables within JavaScript is done using the standard dot notation, for example <code>brtSampleVars.theme_dir</code> to access the theme directory.</p>
<p>Using a post&#8217;s custom fields is slightly more complicated so I&#8217;ll write out the code in full:</p>
<pre><code>&lt;?php
function brt_load_scripts() {
  if (is_singular()) {
    wp_enqueue_script(
      'brt-sample-script', //handle
      '/path/2/script.js', //source
      null, //no dependancies
      '1.0.1', //version
      true //load in html footer
    );

    the_post();
    $allPostMeta = get_post_custom();
    wp_localize_script('brt-sample-script', 'brtSampleVars',
    array(
      'petersTwitter' =&gt; $allPostMeta['myTwitter'][0],
      'joshsTwitter' =&gt; $allPostMeta['joshsTwitter'][0]
      )
    );
    rewind_posts();
  }
}

add_action('wp_print_scripts', 'brt_load_scripts');
?&gt;</code></pre>
<p>Only pages and posts have custom fields so the check at the start of the function has become <code>is_singlular()</code> to check the user is on either a post or a page, earlier we were testing if the user was anywhere on the front end. The arguments for <code>wp_enqueue_script</code> have not changed.</p>
<p><code>the_post()</code> needs to be called to start the loop and initiate the $post object so the associated custom fields can be accessed in the following line and put in an array.</p>
<p>With the custom fields easily available, the information can then be passed to <code>wp_localize_script()</code> as earlier demonstrated. The final step is to rewind the loop so next time <code>the_post()</code> is called, from either single.php or page.php, the post data is available.</p>
<p>The html output from the sample above would be:</p>
<pre><code>&lt;script type='text/javascript'&gt;
/* &lt;![CDATA[ */
var brtSampleVars = {
  petersTwitter: "@pwcc",
  joshsTwitter: "@sealfur"
};
/* ]]&gt; */
&lt;/script&gt;
&lt;script type='text/javascript' src='/path/2/script.js?ver=1.0.1'&gt;&lt;/script&gt;</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/javascript-localisation-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Delay Print Stylesheets Plugin</title>
		<link>http://bigredtin.com/behind-the-websites/delay-print-css-plugin/</link>
		<comments>http://bigredtin.com/behind-the-websites/delay-print-css-plugin/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 02:17:29 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=739</guid>
		<description><![CDATA[A few weeks ago I wrote a post in which I adapted an idea from a zOompf article to delay the loading of print stylesheets until after a web page has fully rendered. I've decided to convert the code from the original post into a plugin and add it to the WordPress plugin directory.]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I wrote a post in which I <a title="adapted an idea from a zOompf article" href="http://bigredtin.com/behind-the-websites/delay-loading-of-print-css/">adapted an idea from a zOompf article</a> to delay the loading of print stylesheets until after a web page has fully rendered. I finished that post with the following point/question:</p>
<blockquote><p>Another question to ask is whether all this is actually worth the effort – even when reduced through automation. On Big Red Tin, the print.css is 595 bytes, the delay in rendering is negligible.</p></blockquote>
<p>Chris and Jeff at <a title="Digging into WordPress" href="http://digwp.com">Digging into WordPress</a> picked up the article and posted it on their site. In turn it was picked up elsewhere and became the surprise hit of the summer at Big Red Tin. Not bad when one is shivering through a bitter Melbourne winter.</p>
<p>As a result of the interest, I decided to convert the code from the original post into a plugin and add it to the WordPress plugin directory.</p>
<h4>Further Testing</h4>
<p>As I warned in the original article, I&#8217;d tested the code in very limited circumstances and found it had worked. Fine for a code sample but not enough for a sub version-1.0-release plugin. Additional testing showed:</p>
<ol>
<li>Stylesheets intended for IE, through conditional comments, were loading in all browsers</li>
<li>When loading multiple stylesheets, the correct order was not maintained in all browsers</li>
</ol>
<p>If jQuery was available I wanted to use it for JavaScript event management otherwise I&#8217;d use purpose-written JavaScript. There&#8217;s no point, after all, of worrying about the rendering delay caused by 600-1000 bytes only to load a 71KB (or 24KB <a title="gzipped" href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_3.html">gzipped</a>) file in its place.</p>
<p>Other things I wanted to do included:</p>
<ol>
<li>Put the PHP in a class to reduce the risk of clashing function/class names</li>
<li>Put the JavaScript in its own namespace</li>
<li>Keep the output code as small as possible</li>
</ol>
<p>To support conditional comments for IE required adding each stylesheet within a separate &lt;script&gt; tag, using this method the output HTML takes the following form:</p>
<pre><code>&lt;script&gt;
  // add global print.css
&lt;/script&gt;
&lt;!--[if IE 6]&gt;
  &lt;script type='text/javascript'&gt;
    // add ie6 specific print.css
  &lt;/script&gt;
&lt;![endif]--&gt;</code></pre>
<p>This violates my aim to keep output as small as possible but footprint has to take second place to bug-free. I could have translated the code to use <a title="JavaScript conditional comments" href="http://en.wikipedia.org/wiki/Conditional_comment#Conditional_Comment_in_JScript">JavaScript conditional comments</a> by translating the IE version to the JavaScript engine it uses but this could lead to future-proofing problems.</p>
<p>To maintain the order of stylesheets, I added each event to an array of functions and then used a single event to loop through the array of functions. If jQuery is used, I add multiple events because jQuery runs events on a first in first out basis.</p>
<p>Putting the PHP in a class and the JavaScript in its own namespace is fairly self-explanatory. <a href="http://bit.ly/bBUXCh">Google is your friend</a> if you wish to read up further on this.</p>
<p>Minimising the footprint was also a simple step. I wrote the JavaScript out in full with friendly variable names. Once I was happy with the code, I ran the code through <a href="http://refresh-sf.com/yui/">the YUI JavaScript compressor</a>, commented out the original JavaScript in the plugin file and output the compressed version in its place.</p>
<p>The JavaScript is output inline (within the HTML) to avoid additional HTTP requests. I was in two minds about this because browser caching is lost in the process. So it may change in a later version.</p>
<p>I&#8217;ve worked out another way to keep the footprint small. Rather than creating a function to pass the stylesheet&#8217;s URL and ID to <code>brt_print.add(url, id)</code>, I wrote out the full function for each style sheet. I&#8217;ll fix that in the next release.</p>
<p>You can <a title="download" href="http://wordpress.org/extend/plugins/delay-print-css/download/">download</a> the <a title="Delay Print CSS Plugin" href="http://http://wordpress.org/extend/plugins/delay-print-css/">Delay Print CSS Plugin</a> from the WordPress plugin repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/delay-print-css-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delay loading of print CSS</title>
		<link>http://bigredtin.com/behind-the-websites/delay-loading-of-print-css/</link>
		<comments>http://bigredtin.com/behind-the-websites/delay-loading-of-print-css/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 05:03:50 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=689</guid>
		<description><![CDATA[Recently I stumbled across an article detailing browser performance with the CSS print media type. In most recent browsers the print stylesheet held up rendering.

The article suggested a solution, which I decided to automate for WordPress.]]></description>
			<content:encoded><![CDATA[<p>Recently I stumbled across an article on zOompf <a title="detailing browser performance with the CSS print media type" href="http://zoompf.com/blog/2009/12/browser-performance-problem-with-css-print-media-type">detailing browser performance with the CSS print media type</a>. In most recent browsers, Safari being the exception, the print stylesheet held up rendering of the page.</p>
<p>The zOomph article suggests a solution, to load print stylesheets using JavaScript once the page has  rendered (ie, on the <code>window.onload</code> event), with a backup for the JavaScript impaired. You can see their code in the original article.</p>
<h4>Automating the task for WordPress</h4>
<p>Most sites I develop are in WordPress so I decided to automate the process. This relies on using <code>wp_enqueue_style</code> to register the stylesheets:</p>
<pre><code>function enqueue_css(){
  if (!is_admin()){
    wp_enqueue_style (
      'bigred-print', /* handle */
      '/path-to/print.css', /* source */
      null, /* no requirements */
      '1.0', /* version */
      'print' /* media type */
    );
  }
}
add_action('wp_print_styles', 'enqueue_css');</code></pre>
<p>The above code will output the following HTML in the header:</p>
<pre><code>&lt;link rel='stylesheet' id='bigred-print-css'  href='/path-to/print.css?ver=1.0' type='text/css' media='print' /&gt;</code></pre>
<p>The first step is to wrap the above html in noscript tags, the WordPress <a title="filter" href="http://codex.wordpress.org/Plugin_API#Filters">filter</a> <code>style_loader_tag</code> is ideal for this.</p>
<pre><code>function js_printcss($tag, $handle) {
  global $wp_styles;
  if ($wp_styles-&gt;registered[$handle]-&gt;args == 'print') {
    $tag = "&lt;noscript&gt;" . $tag . "&lt;/noscript&gt;";
  }
  return $tag;
}
add_filter('style_loader_tag', 'js_printcss', 5, 2);</code></pre>
<p>The filter runs for all stylesheets, regardless of media type, so the function checks for print stylesheets and wraps them in the <code>noscript</code> tag, other media types are left alone.</p>
<p>The first two arguments are the filter and function names respectively, the third argument specifies the timing (5 is the default) and the final argument tells WordPress how many arguments to pass to the filter – two – in this case $tag and $handle.</p>
<p>With the new filter, WordPress now outputs following HTML in the header:</p>
<pre><code>&lt;noscript&gt;
&lt;link rel='stylesheet' id='bigred-print-css'  href='/path-to/print.css?ver=1' type='text/css' media='print' /&gt;
&lt;/noscript&gt;</code></pre>
<p>The next step is to add the JavaScript to load the stylesheets, we can do this by changing our original function,  <code>js_printcss</code>, and making use of a global variable:</p>
<pre><code>$printCSS = '';

function js_printcss($tag, $handle){
  global $wp_styles, $printCSS;
  if ($wp_styles-&gt;registered[$handle]-&gt;args == 'print') {

    $tag = "&lt;noscript&gt;" . $tag . "&lt;/noscript&gt;";

    preg_match("/&lt;\s*link\s+[^&gt;]*href\s*=\s*[\"']?([^\"' &gt;]+)[\"' &gt;]/", $tag, $hrefArray);
    $href = $hrefArray[1];

    $printCSS .= "var cssNode = document.createElement('link');";
    $printCSS .= "cssNode.type = 'text/css';";
    $printCSS .= "cssNode.rel = 'stylesheet';";
    $printCSS .= "cssNode.href = '" . esc_js($href) . "';";
    $printCSS .= "cssNode.media = 'print';";
    $printCSS .= "document.getElementsByTagName(\"head\")[0].appendChild(cssNode);";
  }
  return $tag;
}</code></pre>
<p>The code creates the PHP variable <code>$printCSS</code> globally, which is then called into the function using the global command.</p>
<p>After wrapping the tag in the <code>noscript</code> tags, the new function uses a regular expression to extract the URL of the stylesheet from the link tag and placing it the variable <code>$href</code>.</p>
<p>Having extracted the stylesheet&#8217;s URL, the function then appends the required JavaScript to the PHP global variable <code>$printCSS</code>.</p>
<p>The final step is to add the JavaScript to the footer of the HTML using the wp_footer action in WordPress. The PHP to do this is:</p>
<pre><code>function printCSS_scriptTags(){
  global $printCSS;
  if ($printCSS != '') {
    echo "&lt;script type='text/javascript'&gt;\n";
    echo "window.onload = function(){\n";
    echo $printCSS;
    echo "}\n&lt;/script&gt;";
  }
}

add_action('wp_footer', 'printCSS_scriptTags');</code></pre>
<p>The above code uses <code>window.onload</code> as dictated in the original article. A better method would be to use an event listener to do the work, for those using jQuery we would change the function to:</p>
<pre><code>function printCSS_scriptTags(){
  global $printCSS;
  if ($printCSS != '') {
    echo "&lt;script type='text/javascript'&gt;\n";
    echo "jQuery(window).ready(function(){\n";
    echo $printCSS;
    echo "});\n&lt;/script&gt;";
 }

}
add_action('wp_footer', 'printCSS_scriptTags');</code></pre>
<p>The above solution had been tested for very limited circumstances only and found to have worked. Were I to use the function in a production environment I would undertake further testing.</p>
<p>Another question to ask is whether all this is actually worth the effort – even when reduced through automation. On Big Red Tin, the print.css is 595 bytes, the delay in rendering is negligible.</p>
<p><strong>Update Aug 23, 2010</strong>: Fixed a type in the code block redefining <code>js_printcss</code>.</p>
<p><strong>Update Aug 27, 2010</strong>: I&#8217;ve decided to release this as a plugin, get the skinny and the plugin from the <a href="http://bigredtin.com/behind-the-websites/delay-print-css-plugin/">followup article</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/delay-loading-of-print-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript the WordPress Way / Part 2</title>
		<link>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/</link>
		<comments>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 00:00:20 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_enqueue_script]]></category>
		<category><![CDATA[wp_enqueue_style]]></category>
		<category><![CDATA[wp_register_script]]></category>
		<category><![CDATA[wp_register_style]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=499</guid>
		<description><![CDATA[In Part 1 we introduced the <code>wp_register_script</code> and <code>wp_enqueue_script</code> functions developed to avoid JavaScript conflicts.

In this section we’ll deal with a more complicated example. We’ll also take what we’ve learnt about including JavaScript and apply it to our CSS.]]></description>
			<content:encoded><![CDATA[<p>In <a title="Part 1" href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/">Part 1</a> we discussed the conflicts that can occur on a WordPress site if themes and plugins add JavaScript using &lt;script&gt; tags. We introduced the <code>wp_register_script</code> and <code>wp_enqueue_script</code> functions developed to avoid these conflicts.</p>
<p>In this section we&#8217;ll deal with a <a title="more complicated example" href="#jswp2complicated">more complicated example</a> and <a title="use Google's AJAX libraries API" href="#jswp2googleapi">use Google&#8217;s AJAX libraries API</a> to lower your bandwidth costs. We&#8217;ll also take what we&#8217;ve learnt about including JavaScript and <a title="apply it to our CSS" href="#jswpcss">apply it to our CSS</a>.<span id="more-499"></span></p>
<h4 id="jswp2complicated">A more complicated example</h4>
<p>The <a title="sample code yesterday" href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/#jswpregister">sample code yesterday</a> included a simple example of using the same JavaScript file throughout your site. Let&#8217;s spice things up slightly by introducing the following circumstance. Consider that you have three JavaScript files:</p>
<ul>
<li><strong>base.js</strong> is the base JavaScript file to be used throughout the site. It uses the jQuery library and must load in the HTML &lt;head&gt;;</li>
<li><strong>posts.js</strong> loads on pages and posts and only calls functions from the base JavaScript file but doesn&#8217;t use the jQuery library directly. It can load in the HTML footer.</li>
<li><strong>archives.js</strong> loads on all other pages throughout your site (except admin). It calls functions from your base JavaScript file and makes direct use of the jQuery library. It can load in the HTML footer.</li>
</ul>
<p>In this situation, you could change your base.js to use another library – say, Prototype – and it would have no effect on the posts.js. So, posts.js can be said not to depend on jQuery directly.</p>
<p>Changing libraries would require edits to the third file, in which case archives.js does depend on jQuery directly.</p>
<h5>Registering the Files</h5>
<p>The code to register those JavaScript files and conditions with WordPress is:</p>
<pre><code>wp_register_script (
  'mytheme-base', /* handle */
  get_bloginfo('template_url') . '/base.js', /* source */
  array('jquery'), /* requires jQuery */
  1.0 /* version 1.0 */
);

wp_register_script (
  'mytheme-posts', /* handle */
  get_bloginfo('template_url') . '/posts.js', /* source */
  array('mytheme-base'), /* requires base.js */
  1.0, /* version 1.0 */
  true /* load in footer */
);

wp_register_script (
  'mytheme-archives', /* handle */
  get_bloginfo('template_url') . '/archives.js', /* source */
  array('jquery', 'mytheme-base'), /* requires jQuery &amp; base.js */
  1.0, /* version 1.0 */
  true /* load in footer */
);</code></pre>
<p>This code says to WordPress:</p>
<ul>
<li> If loading posts.js, also load base.js;</li>
<li> If loading archives.js, also load jQuery and base.js;</li>
<li> If loading base.js, also load jQuery;</li>
</ul>
<p>Each of these are separate checks (if) rather than a series of options (if, else if).</p>
<h5>Enqueueing the files</h5>
<p>All the hard work has been done registering the files, WordPress knows what each file requires to work and where in the HTML (&lt;head&gt; or footer) the &lt;script&gt; tags should be inserted.</p>
<p>Now it&#8217;s a simple process of checking if the visitor is loading a post or page and requesting the appropriate file.</p>
<p>We need to delay the check slightly, otherwise WordPress won&#8217;t know if the user is on a page, post or elsewhere. The first step is to enclose the check in a function.</p>
<pre><code>function que_scripts(){
  if (!is_admin()) { //visitors only
    if (is_singular()) { // pages &amp; posts
      wp_enqueue_script('mytheme-posts');
    }
    else {
      wp_enqueue_script('mytheme-archives');
    }
  }
}</code></pre>
<p>Then tell WordPress when to execute the function. For this we use the <a title="action" href="http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters">action</a> wp_print_scripts. This runs the code above before WordPress attempts to output the <code>&lt;script&gt;</code> tags to your HTML.</p>
<pre><code>add_action('wp_print_scripts', 'que_scripts')</code></pre>
<p>There is no need to enqueue jQuery or base.js directly. WordPress knows to add those to the HTML based on the dependancies listed when registering the files.</p>
<h4 id="jswp2googleapi">Using the Google AJAX Libraries API</h4>
<p>Loading all these libraries from your own server may seem like a waste of your bandwidth considering you could be loading the libraries directly from Google&#8217;s servers.</p>
<p>With a bit of luck, your visitor will have Google&#8217;s version of the libraries in their browser&#8217;s cache already and <a title="your site will load a little quicker" href="http://bigredtin.com/behind-the-scenes/caching-on-the-google-ajax-libraries-api/">your site will load a little quicker</a>.</p>
<p>It&#8217;s a bit messy to do this yourself, especially since jQuery needs to be kept in noConflict mode. Fortunately <a title="Jason Penny" href="http://jasonpenney.net/">Jason Penny</a> has developed a plugin, called “<a title="Use Google Libraries" href="http://wordpress.org/extend/plugins/use-google-libraries/">Use Google Libraries</a>”, to do the hard work for you.</p>
<p>“Use Google Libraries” is the first plugin I add when setting up sites for me and my clients.</p>
<h4 id="jswp2superfab">Making our super-broken example super-fabulous</h4>
<p>If, when developing their theme and plugin, George and Richard <a title="from our example in Part 1" href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/#jswpproblem">from our  example in Part 1</a>, had used the <code>wp_register_script</code> and <code>wp_enqueue_script</code> WordPress functions to load their JavaScript, WordPress would know that only one version of jQuery needs to be loaded for both the theme and the plugin to work.</p>
<p>As WordPress releases come in fits and bursts, sometimes the version of the JS libraries included with WordPress fall behind the current version of the library available otherwise.</p>
<p>This is a small price to pay for the knowledge that our sites will remain super-fabulous rather than super-broken, at least when it comes to the JavaScript.</p>
<h4 id="jswpcss">A bonus for CSS</h4>
<p>By now the reasons for using <code>wp_register_script</code> and <code>wp_enqueue_script</code> should be clear. If you want to go that one step further, you can use the equivalent functions for your CSS.</p>
<p>As CSS should always be loaded in the HTML &lt;head&gt;, there is no option to load the CSS files in the footer of the HTML. Otherwise the CSS functions take the same arguments as their JavaScript equivalents. The CSS functions are:</p>
<ul>
<li>wp_register_style</li>
<li>wp_enqueue_style</li>
</ul>
<p>The four parameters they take are:</p>
<dl>
<dt>$handle</dt>
<dd>(required for both functions) Name of the style sheet</dd>
<dt>$source</dt>
<dd>(required for <code>wp_register_style</code>) – URL of the script. Also required by <code>wp_enqueue_style</code> if the style is unregistered.</dd>
<dt>$dependancies</dt>
<dd>(optional) – an array of handles the style sheet depends on</dd>
<dt>$version</dt>
<dd>(optional) – version number</dd>
</dl>
<p>WordPress doesn&#8217;t include any style sheets bases, such as YUI CSS, in the default distribution, so the main purpose of the CSS functions is to allow you to keep track of your version numbers and to call in style sheets only as they&#8217;re needed.</p>
<h4>Conditional comments</h4>
<p>Unfortunately the WordPress functions for JavaScript do not allow for conditional comments, so scripts targeting older versions of IE (okay, I&#8217;m referring to PNG fixers for IE6) need to be coded directly into your theme&#8217;s header.php or footer.php.</p>
<p>On the other hand, the CSS functions do allow for conditional comments, in a round-about fashion. Let&#8217;s register a CSS file we&#8217;ll be targeting at IE6.</p>
<pre><code>wp_register_style (
  'mytheme-ie6', /* handle */
  get_bloginfo('template_url') . '/ie6.css', /* source */
  array('mytheme-css'), /* requires mytheme-css (not shown) */
  1.0 /* version 1.0 */
);</code></pre>
<p>Behind the scenes WordPress is adding the stylesheet to the object $wp_styles. To add the conditional comments you need to access the object more directly. To target the above to IE6, the command is:</p>
<pre><code>$wp_styles-&gt;registered['mytheme-ie6']-&gt;extra['conditional'] = 'IE 6';</code></pre>
<p>If, as in the complicated example above, you need to check where a user is before enqueueing your stylesheet, again you need to enclose the enqueueing command in a function and run it on an action, in this case the action <code>wp_print_styles</code>.</p>
<p>For example, to output the IE6 stylesheet above on pages and posts only, the full code would be:</p>
<pre><code>function que_styles(){
  if (!is_admin()) { //visitors only
    if (is_singluar() {
      wp_enqueue_style('mytheme-ie6');
    }
  }
}

add_action('wp_print_styles', 'que_styles')</code></pre>
<h4>Conclusion</h4>
<p>The methods described in this tutorial may appear to complicate matters for little pay off, especially if you&#8217;re developing a theme or plugin for your own site.</p>
<p>Replacing your &lt;script&gt; tags with the code above, it is true, will have almost no effect on the HTML or JavaScript output by WordPress. What&#8217;s worse, if you use the $ alias for jQuery in your JavaScript, you&#8217;ll need to edit both the theme&#8217;s php and JavaScript files.</p>
<p>These functions, <code>wp_register_script</code> and <code>wp_enqueue_script</code>, aren&#8217;t about now, however, they&#8217;re about “then”.</p>
<ul>
<li>Then&#8230; I activated this plugin and my JavaScript stopped working.</li>
<li>Then&#8230; I changed the theme and my favourite plugin stopped working</li>
<li>Then&#8230; I looked at the source code, and two copies of jQuery were being sent to every visitor</li>
</ul>
<p>The WordPress developers have seen the problems JavaScript conflicts can cause, and developed these functions to make your theme or plugin more robust. It&#8217;s the added robustness, for such little effort, that make these functions worth using.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript the WordPress Way / Part 1</title>
		<link>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/</link>
		<comments>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 05:44:42 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_enqueue_script]]></category>
		<category><![CDATA[wp_register_script]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=488</guid>
		<description><![CDATA[Problems arise when your theme or plugin both use the same JavaScript library or if Prototype and jQuery are both used on the same site. 

Two of the most important WordPress functions are often ignored by WordPress theme and plugin developers. Introducing: <code>wp_register_script</code> and <code>wp_enqueue_script</code>.]]></description>
			<content:encoded><![CDATA[<p>Two of the most important WordPress functions are often ignored by WordPress theme and plugin developers. This is the fault of the functions themselves, they need to improve their PR and hire better publicists.</p>
<p>It&#8217;s also possible your theme or plugin will work perfectly well without these functions on its own. Problems <em>will</em> arise when your theme or plugin both use the same JavaScript library or if Prototype and jQuery are both used on the same site.</p>
<p>These functions are used to add JavaScript to the html, either in the head or the footer.</p>
<p>Introducing <code>wp_register_script</code> and <code>wp_enqueue_script</code><span id="more-488"></span></p>
<p>For the rest of this article I&#8217;ll refer to development of <em>your theme</em>. The tutorial applies equally to development of <em>your plugin</em>, but there&#8217;s only so many times the phrase “your theme or plugin” can appear in a page.</p>
<p>In this first part, we&#8217;ll look at:</p>
<ul>
<li>Defining <a title="the problem" href="#jswpproblem">the problem</a>;</li>
<li>The <a title="libraries included" href="#jswplibraries">libraries included</a> with WordPress;</li>
<li>Defining <a title="the functions" href="#jswpfunctions">the functions</a> we&#8217;ll be using and;</li>
<li>The functions for <a title="registering" href="#jswpregister">registering</a> and <a title="enqueueing" href="#jsenqueue">enqueueing</a> your scripts.</li>
</ul>
<h4 id="jswpproblem">The problem defined</h4>
<p>Imagine you&#8217;ve used a theme on your WordPress site for months or years without a problem. It uses jQuery to add AJAX functionality to your site and it&#8217;s called <em>George&#8217;s Super Theme</em>.</p>
<p>You decide to add a plugin to your WordPress site and add more AJAXy goodness to your site. This plugin also uses jQuery for the JavaScript and it&#8217;s called <em>Richard&#8217;s Fabulous Plugin</em>.</p>
<p>You activate the plugin and instead of the super-fabulous result you were looking for you end up with a super-broken site. None of the JavaScript is working and your visitors are stuck on the home page, with nowhere to go.</p>
<p>Looking at the HTML of your WordPress site, you see the following lines:</p>
<pre><code>&lt;script type='text/javascript'
  src='/wp-content/themes/george/jquery-1.4.2.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript'
  src='/wp-content/plugins/richard/jquery-1.3.2.js'&gt;&lt;/script&gt;</code></pre>
<p>Both the theme and the plugin are attempting to load the jQuery library. each uses a different version, which further complicates matters.</p>
<p>In this particular case, the theme is expecting jQuery version 1.4.2 and the plugin is replacing it with version 1.3.2. Anytime the theme attempts to use a function added to jQuery in version 1.4 or later, an error occurs preventing the JavaScript from running.</p>
<p>Even if the site was running as expected, it&#8217;s a waste of your users&#8217; bandwidth.</p>
<p>Fortunately the WordPress developers envisioned this problem and sort to solve it by including the most common JavaScript libraries with WordPress and developing the functions that are the subject of this tutorial.</p>
<h4 id="jswplibraries">Libraries included with WordPress</h4>
<p>WordPress comes with a number of JavaScript libraries pre-registered, so you need not include these libraries in your theme. All you need to do is include them as dependencies of your custom JavaScript and WordPress will do the work.</p>
<p>Some of the libraries included with WordPress are:</p>
<ul>
<li>jQuery – with the handle &#8216;jquery&#8217;, runs in <a title="no conflict mode" href="http://api.jquery.com/jQuery.noConflict/">no conflict mode</a>
<ul>
<li>WP 2.9.2 includes version 1.3.2 of jQuery</li>
<li>WP 3.0 will include version 1.4.2 of jQuery</li>
</ul>
</li>
<li>Scriptaculous – with the handle &#8216;scriptaculous&#8217;</li>
<li>SWFObject – with the handle &#8216;swfobject&#8217;</li>
<li>Prototype – with the handle &#8216;prototype&#8217;</li>
<li>A full list of pre-registered script can be found in the <a title="codex" href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress">codex</a>.</li>
</ul>
<h4 id="jswpfunctions">The functions defined</h4>
<p>The <a title="WordPress codex defines wp_enqueue_script" href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">WordPress codex defines <code>wp_enqueue_script</code></a> as “a safe way of adding JavaScripts (sic) to a WordPress generated page”.</p>
<p><code>wp_register_script</code> is similar and can be thought of as a safe way of introducing JavaScript to WordPress, without adding it to the generated page.</p>
<p>The difference between these functions are fully explained in the sections <a title="Registering your scripts" href="#jswpregister"><em>Registering your scripts</em></a> and <em><a title="Enqueueing your scripts" href="#jsenqueue">Enqueueing your scripts</a></em> below.</p>
<p>Both functions take the same five parameters:</p>
<dl>
<dt>$handle</dt>
<dd>(required for both functions) Name of the script</dd>
<dt>$source</dt>
<dd>(required for <code>wp_register_script</code>) – URL of the script. Also required by <code>wp_enqueue_script</code> if the script is unregistered.</dd>
<dt>$dependancies</dt>
<dd>(optional) – an array of handles the script depends on</dd>
<dt>$version</dt>
<dd>(optional) – version number</dd>
<dt>$in_footer</dt>
<dd>(true/false, optional) – if true, place script above the HTML &lt;/body&gt; tag, otherwise place the script in the HTML &lt;head&gt;</dd>
</dl>
<p>These functions only work as expected if both <code>wp_head()</code> and <code>wp_footer()</code> are included in header.php and footer.php respectively.</p>
<h4 id="jswpregister">Registering your scripts – <code>wp_register_script</code></h4>
<p><code>wp_register_script</code> gives WordPress knowledge of your script without adding it to your HTML. <strong>Used by itself, it has no effect on your HTML whatsoever</strong>.</p>
<p>You can use it if you wish to register your various JavaScript files in one block of php within your theme&#8217;s functions.php file, allowing you to update version numbers, dependancies and handles in one place.</p>
<p>If the use of the JavaScript depends on particular conditions being met, such as the user being on a particular post, you can then enqueue your scripts elsewhere without losing track of where you should update version numbers.</p>
<p>If your theme&#8217;s JavaScript can be loaded in the footer and relies on the jQuery library, you would register your script with the following code:</p>
<pre><code>wp_register_script (
  'mytheme-custom', /* handle WP will know JS by */
  get_bloginfo('template_url') . '/custom.js', /* source */
  array('jquery'), /* requires jQuery */
  1.0, /* version 1.0 */
  true /* can load in footer */
);</code></pre>
<p>At this stage, this has no effect on your HTML, you need to use <code>wp_enqueue_script</code> to do that.</p>
<h4 id="jsenqueue">Enqueueing your scripts – <code>wp_enqueue_script</code></h4>
<p><code>wp_enqueue_script</code> outputs your JavaScript to the HTML of the page, either as a result of the w<code>p_head()</code> or the <code>wp_footer()</code> call in your PHP. If you have pre-registered your script (as above), the full code is:</p>
<pre><code>if (!is_admin()) {
  wp_enqueue_script('mytheme-custom');
}</code></pre>
<p>Combined with the code above, this line of code will load both jQuery and your theme&#8217;s JavaScript. The only reference to jQuery is needed in the dependancies. There is no need to register or enqueue it yourself.</p>
<p>The check that your visitor is outside of the admin area, <code>!is_admin()</code> – is important, as these functions can be used to add JavaScript to both the visitor and admin areas.</p>
<p>More often than not, you&#8217;ll want to leave the admin area alone.</p>
<h5>A shortcut</h5>
<p>It&#8217;s possible to enqueue your script without registering it first. This comes in handy if your theme uses a single JavaScript that is loaded on every page, outside the admin area, of your blog.</p>
<p>The code to do this is:</p>
<pre><code>if (!is_admin()) {
  wp_enqueue_script (
    'mytheme-custom', /* handle WP will know JS by */
    get_bloginfo('template_url') . '/custom.js', /* source */
    array('jquery'), /* requires jQuery */
    1.0, /* version 1.0 */
    true /* can load in footer */
  );
}</code></pre>
<p>Again, our custom code requires the jQuery Library.</p>
<h4>Aside: noConflict for jQuery and Prototype</h4>
<p>As mentioned earlier, jQuery runs in <a title="noConflict mode" href="http://api.jquery.com/jQuery.noConflict/">noConflict mode</a>. The reason being that in jQuery&#8217;s standard mode it includes the <code>$</code> variable as an alias. WordPress also includes Prototype, which includes the JavaScript variable <code>$</code>.</p>
<p>Imagine if your theme uses jQuery and you add a WordPress plugin that makes use of Prototype, whichever is loaded second would override the <code>$</code> variable of the former. Either your theme&#8217;s or the WordPress plugin&#8217;s JavaScript would crash as a result.</p>
<p>If your theme uses jQuery and you&#8217;d still like to make use of the <code>$</code> alias, it&#8217;s possible to do so without breaking the plugin&#8217;s JavaScript. In your JavaScript file, just wrap your code between these two lines:</p>
<pre><code>(function($) {
  /* your code */
})(jQuery);</code></pre>
<p>– <a title="source" href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers">source</a></p>
<p>Under no circumstances should you reintroduce the <code>$</code> alias for jQuery, it could lead to problems down the track.</p>
<p>A shorter aside: throughout this post I&#8217;m using the example of JavaScript requiring jQuery. The reasons are two-fold: a) it&#8217;s the library we use at <a title="Soupgiant" href="http://soupgiant.com">Soupgiant</a> and <a href="http://bigredtin.com">Big Red Tin</a> and, b) as Twitter&#8217;s Dustin Diaz said recently of jQuery: &#8216;<a title="they won" href="http://boagworld.com/technology/dustin-diaz">they won</a>&#8216;.</p>
<h4>Now Available: <a href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/" title="Part Two">Part Two</a></h4>
<p>In part 2 of this tutorial we&#8217;ll <a href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/#jswp2complicated">further illustrate dependancies</a>, <a href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/#jswp2googleapi" title="lower your bandwidth costs">lower your bandwidth costs</a> and show you how to use <a href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-2/#jswpcss" title="similar functions to load your stylesheets">similar functions to load your stylesheets</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Charging for themes? Do the right thing!</title>
		<link>http://bigredtin.com/behind-the-websites/charging-for-themes-do-the-right-thing/</link>
		<comments>http://bigredtin.com/behind-the-websites/charging-for-themes-do-the-right-thing/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 00:23:42 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[rants]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://soupgiant.com/?p=240</guid>
		<description><![CDATA[Of all the Wordpress functions, I think wp_register_script, wp_register_style, wp_enqueue_script, and, wp_enqueue_style are the most elegant.]]></description>
			<content:encoded><![CDATA[<p>Of all the WordPress functions, I think <code>wp_register_script</code>, <code>wp_register_style</code>, <code>wp_enqueue_script</code>, and, <code>wp_enqueue_style</code> are the most elegant. It&#8217;s possible to get away with using only the <code>wp_enqueue_*</code> functions, but I prefer to use both for a little bit more control.</p>
<p>For the uninitiated, these functions allow you to add JavaScript and CSS files to the header (or footer, in later versions of <abbr title="WordPress">WP</abbr>) without running of the risk of another plugin adding the same file, that is, you avoid the following:</p>
<pre><code>&lt;script type="text/javascript" source="http://.../a-plugin/jQuery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" source="http://.../a-plugin/plugin.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" source="http://.../my-theme/jQuery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" source="http://.../my-theme/theme.js"&gt;&lt;/script&gt;</code></pre>
<p>If you develop themes or plugins for WordPress and are unaware of these functions, you should refer to the codex to get the low down on at least <code><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">wp_enqueue_script</a></code>, and <code><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style">wp_enqueue_style</a></code> &#8211; these register and queue the files, the <code>wp_register_*</code> functions register the file ready to be queued for output.</p>
<h4>The Rant</h4>
<p>Despite the value these functions can add to themes and plugins, they are under-utilised in development. The functions have wider adoption in plug-in development, but it&#8217;s still well below 100%. In theme development adoption is virtually non-existent (at least in the themes I&#8217;ve used).</p>
<p>This failure to code properly is evenly spread across both free and paid themes. I?m happy to look the other way for free themes. After all, the motivation behind the theme may have been to learn WordPress development.</p>
<p>In commercial themes, particularly those above the US$30-$35 price point, it&#8217;s downright frustrating that these products haven&#8217;t been developed properly.</p>
<p>When someone purchases a theme, they shouldn&#8217;t have to debug the product to find out why a JavaScript framework is being included twice. The reason for the purchase is that they either don&#8217;t want to, or know how to, develop.</p>
<h4>Avoiding costly mistakes</h4>
<p>Evaluating a commercial theme on behalf of a client triggered this rant. At $195, it&#8217;s quite expensive in the world of WordPress. As I was viewing the source code of the online demo, I discovered the <em>faux pas</em>. We&#8217;ll advise against using that theme as a starting point. In fact, within short time we&#8217;d found a theme $195 cheaper that would do the job.</p>
<p>Looking at the source code of a demo site is usually enough to tell you how the theme is inserting its scripts:</p>
<ul>
<li>Both &lt;link&gt; and &lt;script&gt; tags will use single quotes around attributes (&#8216;), rather than double quotes (&#8220;),</li>
<li>CSS &lt;link&gt; tags will have an ID attribute ending in -css, eg: id=&#8217;shadowbox-css-css&#8217;, and,</li>
<li>JavaScript and CSS files include the version number as a query string eg jQuery.js?ver=1.3.2 (although a hook can be used to remove this)</li>
</ul>
<p>Making sure that the <code>wp_register_script</code>, <code>wp_register_style</code>, <code>wp_enqueue_script</code>, and, <code>wp_enqueue_style</code> are included properly will save time, bandwidth and also avoid some detrimental conflicts.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/charging-for-themes-do-the-right-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Partying like it&#8217;s 1999</title>
		<link>http://bigredtin.com/behind-the-websites/partying-like-its-1999/</link>
		<comments>http://bigredtin.com/behind-the-websites/partying-like-its-1999/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 00:23:00 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://soupgiant.com/?p=226</guid>
		<description><![CDATA[Within a couple of months of writing that I prefered my own base JavaScript file to an exisiting framework, I was a convert to jQuery. Why the turnaround?]]></description>
			<content:encoded><![CDATA[<p>A little under twelve months ago I wrote a post on a JavaScript base file I&#8217;d set up:</p>
<blockquote><p>I could have used an existing frame work, such as the increasingly popular <a href="http://jquery.com/">jQuery</a>; I decided to use my own, which contains only the very basics, rather than have a larger file containing <em>rarely used functions</em></p>
<p>&#8211;<a title="Base JavaScript File" href="http://bigredtin.com/javascript/base-js-file/">source</a> (emphasis added for this post)</p></blockquote>
<p>Within a couple of months of writing that, I was a convert to jQuery. There was no single reason for my conversion, from memory some of the reasons where:</p>
<ul>
<li>the selector engine,</li>
<li>plugin availability,</li>
<li>documentation — both <a title="official" href="http://docs.jquery.com">official</a> and unofficial tutorials, and,</li>
<li>lazyness</li>
</ul>
<p>It&#8217;s the last one that probably had the biggest influence, I&#8217;m too lazy to reinvent the wheel, and without using a framework that&#8217;s what I was going to be doing. Frequently. Examples in blogs I read, weather it be group blogs, such as <a title="A List Apart" href="http://alistapart.com">A List Apart</a>, or one person shows, such as <a title="CSS-Tricks" href="http://css-tricks.com/">CSS-Tricks</a> were all beginning to use jQuery. If I were to stick to my guns and not use a framework, I&#8217;d be doing a lot of rewriting.</p>
<p>As for the functions that are rarely used? It&#8217;s really only the Ajax group of functions that I&#8217;ve never used, for the simple reason that I don&#8217;t like Ajax. In many of the situations it&#8217;s used, it is often fanciness without necessary function. Then again, ask me about Ajax in a couple of months&#8217; time.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/partying-like-its-1999/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Including WordPress&#8217;s comment-reply.js (the right way)</title>
		<link>http://bigredtin.com/behind-the-websites/including-wordpress-comment-reply-js/</link>
		<comments>http://bigredtin.com/behind-the-websites/including-wordpress-comment-reply-js/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 03:15:41 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[threaded comments]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://peterwilson.cc/?p=134</guid>
		<description><![CDATA[Since threaded comments were enabled in Wordpress 2.7, most themes check if the visitor is browsing either a page or a post and adds the JavaScript required for threaded comments if they are.

I prefer a slight variation]]></description>
			<content:encoded><![CDATA[<p>Since threaded comments were enabled in WordPress 2.7, most themes include the following line in header.php</p>
<pre><code>&lt;?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?&gt;</code></pre>
<p>This code checks if the visitor is browsing either a page or a post and adds the JavaScript required for threaded comments if they are.</p>
<p>I prefer a slight variation</p>
<pre><code>&lt;?php
if ( is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
  wp_enqueue_script( 'comment-reply' );
?&gt;</code></pre>
<p>My variation checks if the visitor is browsing either a page or a post, if comments are open for the entry, and finally, if threaded comments are enabled. If all of these conditions are met, the JavaScript required for threaded comments is added.</p>
<p>If you run your wp_enqueue_script calls in functions.php, as I do, this is the code to use:</p>
<pre><code>&lt;?php
function theme_queue_js(){
  if (!is_admin()){
    if ( is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
      wp_enqueue_script( 'comment-reply' );
  }
}
add_action('wp_print_scripts', 'theme_queue_js');
?&gt;</code></pre>
<p>The call is added to the wp_print_scripts action as <code>is_singular</code> and <code>comments_open</code> are unknown during the init action.</p>
<p><strong>Updated Aug 15 &#8217;09</strong>: Fixed two typos in the final code sample</p>
<p><strong>Updated Jun 13 &#8217;10</strong>: Changed action in final code block from get_header to wp_print_scripts. Either action works but wp_print_scripts has a closer relationship with the task being performed than the former.</p>
<p><em>Props to <a title="Digging Into WordPress" href="http://digwp.com">Digging Into WordPress</a> for the &#8230;the right way&#8230; terminology:)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/including-wordpress-comment-reply-js/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Caching on the Google AJAX Libraries API</title>
		<link>http://bigredtin.com/behind-the-websites/caching-on-the-google-ajax-libraries-api/</link>
		<comments>http://bigredtin.com/behind-the-websites/caching-on-the-google-ajax-libraries-api/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 03:49:41 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://peterwilson.cc/?p=106</guid>
		<description><![CDATA[Using the Google Ajax Libraries API, there are several options for specifying the version numbers of the library you wish to use, for example, three URLs point to the latest version of jQuery.]]></description>
			<content:encoded><![CDATA[<p>Using the <a title="Google Ajax Libraries API" href="http://code.google.com/apis/ajaxlibs/">Google AJAX Libraries API</a>, there are several options for specifying the version numbers of the library you wish to use, for example the following URLs all point to the latest version &#8211; at the time of writing &#8211; of jQuery.</p>
<ol>
<li><a href="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js</a>,</li>
<li><a href="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js</a>, and,</li>
<li><a href="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js</a></li>
</ol>
<p>The first uses the latest release in the version 1 tree, the second the latest in the version 1.3 tree, and the third the release 1.3.0 exactly. What I found interesting &#8211; and discovered accidently &#8211; is the browser caching times for each of these urls; the first two are cached for an hour, the third for twelve months.</p>
<p>The vastly different caching times make perfect sense, in the first two cases, the developer is expecting an upgrade to the latest version, and doesn&#8217;t want to wait up to twelve months for it; in the third case the developer doesn&#8217;t expect an upgrade so an extended caching period has no effect.</p>
<p>To take full advantage of the Google servers, as described in a recent article by <a title="3 reasons why you should let Google host jQuery for you" href="http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/">Dave Ward</a>, the caching times suggest it&#8217;s best to specify the full version of the library you wish to use.</p>
<h4>google.load Method &#8211; Update Jan 24, 2009</h4>
<p><span id="more-106"></span>Further investigation shows that the Google load method uses the full URL for the less specific version requests, for example the following will load <a href="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js">ajax.googleapis.com[...]/1.3.1/jquery.min.js</a>* ? at the time of writing ? and cache the file for twelve months.</p>
<pre><code>&lt;script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
&lt;script&gt;
  /* &lt;![CDATA[ */
  google.load("jquery", "1"); //latest version in the v1 tree
  /* ]]&gt; */
&lt;/script&gt;</code></pre>
<p>The jsapi file is not cached, so a additional 5K is downloaded for every page hit on your website; if a visitor loads more than three pages an hour then the advantage of the longer caching time is lost. As always, it&#8217;s a matter of compromise.</p>
<p>* A minor update to jQuery has been released since the original article was written.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/caching-on-the-google-ajax-libraries-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
