<?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; Peter Wilson</title>
	<atom:link href="http://bigredtin.com/author/peter/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>Wed, 28 Jul 2010 05:03:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<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 check_for_print($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>
]]></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>Thesis V WordPress, Pearson V Mullenweg</title>
		<link>http://bigredtin.com/behind-the-websites/thesis-v-wordpress/</link>
		<comments>http://bigredtin.com/behind-the-websites/thesis-v-wordpress/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 01:33:38 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[Chris Pearson]]></category>
		<category><![CDATA[GPL]]></category>
		<category><![CDATA[Matt Mullenweg]]></category>
		<category><![CDATA[Movable Type]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[Thesis]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=669</guid>
		<description><![CDATA[Mullenweg believes that, because WordPress is released under the GPLv2 license, all themes and plugins developed for WordPress must also be released under the same license. Pearson disagrees. I believe that Mullenweg is wrong. WordPress themes can operate on other blogging platforms with minimal changes.]]></description>
			<content:encoded><![CDATA[<p>Reading my WordPress feeds this-morning, it appears a <a title="war of words" href="http://thenextweb.com/socialmedia/2010/07/14/wordpress-and-thesis-go-to-battle-mullenweg-may-sue/">war of words</a> broke out overnight between Matt Mullenweg (the lead developer of WordPress) and Chris Pearson, the developer of the Thesis theme.</p>
<p>In brief, Mullenweg believes that, because WordPress is released under the <a title="GPLv2 license" href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt">GPLv2 license</a>, all themes and plugins developed for WordPress must also be released under the same license. Pearson disagrees.</p>
<p>This situation has never affected us directly at Soupgiant so we haven&#8217;t needed to, and this is important, ask our lawyer if my interpretation is correct. <strong>This is a layman&#8217;s opinion and should be treated as such</strong>.</p>
<p>The battle comes down to these clauses in the GPLv2 license:</p>
<blockquote><p>You must cause any work that you distribute or publish, that in    whole or in part contains or is derived from the Program or any    part thereof, to be licensed as a whole at no charge to all third    parties under the terms of this License.</p>
<p>If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works.</p>
<p>&#8211;source <a title="GPLv2 license" href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt">GPLv2 license</a></p></blockquote>
<p>Due to the second clause quoted above, I believe that Mullenweg is wrong. WordPress themes can operate on other blogging platforms with minimal changes. This has been done before with the <a title="Sandbox theme for WordPress" href="http://www.plaintxt.org/themes/sandbox/">Sandbox theme for WordPress</a> which was successfully ported to <a title="Movable Type" href="http://plugins.movabletype.org/sandbox/">Movable Type</a>.</p>
<p>WordPress themes output <abbr>HTML</abbr> with a series of calls to the blogging platform. To output the post&#8217;s title and contents in our base theme, we use the code:</p>
<pre><code>&lt;h2 class="entry-title"&gt;&lt;?php the_title() ?&gt;&lt;/h2&gt;
&lt;div class="entry-content"&gt;
    &lt;?php the_content("Continue reading " . the_title('', '', false)); ?&gt;
&lt;/div&gt;</code></pre>
<p>To output the same <abbr>HTML</abbr> in a Movable Type theme, we would output:</p>
<pre><code>&lt;h2 class="entry-title"&gt;&lt;$mt:EntryTitle$&gt;&lt;/h2&gt;
&lt;div class="entry-content"&gt;
    &lt;$mt:EntryBody$&gt; &lt;$mt:EntryMore$&gt;
&lt;/div&gt;</code></pre>
<p>In terms of a page&#8217;s output, the above code is a minor part of the page. <em>The theme&#8217;s template is mostly made up of HTML and CSS, HTML and CSS operate in the browser and not in the blogging platform</em>. It&#8217;s for that reason that I believe that Pearson is correct in this case.</p>
<p>I acknowledge that WordPress hooks may complicate the matter but these hooks output such a minor part of a theme&#8217;s HTML, that I consider the theme <em>uses</em> the platform but isn&#8217;t <em>derived</em> from the platform. I&#8217;ve left plugins out of this discussion as these are a more complicated matter: they can output HTML or they can build on the platform.</p>
<p>The above said, were I to release a WordPress theme I would probably release it under the GPL as a hat tip and thank you to the community that has assisted me so much. However, if the theme was as complicated as the Thesis theme, I may feel differently about the matter when it&#8217;s crunch time.</p>
<p>Again, this is a layman&#8217;s opinion and should be treated as such. If you have a layman&#8217;s opinion too, <a title="we'd love to hear it in the comments" href="http://bigredtin.com/behind-the-websites/thesis-v-wordpress/#comments">we&#8217;d love to hear it in the comments</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/thesis-v-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting the bloginfo correctly</title>
		<link>http://bigredtin.com/behind-the-websites/getting-the-bloginfo-correctly/</link>
		<comments>http://bigredtin.com/behind-the-websites/getting-the-bloginfo-correctly/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 02:12:30 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[bloginfo]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[domains]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress MS]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=646</guid>
		<description><![CDATA[One of the standout problems when using plugins with WordPress MS is when they define a constant for the plugin’s url as the script starts executing.]]></description>
			<content:encoded><![CDATA[<p>This site runs on an <a title="WordPress MS" href="http://codex.wordpress.org/Create_A_Network">WordPress </a><abbr title="Multi Site"><a title="WordPress MS" href="http://codex.wordpress.org/Create_A_Network">MS</a></abbr> install, along with a number of other small sites. You can see the full list of sites <a title="on the directory page" href="http://pressgiant.net/">on the directory page</a>.</p>
<p>As with most WordPress sites we use plugins to enhance WordPress, including <a title="Donncha O Caoimh" href="http://ocaoimh.ie/">Donncha O Caoimh</a>&#8216;s excellent <a title="WordPress MU Domain Mapping" href="http://ocaoimh.ie/wordpress-mu-domain-mapping/">WordPress <abbr>MU</abbr> Domain Mapping</a> plugin. As the name implies, the domain mapping plugin allows us to use top level domains for each site rather than being stuck with sub-domains. In the case of this blog, without the plugin it would reside at <a title="bigredtin.pressgiant.com" href="http://bigredtin.com">bigredtin.pressgiant.net</a>.</p>
<h4>Taking care with plugins</h4>
<p>Many plugins are tested for the single site version of WordPress only. I don&#8217;t have a problem with this as most plugins are released under the GPL and free in terms of both speech and beer. If I&#8217;m not paying for software, it&#8217;s up to me to test it in the fringe environment of WordPress <abbr title="Multi Site">MS</abbr>.</p>
<p>Now that WordPress is WordPress <abbr title="Multi Site">MS</abbr> is WordPress, more developers may test in both environments but they certainly can&#8217;t be expected to test with all manner of combinations of plugins.</p>
<h4>The standout problem</h4>
<p>One of the standout problems when using plugins with WordPress <abbr title="Multi Site">MS</abbr> is when they define a constant for the plugin&#8217;s url as the script starts executing, the PHP code may look similar to:</p>
<pre><code>&lt;?php

  define('PLUGIN_DIR', get_bloginfo('url') . "/wp-content/plugins/peters-plugin");

  function plugin_js_css(){
    wp_enqueue_script('plugin-js', PLUGIN_DIR . '/script.js');
    wp_enqueue_style('plugin-css', PLUGIN_DIR . '/style.css');
  }

  add_action('init', 'plugin_js_css');

?&gt;</code></pre>
<p>The above stands equally for themes mapping the stylesheet directory at the start of execution:</p>
<pre><code>&lt;?php

  define('THEME_DIR', get_bloginfo('stylesheet_directory') );

  function theme_js_css(){
    wp_enqueue_script('theme-js', THEME_DIR . '/script.js');
    wp_enqueue_style('theme-css', THEME_DIR . '/style.css');
  }

  add_action('init', 'theme_js_css');

?&gt;</code></pre>
<p>The <a title="get_bloginfo" href="http://codex.wordpress.org/Function_Reference/get_bloginfo"><code>get_bloginfo</code></a> and <a title="bloginfo" href="http://codex.wordpress.org/Function_Reference/bloginfo"><code>bloginfo</code></a> functions return information about your blog and your theme settings including the site&#8217;s home page, the theme&#8217;s directory (as in the second code sample above) or the stylesheet url. <code>bloginfo</code> outputs the requested information to your <abbr>HTML</abbr>, <code>get_bloginfo</code> returns it for use in your <abbr>PHP</abbr>. </p>
<p>Outside of code samples, <code>bloginfo</code> and <code>get_bloginfo</code> are interchangeable throughout this article.</p>
<p>The problems occur when a subsequently loaded plugin needs to change something retrieved from <code>bloginfo</code>. In this site&#8217;s case, Domain Mapping changes all URLs obtained through <code>bloginfo</code>, but it could be a plugin that simply changes the stylesheet url to a subdomain to speed up page load.</p>
<p>In a recent case, a plugin – let&#8217;s call it <a title="Disqus" href="http://disqus.com/">Disqus</a> – was defining a constant in this manner. As result an <abbr title="Cross-site scripting">XSS</abbr> error was occurring when attempting to use Facebook Connect. Replacing the constant with a <code>bloginfo</code> call fixed the problem.</p>
<p>The improved code for the first sample above is:</p>
<pre><code>&lt;?php

  function plugin_js_css(){
    wp_enqueue_script('plugin-js', get_bloginfo('url') . '/wp-content/plugins/peters-plugin/script.js');
    wp_enqueue_style('plugin-css', Pget_bloginfo('url') . '/wp-content/plugins/peters-plugin/style.css');
  }

  add_action('init', 'plugin_js_css');

?&gt;</code></pre>
<h4>bloginfo doesn&#8217;t hit the database everytime</h4>
<p>I presume the developers set their own constants because they&#8217;d like to avoid hitting the database repeatedly to receive the same information.</p>
<p>Having run some tests on my local install of WordPress, I can assure you this is not the case. Running <code>bloginfo('stylesheet_directory')</code> triggers a db call on the first occurrence, the information is then cached for subsequent calls.</p>
<p>I realise I sound incredibly fussy and that I&#8217;m suggesting we protect against edge cases on our edge cases. You&#8217;re right, and <a title="it's not the first time" href="http://bigredtin.com/behind-the-websites/javascript-the-wordpress-way-part-1/">it&#8217;s not the first time</a>, but as developers it&#8217;s the edge cases that we&#8217;re employed to avoid.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/getting-the-bloginfo-correctly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8216;Skip to Content&#8217; Links</title>
		<link>http://bigredtin.com/behind-the-websites/skip-to-content-links/</link>
		<comments>http://bigredtin.com/behind-the-websites/skip-to-content-links/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 02:03:45 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[Best Practice]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[JAWS]]></category>
		<category><![CDATA[screen readers]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=635</guid>
		<description><![CDATA[Josh and I were discussing the positioning of Skip to Content links on a website. In the past I've placed these in the first menu on the page, usually positioned under the header.]]></description>
			<content:encoded><![CDATA[<p>Big Red Tin co-author, Josh, and I were discussing the positioning of <em>Skip to Content</em> links on a website. In the past I&#8217;ve placed these in the first menu on the page, usually positioned under the header.</p>
<p>According to the <a title="fangs plugin for firefox" href="http://www.standards-schmandards.com/projects/fangs/">fangs plugin</a>, the <a title="JAWS" href="http://www.freedomscientific.com/products/fs/jaws-product-page.asp">JAWS</a> screen reader reads the opening of <a title="Soupgiant.com" href="http://soupgiant.com/">Soupgiant.com</a> as:</p>
<blockquote><p>Page has seven  headings and forty-three  links Soupgiant   vertical bar  Web Production  dash  Internet Explorer Heading level one Link Graphic Soupgiant   vertical bar  Web Production Heading level five Heat and Serve Combine  seventeen  years of web production experience, twenty  years of  television  and  radio experience,  put it all in a very large pot on a  gentle heat.  Stir regularly  and  serve. Soupgiant goes well with croutons  and  a  touch of parsley.List of five  items bullet <strong>This page link  Skip to Content</strong> bullet Link Home bullet Link About bullet Link Contact bullet Link Folio</p>
<p>- my emphasis</p></blockquote>
<p>That&#8217;s a lot of content to get through, on every page of the site, before the Skip to Content link. It would be much better if the skip to content link were earlier on the site.</p>
<p>As the <abbr>HTML</abbr> title of the page is read out by JAWS, the best position would be before the in-page title. The opening content would then read as:</p>
<blockquote><p>Page has seven  headings and forty-three  links Soupgiant   vertical bar  Web Production  dash  Internet Explorer <strong>This page link Skip to Content</strong> Heading level one Link Graphic Soupgiant   vertical bar  Web Production</p>
<p>- again, the emphasis is mine</p></blockquote>
<p>That gives the JAWS user the title of the page and immediately allows them to skip to the page&#8217;s content. I don&#8217;t read the header of on every page of a site, nor should I expect screen reader users to.</p>
<p>I realise screen readers most likely have a feature to skip around the page relatively easily, regardless of how the page is set up but our aim should not be <em>relative</em> ease, our aim should be <em>absolute</em> ease.</p>
<p>As a result, we&#8217;ve decided to move the skip to content links on future sites to earlier in the page.</p>
<p>Sadly, this revelation came up as a result of what I consider to be a limitation of the WordPress 3.0+ function <code><a title="wp_nav_menu" href="http://codex.wordpress.org/Function_Reference/wp_nav_menu">wp_nav_menu</a></code>, the inability to add items at the start of the menu. I should have considered the accessibility implications much earlier. It serves as a reminder, to all web developers, that we should constantly review our practices and past decisions.</p>
<p>If you&#8217;ve recently changed something you&#8217;ve always done, <a title="we'd love to hear about it in the comments" href="http://bigredtin.com/behind-the-websites/skip-to-content-links/#comments">we&#8217;d love to hear about it in the comments</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/skip-to-content-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog Post: This Tweet Looks Unloved</title>
		<link>http://bigredtin.com/content-strategy/unloved-tweets/</link>
		<comments>http://bigredtin.com/content-strategy/unloved-tweets/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 00:30:03 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Content Strategy]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[blogs]]></category>
		<category><![CDATA[CoTweet]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Twitterfeed]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=588</guid>
		<description><![CDATA[We had Twitterfeed set up at this blog's old location and took the opportunity to compare click-throughs from manual tweets versus automated tweets. Manual tweets had a substantially higher click-through rate than the automated tweets.]]></description>
			<content:encoded><![CDATA[<p>Any blogger worth their salt knows of <a href="http://twitterfeed.com/">Twitterfeed</a> or a similar service. For the uninitiated, Twitterfeed converts a site&#8217;s RSS feed into tweets, allowing users to set and forget. The auto-tweets take the form &#8216;Blog Post: &lt;title&gt; &lt;short url&gt;&#8217; or similar.</p>
<p>When we launched Big Red Tin we didn&#8217;t set up Twitterfeed immediately.</p>
<p>With manual tweets we could customise the message to provide more details to Twitter users, one such tweet was:</p>
<blockquote><p>We&#8217;ve defined a new term: Web 1.5 <a href="http://bigredtin.com/design/web-1-5/">http://redt.in/b0KRut</a> ^pw</p>
<p>– <a href="http://twitter.com/bigredtin/status/16282703026">source: @bigredtin</a></p></blockquote>
<p>We had Twitterfeed set up at this blog&#8217;s old location and took the opportunity to compare click-throughs from manual tweets versus automated tweets.</p>
<p>Manual tweets had a substantially higher click-through rate than the automated tweets. I suspect the reason for this is two fold:</p>
<ul>
<li>With so many people using Twitterfeed type 	services, Twitter users have learnt to ignore tweets that appear 	auto-generated.</li>
<li>More information can be included in a manual tweet than might appear in an auto-tweet.</li>
</ul>
<p>Take the post we were linking to earlier, had we been using Twitterfeed the tweet would have been &#8216;Blog Post: Web 1.5 <a href="http://bigredtin.com/design/web-1-5/">http://redt.in/b0KRut</a>&#8216;. This provides so little information as to be next to useless. We would have ignored such a tweet out ourselves.</p>
<p>Many of the posts on this site are scheduled in advance, this allows us to publish at roughly the same time each week.</p>
<p>To schedule the associated tweets we use <a href="http://cotweet.com/">CoTweet</a>. We have a couple of shared twitter accounts as it is, so CoTweet comes in handy for other purposes, but it&#8217;s the scheduling feature we use most of all.</p>
<p>If you use Twitterfeed yourself, try disabling it for a couple of weeks and manually tweet in its place. There&#8217;s a good chance you&#8217;ll be pleasantly surprised when you compare your <a href="http://bit.ly/">bit.ly</a> stats.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/content-strategy/unloved-tweets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Surprise. It&#8217;s all about honesty</title>
		<link>http://bigredtin.com/business/honesty/</link>
		<comments>http://bigredtin.com/business/honesty/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 02:21:10 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[clients]]></category>
		<category><![CDATA[meetings]]></category>
		<category><![CDATA[sales]]></category>
		<category><![CDATA[selling]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=574</guid>
		<description><![CDATA[We were unable to help a potential client with the task they had in mind. We may have been able to fudge it but we don't think 'fudging it' is the way to keep clients happy.]]></description>
			<content:encoded><![CDATA[<p>Last week we had a sales meeting with a potential client. As it turned out, we were unable to help with the task they had in mind. It was outside our area of expertise.</p>
<p>We may have been able to fudge it. Call us stupid, but we don&#8217;t think &#8216;fudging it&#8217; is the way to keep clients happy or maintain a low client turnover.</p>
<p>In this situation there are two options:</p>
<ol>
<li>Quote ludicrously high with the aim of missing out on the job. In the event the quote is accepted, the job can be outsourced with a tidy profit.</li>
<li>Tell the truth and decline the work</li>
</ol>
<p>We chose the latter option and used the opportunity to explain our areas of expertise. Selling the company, not the lie.</p>
<p>The natural fear is the potential client will storm out of the meeting, muttering obscenities under their breath.</p>
<p>What actually happens is the potential client realises their current project – or at least the original part of their current project – is a bad fit. They also realise they&#8217;re not dealing with sleazy salesmen willing to say anything to get a job and deal with the consequences later.</p>
<p>The second realisation sells a company. It&#8217;s something that can be used to convert a single project into a long term relationship.</p>
<p>Ludicrously high quoting, lies or fudging a task may get you more clients but getting clients isn&#8217;t the aim, the real aim is to keep them.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/business/honesty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web 1.5</title>
		<link>http://bigredtin.com/design/web-1-5/</link>
		<comments>http://bigredtin.com/design/web-1-5/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 04:40:35 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Content Strategy]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=549</guid>
		<description><![CDATA[The brief for Big Red Tin and its redesigned sister site, <a title="Soupgiant" href="http://soupgiant.com">Soupgiant</a>, included the note "We haven't got an exact style in mind, but something relaxed and modern without going over the top - <em>Web 1.5 if you like</em>"]]></description>
			<content:encoded><![CDATA[<p>The brief for Big Red Tin and its redesigned sister site, <a title="Soupgiant" href="http://soupgiant.com">Soupgiant</a>, included a couple of notes for our very patient designer, <a title="Christa at Zepol" href="http://zepol.com.au">Christa at Zepol</a>:</p>
<blockquote><p>The frame of both sites will be pretty similar. We were thinking of different colour schemes [...] as a way of demarcating the two sites.</p>
<p>We haven&#8217;t got an exact style in mind, but something relaxed and modern without going over the top &#8211; <em>Web 1.5 if you like</em>.</p>
<p>— source: email to Zepol (emphasis added for this post)</p></blockquote>
<p>The rest of the brief detailed the <a title="content separation" href="http://bigredtin.com/content-strategy/the-right-content-for-the-right-audience/">content separation</a> between Soupgiant and Big Red Tin. We wanted the design to come from the designer to fit the content, not from a committee to fit some other agenda.</p>
<p>In the context of this site&#8217;s design, the exact interpretation of Web 1.5 was left to Christa&#8217;s devices. As should the exact interpretation of any design brief.</p>
<p>To me, web 1.5 means something like: <strong>dump all the cliches of Web 2.0 design, at the same time keep the good bits</strong></p>
<p>&#8220;A good bit&#8221; may be a feature that is used frequently, such as oversized footers, because it adds something for users of the site. Cliches are those design tricks that add nothing but appear on every second web site, such as elements with faded backgrounds and rounded corners.</p>
<p>It strikes me, as a developer, it must be said that the definition of design is something similar: <strong>dump all the cliches and keep all the good bits</strong></p>
<p>Failing to do so risks rendering you a regurgitator, not a designer.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/design/web-1-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Valid Isn&#8217;t Best Practice</title>
		<link>http://bigredtin.com/behind-the-websites/valid-isnt-best-practice/</link>
		<comments>http://bigredtin.com/behind-the-websites/valid-isnt-best-practice/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 00:30:09 +0000</pubDate>
		<dc:creator>Peter Wilson</dc:creator>
				<category><![CDATA[Behind the Websites]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[input types]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://bigredtin.pressgiant.net/?p=512</guid>
		<description><![CDATA[Not long ago, on the <a title="@soupgiant" href="http://twitter.com/soupgiant">@soupgiant</a> account, I tweeted "Vaild html / css doesn't indicate your code is best practice; it may even indicate the opposite. #css3". Neither the xHTML nor the CSS on this site validates, we consider it to observe best practices.]]></description>
			<content:encoded><![CDATA[<p>Not long ago, on the <a title="@soupgiant" href="http://twitter.com/soupgiant">@soupgiant</a> account, I tweeted:</p>
<p><a href="http://twitter.com/soupgiant/status/13047478686"><img class="aligncenter size-full wp-image-513" title="Valid HTML / CSS - Tweet" src="http://bigredtin.com/files/2010/06/valid-tweet.png" alt="'Vaild html / css doesn't indicate your code is best practice; it may even indicate the opposite. #css3.'" width="610" height="197" /></a></p>
<p>While neither the xHTML nor the CSS on this site validates, we consider it to observe best practices.<span id="more-512"></span>The <a title="HTML fails validation" href="http://validator.w3.org/check?verbose=1&amp;uri=http%3A%2F%2Fbigredtin.com%2Fbehind-the-websites%2Fvalid-isnt-best-practice%2F">HTML fails validation</a> due to the following problems:</p>
<ul>
<li>One of the WordPress plugins we use on the site inserts JavaScript with unencoded ampersands in the URL. This isn’t a best practice but unfortunately it’s beyond our control;</li>
<li>The <code>&lt;html&gt;</code> tag has a class attribute;</li>
<li>Two of the fields in our comment form have an unrecognised value for the <code>type</code> attribute.</li>
</ul>
<p>The <a title="CSS fails validation" href="http://jigsaw.w3.org/css-validator/validator?profile=css21&amp;warning=0&amp;uri=http%3A%2F%2Fbigredtin.com%2Fbehind-the-websites%2Fvalid-isnt-best-practice%2F">CSS fails validation</a> due to the following problems:</p>
<ul>
<li>We load the YUI 2.7.0 CSS reset, base and fonts files from the Google AJAX API servers. These files contain star hacks for IE targeting;</li>
<li>We use browser extensions to implement CSS rounded corners and rgba backgrounds;</li>
<li>Until I started writing this post, we styled a heading with <code>font-size: 145.4545<strong>%%</strong></code>.</li>
</ul>
<p>That last one was a typo, and has since been fixed.</p>
<h4>Justifying the invalid CSS</h4>
<p>We’d prefer to avoid <a href="http://www.dynamicsitesolutions.com/css/filters/star-html/" title="* html ("Star HTML") CSS Hack">the star hacks</a> in YUI’s CSS. If we self-hosted the files they’d be moved into the CSS files with our other IE specific hacks and targeted with conditional comments. Our compromise is to load the files from Google’s server with the aim that our site will load faster. Either the user will already have the files in their browser cache, or the user will load the files from a closer, faster server on Google’s global network.</p>
<p>The W3C’s CSS validator also screams at us for using CSS3 browser extensions and rgba backgrounds. While the browser manufacturers are still trying out their implementations of the new specs, we’ve no choice but to use these for the time being. As they iron out their implementations and move them over to the specified properties we can remove these. CSS3 junkies could probably have skipped this paragraph.</p>
<h4>Justifying the invalid HTML</h4>
<p>This site uses xHTML. We’ve discussed using HTML5 but WordPress, which powers Big Red Tin, isn’t ready to drop the <code>/&gt;</code> from self closing tags. At this stage the HTML5 spec is too dynamic for our liking. Have a late afternoon snooze and the spec is likely to change during your siesta.*</p>
<p>Depending on your point of view, and even what you are coding, it is either fortunate or unfortunate that browsers don’t always follow specs to the letter. We’ve taken advantage of that to throw one of the biggest advantages of HTML5 into our xHTML. In our comments form, we’ve used HTML5 type attributes for two of our fields: <code>&lt;input type=”email” … /&gt;</code> and <code>&lt;input type=”url” … /&gt;</code>.</p>
<p>In browsers that do not support these types of inputs the type falls back to the standard of <code>type=”text”</code>. In browsers that do support input types of email and url, it prevents the user from entering an erroneous value. For users of the iPhone or iPad, a special keyboard is presented to make entering an email address or URL easier. In browsers that support the new HTML5 input types, the behaviour is identical regardless of the doc type.</p>
<div id="attachment_514" class="wp-caption aligncenter" style="width: 490px"><img class="size-full wp-image-514" title="iPhone keyboard for email input type" src="http://bigredtin.com/files/2010/06/valid-iphone2.png" alt="" width="480" height="320" /><p class="wp-caption-text">The iPhone displays different keyboards for HTML5 input types</p></div>
<p>It may be breaking some of the rules of standards. To me it&#8217;s not so much about standards as best practices and giving the user the best possible experience on a site, even if this means breaking the rules sometimes.</p>
<p>Don&#8217;t take that to mean that we&#8217;ll be reverting to table layouts, or insert code such as <code>&lt;a href="#"&gt;&lt;h1&gt;Heading&lt;/h1&gt;&lt;/a&gt;</code> into our xHTML. It&#8217;s bending the rules around the edges, which is no different to using vendor prefixes to make use of CSS3.</p>
<p>*Yes. I know this is an unfair characterisation. A lot of very important people, with very important lawyers, are working hard to change this.</p>
]]></content:encoded>
			<wfw:commentRss>http://bigredtin.com/behind-the-websites/valid-isnt-best-practice/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>
	</channel>
</rss>
