<?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; jquery</title>
	<atom:link href="http://digwp.com/tag/jquery/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>Using &#8216;$&#8217; instead of &#8216;jQuery&#8217; in WordPress</title>
		<link>http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/</link>
		<comments>http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 18:57:29 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[functions.php]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5240</guid>
		<description><![CDATA[Writing out 'jQuery' a billion times in a script makes it harder to read and bloats its size. Let's stop doing that and start writing '$' like you see in the vast majority of jQuery code in the world.]]></description>
			<content:encoded><![CDATA[<p>WordPress ships with jQuery (for longevity&#8217;s sake, as I write this WordPress is v3.2.1). To use jQuery in your plugins and themes <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/">&#8220;The Right Way&#8221;</a> all you need to do is <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">enqueue it</a> (probably in your functions.php file):</p>
<pre><code>wp_enqueue_script("jquery");</code></pre>
<p>The tricky thing is this particular copy of jQuery is in <a href="http://docs.jquery.com/Using_jQuery_with_Other_Libraries">compatibility mode</a> by default.<span id="more-5240"></span> That means that the typical &#8216;$&#8217; shortcut for jQuery doesn&#8217;t work, so it doesn&#8217;t conflict with any other JavaScript libraries that use the dollar sign also, like MooTools or Prototype. </p>
<p>Many plugin authors and theme developers are aware of this, and they use &#8216;jQuery&#8217; instead of &#8216;$&#8217; to be safe. </p>
<pre><code>/* Normal jQuery you see everywhere */
$("#some-element").yaddaYaddaYadda();

/* "Safe" jQuery you see in WordPress */
jQuery("#some-element").yaddaYaddaYadda();</code></pre>
<p><strong>Here&#8217;s the thing&#8230; writing out &#8216;jQuery&#8217; a billion times in a script makes it harder to read and bloats its size.</strong> Let&#8217;s stop doing that. </p>
<p>If the script is being loaded in the footer (which you should be doing in the vast majority of cases) you can wrap the code in an anonymous function (technically any <a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/">IIFE</a>) where you pass in jQuery to be mapped to $. </p>
<pre><code>(function($) {
	
	// $ Works! You can test it with next line if you like
	// console.log($);
	
})( jQuery );</code></pre>
<p>If you absolutely need to load the scripts in the header, you&#8217;ll probably need to use a document ready function anyway, so you can just pass in $ there.</p>
<pre><code>jQuery(document).ready(function( $ ) {
	
	// $ Works! You can test it with next line if you like
	// console.log($);
	
});</code></pre>
<p>Go forth and write nice looking jQuery!</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/">Permalink</a> | <a href="http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/#comments">9 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/&title=Using &#8216;$&#8217; instead of &#8216;jQuery&#8217; in WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions-php/" rel="tag">functions.php</a>, <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Randomized Grid of Posts</title>
		<link>http://digwp.com/2010/08/randomized-grid-of-posts/</link>
		<comments>http://digwp.com/2010/08/randomized-grid-of-posts/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 15:31:11 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[archives]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2673</guid>
		<description><![CDATA[I&#8217;m all about tinkering with different ideas to display posts with WordPress. After all, it&#8217;s just a bunch of data at our fingertips! WordPress makes it easy to output whatever we need. Not long ago we experimented with making a Thumbnail Based Archives. Now let&#8217;s build a Randomized Grid Archives. 1. Create a page template, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m all about tinkering with different ideas to display posts with WordPress. After all, it&#8217;s just a bunch of data at our fingertips! WordPress makes it easy to output whatever we need. Not long ago we experimented with making a <a href="http://digwp.com/2010/07/thumbnail-based-archives/">Thumbnail Based Archives</a>. Now let&#8217;s build a <a href="http://digwp.com/archives/grid/">Randomized Grid Archives</a>.</p>
<p><img src="http://digwp.com/wp-content/uploads/gridexample.jpg" alt="" title="gridexample" width="590" height="337" class="alignnone size-full wp-image-2668" /></p>
<p><span id="more-2673"></span></p>
<h3>1. Create a page template, publish a page</h3>
<p>We need a totally unique page template to work with. So create a new one. You know, just make a file like grid-archives.php in your theme and put this PHP comment at the top and then WordPress will recognize it. Then create a new page, pick this template, and publish it. The name and content absolutely don&#8217;t matter, just put something there so you can recognize/find it later.</p>
<pre><code>&lt;?php
/*
Template Name: Thumb Archives - Grid
*/
?&gt;</code></pre>
<h3>2. Loop it</h3>
<p>This is pretty much the entire page. We&#8217;ll link out to an external stylesheet for styling. Then we run a Loop for 25 posts (in our demo, we also remove our link style posts). Each post is an anchor tag, and within, the post title, the post image thumbnail, and the excerpt.</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
	&lt;meta charset="UTF-8" /&gt;
	&lt;title&gt;Grid Archives | Digging Into WordPress&lt;/title&gt;
	&lt;link rel="stylesheet" type="text/css" media="all" href="&lt;?php bloginfo("template_url"); ?&gt;/css/gridarchives.css" /&gt;
&lt;/head&gt;

&lt;body&gt;
	&lt;div id="page-wrap"&gt;
		&lt;?php query_posts('posts_per_page=25&amp;cat=-52'); ?&gt;
		&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
			&lt;a href="&lt;?php the_permalink(); ?&gt;" class="box col&lt;?php echo rand(2,4); ?&gt;"&gt;
				&lt;span class="title"&gt;&lt;?php the_title(); ?&gt;&lt;/span&gt;
				&lt;img src="&lt;?php echo get_post_meta($post-&gt;ID, 'PostThumb', true); ?&gt;" alt="" /&gt;
				&lt;span class="ex"&gt;&lt;?php the_excerpt(); ?&gt;&lt;/span&gt;
			&lt;/a&gt;
		&lt;?php endwhile; endif; ?&gt;
	&lt;/div&gt;
&lt;/body&gt;
	
&lt;/html&gt;</code></pre>
<h3>3. Masonize</h3>
<p>Notice in the HTML above we echo&#8217;d out a random number between 2 and 4 as part of a class name. The results classes will be: col-2, col-3, and col-4. These represent columns. So each link box is &#8220;randomized&#8221; based on this class name. Some post link boxes are 2 columns wide, others 3, others 4. Font sizing and thumbnail sizing also adjusts accordingly.</p>
<pre><code>.box { margin: 10px; float: left; }
.box:hover { outline: 2px solid white; }
.box:hover .title { background: none; }
.box:hover .ex { background: none; color: white; }
.box:hover img { opacity: 0.4; }

.col2 { width: 180px; }
.col3 { width: 280px; }
.col4 { width: 380px; }

.col2 img { max-width: 80px; float: right; margin: 0 0 2px 10px; }
.col3 img { max-width: 100px; float: left; margin: 0 10px 2px 0; }
.col4 img { max-width: 120px; float: right; margin: 0 0 2px 10px; }

.title { background: #237abe; color: white; display: block; padding: 10px; overflow: hidden; }
.col2 .title { font-size: 16px; }
.col3 .title { font-size: 18px; }
.col4 .title { font-size: 20px; }

.ex { background: white; color: #222; display: block; padding: 10px; }
.col2 .ex { font-size: 11px; }
.col3 .ex { font-size: 12px; }
.col4 .ex { font-size: 13px; }</code></pre>
<p>So each of those post link boxes is just floated to the left. That&#8217;s going to make a pretty ragged and nasty layout all by itself, because each box will be a different height depending on the length of the title and excerpt. No problem though, we&#8217;re going to rearrange the boxes using the <a href="http://desandro.com/resources/jquery-masonry/">jQuery Masonry plugin</a> by David DeSandro. We&#8217;ll just call jQuery, the plugin, and the write a quick script to call it. This can go pretty much anywhere on the page. Typically jQuery is run on DOM ready, but in this case we are waiting for the window&#8217;s load event, because we want to wait for the thumbnails to load before masonizing.</p>
<pre><code>&lt;script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'&gt;&lt;/script&gt;
&lt;script src='&lt;?php bloginfo("template_url"); ?&gt;/js/jquery.masonry.min.js'&gt;&lt;/script&gt;
&lt;script&gt;
 $(window).load(function() {
   $("#page-wrap").masonry({
      columnWidth: 100, 
      animate: true, 
      animationOptions: {
          duration: 300,
          queue: false
      }
  });
 });
&lt;/script&gt;</code></pre>
<h3>Enjoy!</h3>
<p><a href="http://digwp.com/archives/grid/">Here is ours.</a> Let us know if you play with this on your own site at all.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/08/randomized-grid-of-posts/">Permalink</a> | <a href="http://digwp.com/2010/08/randomized-grid-of-posts/#comments">17 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/08/randomized-grid-of-posts/&title=Randomized Grid of Posts">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/archives/" rel="tag">archives</a>, <a href="http://digwp.com/tag/grid/" rel="tag">grid</a>, <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/08/randomized-grid-of-posts/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Make an Infinite More-Posts Section</title>
		<link>http://digwp.com/2009/11/make-an-infinite-more-posts-section/</link>
		<comments>http://digwp.com/2009/11/make-an-infinite-more-posts-section/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 20:32:23 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=949</guid>
		<description><![CDATA[The goal here is to make a list of posts in the sidebar that show a number of recent posts. There will be a button you can click which will replaces those links to recent posts with older posts, AJAX style. You can keep clicking the button and keep getting older and older posts. On [...]]]></description>
			<content:encoded><![CDATA[<p>The goal here is to make a list of posts in the sidebar that show a number of recent posts. There will be a button you can click which will replaces those links to recent posts with older posts, AJAX style. You can keep clicking the button and keep getting older and older posts. On this site, we currently show 5 recent posts. So this little section shows the 5 posts <em>after that</em>, then clicking the button once will show 5 more older than that, and so on. </p>
<p><span id="more-949"></span></p>
<h3>1. Create a Page Template for &#8220;Additional Posts&#8221;</h3>
<p>All this page template does is display a list of 5 posts. Totally raw and unstyled. No DOCTYPE, no footer, no nothing.</p>
<pre><code>&lt;?php
    /*
        Template Name: Additional Posts
    */
        
    $offset = htmlspecialchars(trim($_GET['offset']));
    if ($offset == '') {
        $offset = 5;
    }
   
    query_posts('showposts=5&amp;offset='.$offset); 
?&gt;

&lt;ul&gt;
    &lt;?php    
        if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
            &lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;?php 
        endwhile; endif; 
    ?&gt;
&lt;/ul&gt;</code></pre>
<p>The only thing special about this template is that it pulls down the URL parameter &#8220;offset&#8221; and scrubs it. The query_posts function will use that offset number to return posts older than that number.</p>
<h3>2. Make sure jQuery is loaded.</h3>
<p>We&#8217;re going to use the powers of jQuery to do this, so make sure it&#8217;s loaded. Here is a simple way to get that done, make sure this is above the wp_head() function.</p>
<pre><code>&lt;?php wp_enqueue_script('jquery'); ?&gt;</code></pre>
<p>I often like to load it directly from Google. To do that, <a href="http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/">see here.</a></p>
<h3>3. Make a new script (or append to your already exisiting script)</h3>
<p>We&#8217;re going to write a little JavaScript here, so this is where we&#8217;ll do that. Call this AFTER the wp_head() call so it will surely run after jQuery is loaded.</p>
<pre><code>&lt;script type="text/javascript" src="&lt;?php bloginfo('template_url') ?&gt;/js/YOUR-SCRIPT.js"&gt;&lt;/script&gt;</code></pre>
<h3>4. Make a home for the list of posts</h3>
<p>On our site, we are putting this in our sidebar (sidebar.php file). </p>
<pre><code>&lt;h4&gt;More Posts&lt;/h4&gt;
&lt;div id="postContainer"&gt;... loading ...&lt;/div&gt;
&lt;p&gt;&lt;a href="#" id="another"&gt;Get more!&lt;/a&gt;&lt;/p&gt;</code></pre>
<p>The #postContainer div will be the target for loading up the new list of articles. The #another link will have a click handler tied to it which will trigger the AJAX load.</p>
<h3>5. Create a new page with your new page template</h3>
<p>Go ahead and publish a new page. It doesn&#8217;t matter what the title is, just select your new page template from the page template dropdown menu. Click the View button to make sure it loads up a list of posts OK. Note the URL of this page.</p>
<h3>6. Write JavaScript to do AJAX load</h3>
<p>We&#8217;ll put jQuery in noConflict mode here just to be super safe.</p>
<pre><code>var $j = jQuery.noConflict(); 

$j(function(){

    var offset = 5;

    $j("#postContainer").load("/additional-posts/?offset="+offset);
    $j("#another").click(function(){
        
        offset = offset+5;
    
        $j("#postContainer")
            .slideUp()
            .load("/additional-posts/?offset="+offset, function() {
                $j(this).slideDown();
            });
            
        return false;
    });

});</code></pre>
<p>In plain English:</p>
<ol>
<li>When the DOM is ready&#8230;</li>
<li>Set variable offset to 5</li>
<li>Load the contents of the URL /additional-posts/?offset=5 into the element with ID postContainer</li>
<li>When the &#8220;more&#8221; button is clicked&#8230;</li>
<li>Increment the offset variable by 5</li>
<li>Slide up the container</li>
<li>Load the URL again, with a new offset parameter</li>
<li>When done, slide the container back down</li>
</ol>
<h3>That&#8217;s it!</h3>
<p>For a demo, check out our current sidebar on <a href="http://digwp.com">Digging Into WordPress</a>. Thanks to Erik Lundmark for the idea.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/11/make-an-infinite-more-posts-section/">Permalink</a> | <a href="http://digwp.com/2009/11/make-an-infinite-more-posts-section/#comments">9 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/11/make-an-infinite-more-posts-section/&title=Make an Infinite More-Posts Section">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a>, <a href="http://digwp.com/tag/pages/" rel="tag">pages</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/11/make-an-infinite-more-posts-section/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Password Protect More Than the_content()</title>
		<link>http://digwp.com/2009/08/password-protect-more-than-the_content/</link>
		<comments>http://digwp.com/2009/08/password-protect-more-than-the_content/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 00:59:47 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[custom-fields]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=531</guid>
		<description><![CDATA[WordPress has the ability to easily password protect the content of any Post or Page. Right over by that big juicy blue &#8220;Publish&#8221; button, there is an option for Visibility. Click edit, and you have the option to make it password-protected and set a password. What happens now? By default, WordPress will prepend &#8220;Protected: &#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has the ability to easily password protect the content of any Post or Page. Right over by that big juicy blue &#8220;Publish&#8221; button, there is an option for <strong>Visibility</strong>. Click edit, and you have the option to make it password-protected and set a password. </p>
<p><span id="more-531"></span></p>
<h3>What happens now?</h3>
<p>By default, WordPress will prepend &#8220;Protected: &#8221; to the title of the post, and replace the content area (and comments area) with a default message:</p>
<blockquote><p>This post is password protected. To view it please enter your password below:</p></blockquote>
<p>After the message, a form prompting the user to enter a password. It looks like this:</p>
<p><img src="http://digwp.com/wp-content/blog-images/pw-protect-example.png" width="470" height="363" alt="" title="" /></p>
<h3>Shortcomings</h3>
<p>Like we said above, what actually gets &#8220;protected&#8221; is the content of the post and the comments for the post. In the actual theme files, that boils down to:</p>
<ul>
<li><code>&lt;?php the_content(); ?&gt;</code></li>
<li><code>&lt;?php comments_template(); ?&gt;</code></li>
</ul>
<p><strong>Anything else</strong> is <strong>not protected!</strong> This includes pulling and displaying custom fields for that Post. Theoretically, this makese sense, as WordPress works by functions, and cannot be expected to know what&#8217;s going on in every single custom theme. However, custom fields can potentially contain important information that you might <strong>want to be hidden</strong> via password protection.  </p>
<p>Here is a little code snapshot from the single.php template right here on DiW:</p>
<p><img src="http://digwp.com/wp-content/blog-images/codeprotected.png" width="590" height="183" alt="" title="" /></p>
<h3>&#8211; UPDATE &#8211;</h3>
<p>The easiest possible way to password protect additional areas is with this simple function:</p>
<pre><code>&lt;?php
    if ( !post_password_required() ) { 
            echo 'protected stuff'; 
    }
?&gt;</code></pre>
<p>Thanks <a href="http://jayj.dk/">Jay</a>!</p>
<h3>The Password Form</h3>
<p>This is the form code that gets generated for a password protected post:</p>
<pre><code>&lt;form method="post" action="http://digwp.com/wp-pass.php"&gt;
&lt;p&gt;This post is password protected. To view it please enter your password below:&lt;/p&gt;
&lt;p&gt;&lt;label for="pwbox-531"&gt;Password:&lt;br/&gt;
&lt;input type="password" size="20" id="pwbox-531" name="post_password"/&gt;&lt;/label&gt;&lt;br/&gt;
&lt;input type="submit" value="Submit" name="Submit"/&gt;&lt;/p&gt;
&lt;/form&gt;</code></pre>
<p>The ID&#8217;s values for you will be different, but that&#8217;s the gist of it. This is a bit of a tangent, but let&#8217;s say you want to style this form. Good luck, form itself nor the labels or inputs have any class names or ID&#8217;s that you can latch onto to style while not potentially affecting other forms. </p>
<p>I thought about the <code>body_class</code> function for a moment, thinking perhaps password protected posts at least get a class for this, but no dice.</p>
<p><strong>I think this is a problem&#8230; WordPress should get us some hook here!</strong></p>
<h3>Hiding more!</h3>
<p>Fair warning, this is a little hacky, as it&#8217;s going to rely on JavaScript, which is client side technology that can be turned off, rather than server side technology which cannot.</p>
<p>There is <em>one</em> thing that is unique in that password form code above (hence the tangent), that we can look for with JavaScript. The idea is this:</p>
<ol>
<li>Look for password input</li>
<li>If it is present&#8230;</li>
<li>Hide other stuff.</li>
</ol>
<p>We&#8217;ll use jQuery for this, because it makes it easy and that&#8217;s just how I roll. The example below is taken from a site I worked on recently where I was displaying an audio track with custom fields, which was displaying even when the post was protected.</p>
<pre><code>if ( $('input[name="post_password"]').length &gt; 0 ) {
    // hide stuff
    $(".audioplayer").hide();
}</code></pre>
<h3>Dealing with that Title</h3>
<p>One of the other potential annoyances with password-protected posts is how it appends &#8220;Protected&#8221; to post titles. This isn&#8217;t an option, so removing it is a matter of adding a filter to the <code>the_title</code> function. Add this to the functions.php file:</p>
<pre><code>function the_title_trim($title)
{
    $pattern[0] = '/Protected:/';
    $pattern[1] = '/Private:/';
    $replacement[0] = ''; // Enter some text to put in place of Protected:
    $replacement[1] = ''; // Enter some text to put in place of Private:
    
    return preg_replace($pattern, $replacement, $title);
}
add_filter('the_title', 'the_title_trim');</code></pre>
<p>If it&#8217;s not obvious, this will also remove the &#8220;Private: &#8221; from being appended to private post titles.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/08/password-protect-more-than-the_content/">Permalink</a> | <a href="http://digwp.com/2009/08/password-protect-more-than-the_content/#comments">9 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/08/password-protect-more-than-the_content/&title=Password Protect More Than the_content()">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/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/08/password-protect-more-than-the_content/feed/</wfw:commentRss>
		<slash:comments>9</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>Use Google-Hosted JavaScript Libraries (&#8230;still the Right Way)</title>
		<link>http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/</link>
		<comments>http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 21:05:04 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://diggingintowordpress.com/?p=136</guid>
		<description><![CDATA[I previously posted on how to include jQuery in your WordPress theme the Right Way. That is, to use the wp_register_script function to register the script first. It&#8217;s literally a one-liner in your header.php or functions.php file, but by default, it loads the internal version of jQuery that ships with WordPress. It&#8217;s all the rage [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/">previously posted</a> on how to include jQuery in your WordPress theme the Right Way. That is, to use the <code>wp_register_script</code> function to register the script first. It&#8217;s literally a one-liner in your header.php or functions.php file, but by default, it loads the internal version of jQuery that ships with WordPress. </p>
<p>It&#8217;s all the rage these days (for good reason), to use Google-hosted JavaScript libraries. They even encourage it. If you&#8217;d like to hop on the bandwagon, you can do so and still use the proper <code>wp_register_script</code> function. </p>
<p><span id="more-136"></span></p>
<h3>UPDATE (December 2011)</h3>
<p>As of WordPress 3.3, this is the best way to do it, using the proper hook (thanks David Hollander):</p>
<pre><code>//jQuery Insert From Google
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}</code></pre>
<p><strike>Just add this code to your <code>functions.php</code> file:</p>
<pre><code>if( !is_admin()){
   wp_deregister_script('jquery'); 
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"), false, '1.3.2'); 
   wp_enqueue_script('jquery');
}</code></pre>
<p></strike></p>
<p>That last line there gets the script queued up for you so you don&#8217;t need to do it again in the header. Assuming you have the proper wp_head(); call in your header, you&#8217;ll be all set! Note that we have wrapped this in a <a href="http://codex.wordpress.org/Conditional_Tags">conditional tag</a> to prevent that action on Admin pages. Because this Google-hosted version of jQuery is not in <code>noConflict</code> mode, it interferes with the rich text editor in the admin. This prevents that.</p>
<p>Remember, the reason for all this is to ensure that other WordPress stuff (plugins&#8230;) are aware that the script is already loaded so don&#8217;t go around loading another copy. Loading two copies of a JS library is just bad news.</p>
<p>Also, I always use jQuery as an example but of course this would work with any popular JavaScript library. For example, the MooTools path at Google is:</p>
<pre><code>http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools-yui-compressed.js</code></pre>
<p>Google&#8217;s <a href="http://code.google.com/apis/ajaxlibs/documentation/index.html">AJAX Developer&#8217;s API Guide is here</a>.</p>
<p>ALSO&#8230; there are a crapload of scripts all ready to be queued up for your use in WordPress. See the <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">full list here</a>. </p>
<p>David wrote in to tell us that he was working in an HTTPS environment, and he used this alteration to ensure the URL for the Google-hosted version of jQuery was properly served from HTTPS when needed:</p>
<pre><code>if (!is_admin()) {
   wp_deregister_script('jquery'); 
   wp_register_script('jquery', ("http".($_SERVER['SERVER_PORT'] == 443 ? "s" : "")."://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false, '1.4.2');
   wp_enqueue_script('jquery');
}</code></pre>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/">Permalink</a> | <a href="http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/#comments">22 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/&title=Use Google-Hosted JavaScript Libraries (&#8230;still the Right Way)">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/header/" rel="tag">header</a>, <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Including jQuery in WordPress (The Right Way)</title>
		<link>http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/</link>
		<comments>http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 01:58:16 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://modernwordpress.com/?p=36</guid>
		<description><![CDATA[If you want, you can just download jQuery, put it on your server and link to it from your header.php file in the &#60;head&#62; section. But that can cause you grief. For one thing, some plugins use the jQuery library, and they are going to load it as well. This can cause problems. How was [...]]]></description>
			<content:encoded><![CDATA[<p>If you want, you can just download <a href="http://jquery.com">jQuery</a>, put it on your server and link to it from your <tt>header.php</tt> file in the <tt>&lt;head&gt;</tt> section. But that can cause you grief. For one thing, some plugins use the jQuery library, and they are going to load it as well. This can cause problems. How was your plugin to know you already had it loaded?</p>
<p>Another thing is that WordPress already includes a copy of jQuery. Here is how you can load up jQuery in your theme the smart (and intended) way. Put the following code in your <tt>header.php</tt> file in the <tt>&lt;head&gt;</tt> section:</p>
<pre><code>&lt;?php wp_enqueue_script("jquery"); ?&gt;

&lt;?php wp_head(); ?&gt;</code></pre>
<p><span id="more-36"></span></p>
<p>Your theme probably already has the <tt>wp_head</tt> function, so just make sure you call the <tt>wp_enqueue_script</tt> function BEFORE that. Now you are all set to call your own jQuery JavaScript file, AFTER the <tt>wp_head</tt> function.</p>
<pre><code>&lt;script type="text/javascript"
   src="&lt;?php bloginfo("template_url"); ?&gt;/js/yourScript.js"&gt;&lt;/script&gt;</code></pre>
<p>You are ready to rock, but there are still some considerations. For example, safeguarding yourself from the future possibility of conflict with other libraries. You really shouldn&#8217;t be using multiple JS libraries to begin with, but&#8230; better safe than sorry.</p>
<p>To be super-safe, you can put jQuery into &#8220;no conflict&#8221; mode and use a different shortcut for jQuery. In this case &#8220;$j&#8221; instead of the default &#8220;$&#8221;. The standard &#8220;$&#8221;, for example, can conflict with Prototype. Here is an example of a safe bit of jQuery JavaScript:</p>
<pre><code>var $j = jQuery.noConflict();

$j(function(){

    $j("#sidebar li a").hover(function(){
    	$j(this).stop().animate({
    		paddingLeft: "20px&amp;"
    	}, 400);
    }, function() {
    	$j(this).stop().animate({
    		paddingLeft: 0
    	}, 400);
    });

});</code></pre>
<p>Can you recognize this bit of code? We use it right here on Modern WordPress to do the cool &#8220;link nudging&#8221; in the sidebar!</p>
<h3>Translations</h3>
<ul>
<li><a href="http://webhostingrating.com/libs/including-jquery-in-wordpress-be">Belorussian translation</a></li>
</ul>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/">Permalink</a> | <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/#comments">59 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/&title=Including jQuery in WordPress (The Right Way)">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/header/" rel="tag">header</a>, <a href="http://digwp.com/tag/jquery/" rel="tag">jquery</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		</item>
	</channel>
</rss>

