<?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>Digging into WordPress &#187; JavaScript</title>
	<atom:link href="http://digwp.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>WordPress JSON API Plugin</title>
		<link>http://digwp.com/2010/05/wordpress-json-api-plugin/</link>
		<comments>http://digwp.com/2010/05/wordpress-json-api-plugin/#comments</comments>
		<pubDate>Sat, 29 May 2010 19:34:51 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2153</guid>
		<description><![CDATA[WordPress already kind of has an XML API. Basically, RSS feeds. WordPress creates feeds for all kinds of stuff: recent posts, comment threads on any Page or Post that has comments, category-specific, tag-specific, and more. The codex covers all this and we&#8217;ve also covered creating your own unique feeds that could literally be from any [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress already kind of has an XML API. Basically, RSS feeds. WordPress creates feeds for all kinds of stuff: recent posts, comment threads on any Page or Post that has comments, category-specific, tag-specific, and more. The codex <a href="http://codex.wordpress.org/WordPress_Feeds">covers all this</a> and we&#8217;ve also covered <a href="http://digwp.com/2009/09/easy-custom-feeds-in-wordpress/">creating your own unique feeds</a> that could literally be from any data in your WordPress database.</p>
<p>So RSS is pretty sweet. There are lots of feed parsers out there that can do cool stuff with feeds. XML is pretty cool. PHP5 has functions to make decently quick work of parsing XML. </p>
<p><strong>But what if you are working with JavaScript?</strong> XML + JavaScript kinda sucks. The <a href="http://www.json.org/">JSON</a> format is approximately one million times easier to work with. Fortunately there is <a href="http://wordpress.org/extend/plugins/json-api/">a kick-ass plugin</a> for us!</p>
<p><span id="more-2153"></span></p>
<h3>The Plugin&#8230;</h3>
<p>is by <a href="http://phiffer.org/">Dan Phiffer</a> who created it for for working with the MoMA (Museum of Modern Art) <a href="http://moma.org/explore/inside_out">blog</a>. Apparently it&#8217;s some Frankenstein&#8217;s monster of Ruby on Rails and WordPress. </p>
<p><a href="http://wordpress.org/extend/plugins/json-api/">WordPress JSON API Plugin</a></p>
<p>All you need to do is install and activate it, and you&#8217;ll have access to a new URL structure that servers up JSON data.</p>
<p>At its most simple:</p>
<pre><code>http://example.com/?json=1</code></pre>
<p>Which will return delicious, delicious JSON:</p>
<pre><code>{
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [
    {
      "id": 1,
      "slug": "hello-world",
      "url": "http:\/\/localhost\/wordpress\/?p=1",
      "title": "Hello world!",
      "title_plain": "Hello world!",
      "content": "&lt;p&gt;Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!&lt;\/p&gt;\n",
      "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
      "date": "2009-11-11 12:50:19",
      "modified": "2009-11-11 12:50:19",
      "categories": [],
      "tags": [],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      },
      "comments": [
        {
          "id": 1,
          "name": "Mr WordPress",
          "url": "http:\/\/wordpress.org\/",
          "date": "2009-11-11 12:50:19",
          "content": "&lt;p&gt;Hi, this is a comment.&lt;br \/&gt;To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.&lt;\/p&gt;\n",
          "parent": 0
        }
      ],
      "comment_count": 1,
      "comment_status": "open"
    }
  ]
}</code></pre>
<p>The plugin page has comprehensive notes on <a href="http://wordpress.org/extend/plugins/json-api/other_notes/">how you can structure the URLs</a> to get just what you need back. </p>
<p>Of course, the more that you want, the larger the size of the data is, the harder it has to work, and the slower it can be. The plugin allows you to trim down what it returns to speed things up if needed. This is how to get just 10 posts, with a specific custom field, and only a few other things:</p>
<pre><code>http://example.com/?json=1&amp;count=10&amp;custom_fields=PostThumb&amp;include=title,custom_fields,excerpt</code></pre>
<h3>What might you use this for?</h3>
<p><a href="http://jqueryui.com/">jQuery UI</a> has recently released a new feature: <a href="http://docs.jquery.com/UI/Autocomplete">Autocomplete</a>. I learned that it basically consumes JSON data to populate what it autocompletes with. So I decided to try and connect the two and make an autocomplete search box for this site. </p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/blog-images/autocompletesearch.jpg" width="581" height="331" alt="" title="" />
</div>
<p>Try typing in like &#8220;CSS&#8221; or something and you can see some articles autocomplete below the input. You can select one and press return to go to that post. It&#8217;s kinda cheezy, probably not something I would roll out live, but it was a fun demo to work on. If you want to see the JavaScript that powers it, <a href="http://digwp.com/wp-content/themes/DiggingIntoWordPress-2/js/autocomplete.js">check it out</a>. </p>
<p>To be fair, I had to hack the plugin just slightly to get it to work. The autocomplete plugin wants a &#8220;value&#8221; and &#8220;label&#8221; data in the JSON, so I added them. It&#8217;s a pretty trivial adjustment so if anyone wants that code, let me know.</p>
<p><strong>UPDATE:</strong> Demo temporarily removed for security.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/05/wordpress-json-api-plugin/">Permalink</a> | <a href="http://digwp.com/2010/05/wordpress-json-api-plugin/#comments">13 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/05/wordpress-json-api-plugin/&title=WordPress JSON API Plugin">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/api/" rel="tag">API</a>, <a href="http://digwp.com/tag/javascript/" rel="tag">JavaScript</a>, <a href="http://digwp.com/tag/json/" rel="tag">JSON</a>, <a href="http://digwp.com/tag/plugin/" rel="tag">plugin</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/05/wordpress-json-api-plugin/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Making an Expanding Code Box</title>
		<link>http://digwp.com/2009/07/making-an-expanding-code-box/</link>
		<comments>http://digwp.com/2009/07/making-an-expanding-code-box/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 15:49:56 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://diggingintowordpress.com/?p=142</guid>
		<description><![CDATA[On blogs that like to share snippets of code like this one, it is common to use the &#60;pre&#62; tag to wrap the code so that the spacing/indenting is maintained and long lines do not wrap. While this is desirable behavior, it can be undesirable to have those un-wrapped lines break out of their containers [...]]]></description>
			<content:encoded><![CDATA[<p>On blogs that like to share snippets of code like this one, it is common to use the &lt;pre&gt; tag to wrap the code so that the spacing/indenting is maintained and long lines do not wrap. While this is desirable behavior, it can be undesirable to have those un-wrapped lines break out of their containers awkwardly and overlap other content.</p>
<p>On Digging Into WordPress, we use JavaScript (jQuery) to solve this problem. One solution could potentially be to use only CSS and expand the width with something like <tt>pre:hover</tt>, but JavaScript is more elegant:</p>
<ul>
<li>Expands only when needed</li>
<li>Expands only to as wide as needed</li>
<li>Expands with animation</li>
</ul>
<p><img src="http://digwp.com/wp-content/blog-images/boxgrow.png" width="590" height="200" alt="" title="" /></p>
<p><span id="more-142"></span></p>
<h3>The CSS</h3>
<p>The &lt;pre&gt; tag is naturally block-level, so this is what we&#8217;ll use to style our code blocks and hide the overflow.</p>
<pre><code>pre { 
  background: #eee; 
  padding: 10px; 
  border: 2px solid #c94a29; 
  overflow: hidden;
  margin: 0 0 15px 0; 
  width: 563px; 
  font-family: Courier, Monospace;
}</code></pre>
<h3>The jQuery</h3>
<p>The real trick here is that the code blocks are wrapped not only in &lt;pre&gt; tags but also in &lt;code&gt; tags. The code may ultimately get cut off by the pre block, but that inner code tag still has a natural width that can be measured by JavaScript. Here is the logic:</p>
<ol>
<li>When a pre block is hovered&#8230;</li>
<li>Measure the internal code blocks width</li>
<li>If that width is wider than the pre block&#8230;</li>
<li>Expand the pre block to be wide enough to accommodate</li>
<li>On mouseOut, return the pre block to its original size</li>
</ol>
<pre><code>$j("pre").hover(function() {
	var codeInnerWidth = $j("code", this).width() + 10;
    if (codeInnerWidth &gt; 563) {
		$j(this)
			.stop(true, false)
			.css({
				zIndex: "100",
				position: "relative"
			})
			.animate({
				width: codeInnerWidth + "px"
			});
		}
	}, function() {
			$j(this).stop(true, false).animate({
				width: 563
		});
	});</code></pre>
<p>Couple of things of note here. The &#8220;codeInnerWidth&#8221; is calculated on every hover, because different blocks will have different inner widths. The width of the pre blocks are hard-coded though. Calculating that on every hover leads to problems like fast mouse-in-mouse-outs expanding the box and re-calculating it&#8217;s size to be larger than it was originally. The <code>stop</code> function prevents queuing up of animations. Also note the $j being used is just because we are using jQuery in <code>noConflict</code> mode:</p>
<pre><code>var $j = jQuery.noConflict(); </code></pre>
<h3>Demo</h3>
<p>None of the code blocks so far have been long enough for you to see it in action, so here is a demo block (RSS readers, obviously you need to see this on our site to see it work).</p>
<pre><code>This is a really long line of code that is even longer than the world super-cala-frag-alistic-espee-ala-dosis</code></pre>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/07/making-an-expanding-code-box/">Permalink</a> | <a href="http://digwp.com/2009/07/making-an-expanding-code-box/#comments">25 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/07/making-an-expanding-code-box/&title=Making an Expanding Code Box">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/javascript/" rel="tag">JavaScript</a>, <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/07/making-an-expanding-code-box/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Create a Stunning Lightbox-Style Random-Post Header Gallery</title>
		<link>http://digwp.com/2009/06/random-lightbox-header-gallery/</link>
		<comments>http://digwp.com/2009/06/random-lightbox-header-gallery/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 14:09:43 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[custom-fields]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[lightbox]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://diggingintowordpress.com/?p=150</guid>
		<description><![CDATA[In this tutorial, we&#8217;re going to take advantage of two of WordPress&#8217; most powerful features, get_posts() and custom fields, to create a stunning random lightbox-style header gallery for your post images. Displayed before the standard post loop, this lightbox gallery will randomly display the images that are associated with your posts while also providing a [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we&rsquo;re going to take advantage of two of WordPress&rsquo; most powerful features, <code>get_posts()</code> and custom fields, to create a stunning random lightbox-style header gallery for your post images. Displayed <em>before</em> the standard post loop, this lightbox gallery will randomly display the images that are associated with your posts while also providing a descriptive title link to the post itself. Here is a graphical representation that will help us visualize what we are going to do:</p>
<p><img src="http://digwp.com/wp-content/blog-images/lightbox-tutorial.jpg" alt="" title="" /><br /><small>Random post-display functionality with lightbox-style popup image and title post link</small></p>
<p>The key to setting this up involves creating a set of custom fields for each post and then displaying the information randomly before the main post loop. Let&rsquo;s begin with the custom fields.</p>
<p><span id="more-150"></span></p>
<h3>Setting up the custom fields</h3>
<p>If you have already been taking advantage of <a href="http://perishablepress.com/press/2008/12/17/wordpress-custom-fields-tutorial/" title="WordPress Custom Fields, Part I: The Basics">WordPress custom fields</a>, then you already understand the basic idea here. For each post that you would like to have appear randomly in the header of your theme, create the following custom fields:</p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-fields.gif" alt="" title="" /><br /><small>Custom-field data associated with each randomly displayed post</small></p>
<p>Notice that we are using <em>relative</em> file paths for the images. This trick ensures maximum flexibility and portability for our custom-field data. Also note the two different images we are using for each post: a full-size image that will be displayed in the lightbox popup, and a cropped or resized version of that same image for display in the random header.</p>
<p>Once you have associated these custom fields with a few of your posts, it is time for a little <code>get_posts()</code> action to display the random content in your active theme.</p>
<h3>Editing your WordPress theme files</h3>
<p>One of the great things about WordPress custom fields is that they enable you to display your content in just about any way imaginable. In this tutorial, we are going to display our custom-field data in the header area for every page on the site. Thus, we open our theme&rsquo;s <code>header.php</code> file and add the following code:</p>
<pre><code>&lt;?php $random = get_posts('showposts=1&amp;orderby=rand'); foreach($random as $post) : setup_postdata($post); ?&gt;

&lt;p&gt;
	&lt;a href="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomImage', true); ?&gt;" title="&lt;?php echo get_post_meta($post-&gt;ID, 'randomCaption', true); ?&gt;" class="thickbox"&gt;
		&lt;img src="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomBanner', true); ?&gt;" alt="" /&gt;
	&lt;/a&gt;
	&lt;small&gt;&lt;a href="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomLink', true); ?&gt;"&gt;&lt;?php echo get_post_meta($post-&gt;ID, 'randomTitle', true); ?&gt;&lt;/a&gt;&lt;/small&gt;
&lt;/p&gt;

&lt;?php endforeach; ?&gt;</code></pre>
<p>As mentioned, this code goes before the normal WordPress loop and calls data from each of the five custom fields that have been added to your posts. Instead of using the more powerful <code>query_posts()</code> for this random post display, we use the simpler <a href="http://codex.wordpress.org/Template_Tags/get_posts" title="WordPress Codex: Template Tags/get posts">get_posts()</a> function instead. It does everything we need while eliminating unnecessary variables and potential complications.</p>
<p>A few other points of interest for this WordPress snippet; notice we are setting the randomness and limiting the post display to one (&nbsp;<code>1</code>&nbsp;) via two parameters in the <code>get_posts()</code> function. Once the query has been setup, we are using the <a href="http://codex.wordpress.org/Function_Reference/get_post_meta" title="Function Reference/get post meta">get_post_meta()</a> tag to populate the following markup with their respective custom-field values:</p>
<pre><code>&lt;p&gt;
	&lt;a href="[randomImage]" title="[randomCaption]" class="thickbox"&gt;
		&lt;img src="[randomBanner]" alt="" /&gt;
	&lt;/a&gt;
	&lt;small&gt;&lt;a href="[randomLink]"&gt;[randomTitle]&lt;/a&gt;&lt;/small&gt;
&lt;p&gt;</code></pre>
<p>This demonstrates the incredible design flexibility enabled by WordPress custom fields. Once this code is in place, it is time to wrap things up by implementing the JavaScript-powered lightbox functionality.</p>
<h3>Adding the JavaScript</h3>
<p>To finish things off, let&rsquo;s make that random-banner image really &ldquo;pop&rdquo; by implementing some JavaScript-powered lightbox functionality. As you probably know, there are <a href="http://planetozh.com/projects/lightbox-clones/" title="The Lightbox Clones Matrix">many lightbox clones</a> from which to choose, but for this tutorial we turn to Cody Lindley&rsquo;s excellent <a href="http://jquery.com/demo/thickbox/" title="ThickBox 3.1">Thickbox</a> script:</p>
<blockquote><p>ThickBox is a webpage UI dialog widget written in JavaScript on top of the jQuery library. Its function is to show a single image, multiple images, inline content, iframed content, or content served through AJAX in a hybrid modal.</p></blockquote>
<p>Using Thickbox is <em>so</em> easy, it almost feels like I&rsquo;m just being lazy for using it. In fact, we have already setup our WordPress code for use with Thickbox by simply adding the <code>class="thickbox"</code> attribute to the random-banner anchor element. The only other thing that really needs to be done is to upload the Thickbox script and accompanying <acronym title="Cascading Style Sheets">CSS</acronym> file to your server and link to them from the <code>&lt;head&gt;</code> section of your <code>header.php</code> file:</p>
<pre><code>&lt;link rel="stylesheet" href="http://domain.tld/wp-content/css/thickbox.css" type="text/css" media="screen" /&gt;
&lt;script type="text/javascript" src="http://domain.tld/wp-content/javascript/thickbox-compressed.js"&gt;&lt;/script&gt;</code></pre>
<h3>Putting it all together</h3>
<p>Once everything is in place, your random-post gallery should be working great. To see a working example of this technique, check out the header area of <a href="http://eightyeightteeth.com/" title="EightyEightTeeth.com - Graphics Portfolio of Thane Champie">Thane Champie&rsquo;s Graphics Portfolio site</a>. That particular implementation of this method features a few more bells and whistles, but the basic functionality is the same.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/06/random-lightbox-header-gallery/">Permalink</a> | <a href="http://digwp.com/2009/06/random-lightbox-header-gallery/#comments">15 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/06/random-lightbox-header-gallery/&title=Create a Stunning Lightbox-Style Random-Post Header Gallery">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/custom-fields/" rel="tag">custom-fields</a>, <a href="http://digwp.com/tag/header/" rel="tag">header</a>, <a href="http://digwp.com/tag/javascript/" rel="tag">JavaScript</a>, <a href="http://digwp.com/tag/layout/" rel="tag">layout</a>, <a href="http://digwp.com/tag/lightbox/" rel="tag">lightbox</a>, <a href="http://digwp.com/tag/loop/" rel="tag">loop</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/06/random-lightbox-header-gallery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

