<?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; Theme</title>
	<atom:link href="http://digwp.com/category/theme/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Thu, 09 Feb 2012 19:03:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Displaying Theme Data with WordPress</title>
		<link>http://digwp.com/2011/12/displaying-theme-data-with-wordpress/</link>
		<comments>http://digwp.com/2011/12/displaying-theme-data-with-wordpress/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 18:36:29 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5457</guid>
		<description><![CDATA[A cool trick you can do with WordPress is display information directly from your theme&#8217;s style.css stylesheet. I recently used this on a site where the theme&#8217;s version number is used throughout the template to keep things current and consistent. get_theme_data() The function that makes it possible is called get_theme_data(), and it simply returns an [...]]]></description>
			<content:encoded><![CDATA[<p>A cool trick you can do with WordPress is display information directly from your theme&#8217;s <code>style.css</code> stylesheet. I recently used this on a site where the theme&#8217;s version number is used throughout the template to keep things current and consistent.</p>
<p><span id="more-5457"></span></p>
<h3>get_theme_data()</h3>
<p>The function that makes it possible is called <a href="http://codex.wordpress.org/Function_Reference/get_theme_data">get_theme_data()</a>, and it simply returns an array of information about any of your theme files. Here is how it&#8217;s used in your theme template:</p>
<pre><code>&lt;?php get_theme_data( $theme_filename ); ?&gt;</code></pre>
<p>So <code>$theme_filename</code> is required and should be the path and filename of your theme&#8217;s <code>style.css</code> file. There is no default value for this, so you need to ensure a proper value.</p>
<p>So what information can you display with <code>get_theme_data()</code>? The function returns an array of values that basically comprises the different meta items in your stylesheet. Here is a list of possible return values (all strings):</p>
<ul>
<li><code>Name</code> &ndash; theme name</li>
<li><code>Title</code> &ndash; either theme name or linked theme name</li>
<li><code>URI</code> &ndash; theme <abbr title="Uniform Resource Identifier">URI</abbr></li>
<li><code>Description</code> &ndash; wptexturized version of the theme name</li>
<li><code>AuthorURI</code> &ndash; theme author URI</li>
<li><code>Template</code> &ndash; theme parent template, if exists</li>
<li><code>Version</code> &ndash; theme version number</li>
<li><code>Status</code> &ndash; theme status (default: <code>publish</code>)</li>
<li><code>Tags</code> &ndash; theme tags</li>
<li><code>Author</code> &ndash; author name or linked author name</li>
</ul>
<p>Note that these return values are <strong>case-sensitive</strong> and will not work if the first letter is not capitalized.</p>
<h3>Examples</h3>
<p>The Codex provides this example for getting and displaying the theme <code>Name</code> and <code>Author</code>:</p>
<pre><code>&lt;?php

    /*
     * Assign theme folder name that you want to get information.
     * make sure theme exist in wp-content/themes/ folder.
     */

    $theme_name = 'twentyeleven'; 

   /*
    * Do not use get_stylesheet_uri() as $theme_filename,
    * it will result in PHP fopen error if allow_url_fopen is set to Off in php.ini,
    * which is what most shared hosting does. You can use get_stylesheet_directory()
    * or get_template_directory() though, because they return local paths.
    */

    $theme_data = get_theme_data( get_theme_root() . '/' . $theme_name . '/style.css' );
    echo $theme_data['Title'];
    echo $theme_data['Author'];

?&gt;</code></pre>
<p>This should get you going.. just copy/paste into your theme template and indicate the theme name in that first line of code. Displaying other bits of theme data is matter of editing/replicating those last two lines. </p>
<p>Once you see how it works, you can do cool stuff with it, like display your theme&#8217;s version number sort of globally throughout your site. I&#8217;m using this technique to append version parameters to my stylesheet <abbr title="Uniform Resource Locator">URL</abbr>s, like so:</p>
<pre><code>&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/style.css&lt;?php if(function_exists('theme_version')) theme_version(); ?&gt;"&gt;</code></pre>
<p>..and the output markup looks like this (notice the appended version parameter, <code>?v=1.3</code>):</p>
<pre><code>&lt;link rel="stylesheet" href="http://example.com/wp-content/themes/xycss/style.css?v=1.3"&gt;</code></pre>
<p>Because I am using multiple stylesheets, this method really saves a LOT of time trying to keep track of everything. Here is the <code>theme_version()</code> function that goes in your theme&#8217;s <code>functions.php</code> file:</p>
<pre><code>// display version number
function theme_version() {
    $theme_name = 'xycss'; // customize with your theme name
    $theme_data = get_theme_data( get_theme_root() . '/' . $theme_name . '/style.css' );
    echo '?v=' . $theme_data['Version'];
}</code></pre>
<p>And with a few modifications, you can also display your theme&#8217;s information in your posts and pages via <strong>shortcode</strong>:</p>
<pre><code>// version number shortcode
function theme_version_shortcode() {
    $theme_name = 'xycss'; // customize with your theme name
    $theme_data = get_theme_data( get_theme_root() . '/' . $theme_name . '/style.css' );
    return $theme_data['Version'];
}
add_shortcode('theme_version', 'theme_version_shortcode');</code></pre>
<p>With that second snippet in your <code>functions.php</code> file, displaying your theme info directly in pages is as easy as writing this:</p>
<p><code>[theme_version]</code></p>
<p>..and the output:</p>
<p><code>1.3</code></p>
<p>It&#8217;s pretty cool stuff, and I&#8217;m sure there&#8217;s tons more you can do with it too. If you know any tricks or tips, please share in the comments, Thanks :)</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/12/displaying-theme-data-with-wordpress/">Permalink</a> | <a href="http://digwp.com/2011/12/displaying-theme-data-with-wordpress/#comments">10 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/12/displaying-theme-data-with-wordpress/&title=Displaying Theme Data with WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/12/displaying-theme-data-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>3 Ways to Reset the WordPress Loop</title>
		<link>http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/</link>
		<comments>http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 16:28:23 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5189</guid>
		<description><![CDATA[WordPress does an excellent job of keeping track of what&#8217;s happening with the loop, but once you start customizing parameters and setting up multiple loops, it&#8217;s a good idea to explicitly reset them using one of three WordPress template tags. In this DiW post, we&#8217;ll explore these techniques to get a better understanding of when [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress does an excellent job of keeping track of what&#8217;s happening with <a href="http://digwp.com/2011/05/loops/" title="4 Ways to Loop with WordPress">the loop</a>, but once you start customizing parameters and setting up <a href="http://perishablepress.com/press/tag/loops/" title="Posts about WordPress Loops">multiple loops</a>, it&#8217;s a good idea to explicitly <strong>reset</strong> them using one of <em>three WordPress template tags</em>. In this <abbr title="Digging into WordPress">DiW</abbr> post, we&#8217;ll explore these techniques to get a better understanding of when and how to use them in your WordPress themes.</p>
<p><span id="more-5189"></span></p>
<ul>
<li><a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#wp_reset_postdata">wp_reset_postdata()</a></li>
<li><a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#wp_reset_query">wp_reset_query()</a></li>
<li><a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#rewind_posts">rewind_posts()</a></li>
<li><em><a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#article-summary">Quick Summary</a></em></li>
</ul>
<h3 id="wp_reset_postdata">wp_reset_postdata()</h3>
<p>First up we have <a href="http://codex.wordpress.org/Function_Reference/wp_reset_postdata" title="Function Reference/wp reset postdata">wp_reset_postdata</a>, which restores the global <code>$post</code> variable to the current post in the main query. This is useful when using <code>WP_Query</code> to customize loops or create multiple loops on the same page. It looks like this:</p>
<p><code>&lt;?php wp_reset_postdata(); ?&gt;</code></p>
<p>This tag is nice and simple &ndash; it accepts no parameters and returns no values. It simply resets the post data after a custom query. So for example, let&#8217;s say we have a custom <code>WP_Query</code> loop in our theme&#8217;s <code>header.php</code> file, something like this:</p>
<pre><code>$random_post = new WP_query(); 
$random_post-&gt;query('cat=3&amp;showposts=1&amp;orderby=rand'); 
while ($random_post-&gt;have_posts()) : $random_post-&gt;the_post(); 
&lt;a href="&lt;?php the_permalink() ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;
	&lt;img src="&lt;?php echo get_post_meta($random_post-&gt;ID, 'featured', true); ?&gt;"&gt;
&lt;/a&gt;
endwhile;</code></pre>
<p>This would display a random post in the header, but it will also change the main query object for any other loops on the page. Without the original query data available, the main posts loop contained in, say, <code>index.php</code> may produce unexpected results. Fortunately, we can use <code>wp_reset_postdata</code> to restore the query object to its original state.</p>
<p>To use <code>wp_reset_postdata</code>, just place after your custom loop(s). Here is our previous example with <code>wp_reset_postdata</code> to reset the loop:</p>
<pre><code>$random_post = new WP_query(); 
$random_post-&gt;query('cat=3&amp;showposts=1&amp;orderby=rand'); 
while ($random_post-&gt;have_posts()) : $random_post-&gt;the_post(); 
&lt;a href="&lt;?php the_permalink() ?&gt;" title="&lt;?php the_title(); ?&gt;"&gt;
	&lt;img src="&lt;?php echo get_post_meta($random_post-&gt;ID, 'featured', true); ?&gt;"&gt;
&lt;/a&gt;
endwhile;
wp_reset_postdata();</code></pre>
<p class="highlight-text"><strong>When to use:</strong> best used after custom or multiple loops created with <code>WP_Query</code>.</p>
<h3 id="wp_reset_query">wp_reset_query()</h3>
<p>Next up we have the <a href="http://codex.wordpress.org/Function_Reference/wp_reset_query" title="Function Reference/wp reset query">wp_reset_query</a> function, which resets the query used with custom loops. It&#8217;s a simple function that accepts no parameters and returns no values. It looks like this:</p>
<p><code>&lt;?php wp_reset_query(); ?&gt;</code></p>
<p>Specifically, this function was <a href="http://core.trac.wordpress.org/ticket/4741" title="Multiple $wp_query objects mess a lot of things up">created to prevent issues with query_posts</a>, as indicated for the function in the <code>wp-includes/query.php</code> file:</p>
<pre><code>/**
 * Destroy the previous query and set up a new query.
 *
 * This should be used after {@link query_posts()} and before another {@link
 * query_posts()}. This will remove obscure bugs that occur when the previous
 * wp_query object is not destroyed properly before another is set up.
 *
 * @since 2.3.0
 * @uses $wp_query
 */</code></pre>
<p>Looking at the <code>wp_reset_query</code> function in <code>query.php</code>, we see that it uses the adjacent <a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#wp_reset_postdata">wp_reset_postdata</a> function to restore the global <code>$post</code> variable to the current post in the main query:</p>
<pre><code>// destroy and reset the query
function wp_reset_query() {
	unset($GLOBALS['wp_query']);
	$GLOBALS['wp_query'] =&amp; $GLOBALS['wp_the_query'];
	wp_reset_postdata(); // &lt;-- RESET QUERY
}

// restore the query
function wp_reset_postdata() {
	global $wp_query;
	if ( !empty($wp_query-&gt;post) ) {
		$GLOBALS['post'] = $wp_query-&gt;post;
		setup_postdata($wp_query-&gt;post);
	}
}</code></pre>
<p>So both <code>wp_reset_query()</code> and <code>wp_reset_postdata()</code> reset the query object by restoring the global <code>$post</code> variable, but <code>wp_reset_query</code> takes it a step further and actually <em>destroys</em> the previous query before doing so. This is important when using <code>query_posts()</code>, as seen in the following example:</p>
<pre><code>&lt;?php query_posts('posts_per_page=3');
if (have_posts()) : while (have_posts()) : the_post(); ?&gt;

&lt;h1&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;

&lt;?php endwhile; endif; ?&gt;
&lt;?php wp_reset_query(); ?&gt;</code></pre>
<p class="highlight-text"><strong>When to use:</strong> best used after a <code>query_posts</code> loop to reset things after a custom query.</p>
<h3 id="rewind_posts">rewind_posts()</h3>
<p>Last but not least, we have the <a href="http://codex.wordpress.org/Function_Reference/rewind_posts" title="Function Reference/rewind posts">rewind_posts</a> function, which basically does what it says: rewinds the loop so you can re-use the same query. The function accepts no parameters, returns no values, and looks like this when used in your theme files:</p>
<p><code>&lt;?php rewind_posts(); ?&gt;</code></p>
<p>So to understand when to use <code>rewind_posts()</code>, let&#8217;s say we want to use the same query in two different locations on the page. We want to display post titles in the first loop and post content in the second loop. To re-use the same posts, we include <code>rewind_posts</code> after the first loop, like so:</p>
<pre><code>if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
&lt;h1&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;?php endwhile; endif; ?&gt;

&lt;?php rewind_posts(); ?&gt;

&lt;?php while (have_posts()) : the_post(); ?&gt;
&lt;?php the_content(); ?&gt;
&lt;?php endwhile; ?&gt;</code></pre>
<p>So, while <code>wp_reset_query</code> and <code>wp_reset_postdata</code> reset the entire query object, <code>rewind_posts</code> simply resets the post count, as seen for the function in the <code>wp-includes/query.php</code> file:</p>
<pre><code>// rewind the posts and reset post index
function rewind_posts() {
	$this-&gt;current_post = -1;
	if ( $this-&gt;post_count &gt; 0 ) {
		$this-&gt;post = $this-&gt;posts[0];
	}
}</code></pre>
<p class="highlight-text"><strong>When to use:</strong> best for re-using the same query on the same page.</p>
<h3 id="article-summary">5-second summary</h3>
<p>Quick summary for future reference:</p>
<ul>
<li><strong><code>wp_reset_postdata()</code></strong> -&gt; best used after custom or multiple loops created with <code>WP_Query</code></li>
<li><strong><code>wp_reset_query()</code></strong> -&gt; best used after a <code>query_posts</code> loop to reset a custom query</li>
<li><strong><code>rewind_posts()</code></strong> -&gt; best for re-using the same query on the same page</li>
</ul>
<p>I hope this is a useful round-up of when &amp; how to reset/rewind the WordPress loop. Note that the <a href="http://codex.wordpress.org/">WP Codex</a> information on these functions is kinda thin, so feel free to share any tips or examples in the comments.</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/">Permalink</a> | <a href="http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/#comments">11 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/&title=3 Ways to Reset the WordPress Loop">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/loop/" rel="tag">loop</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a>, <a href="http://digwp.com/tag/tricks/" rel="tag">tricks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/09/3-ways-to-reset-the-wordpress-loop/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>4 Ways to Loop with WordPress</title>
		<link>http://digwp.com/2011/05/loops/</link>
		<comments>http://digwp.com/2011/05/loops/#comments</comments>
		<pubDate>Wed, 11 May 2011 16:44:06 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[query_posts]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=4013</guid>
		<description><![CDATA[At the heart of the WordPress theme template is the venerable WordPress loop. When you&#8217;re looking at your index.php file, for example, the loop is the part that typically begins with if(have_posts()) and contains all the tags and markup used to generate the page. The default loop works perfectly well for most single-loop themes, but [...]]]></description>
			<content:encoded><![CDATA[<p>At the heart of the WordPress theme template is the venerable WordPress loop. When you&#8217;re looking at your <code>index.php</code> file, for example, the loop is the part that typically begins with <code>if(have_posts())</code> and contains all the tags and markup used to generate the page. The default loop works perfectly well for most single-loop themes, but for more advanced designs with stuff like multiple and custom loops, more looping power is needed. Fortunately, WordPress provides plenty of flexibility with  <strong>four ways to loop</strong>:</p>
<p><span id="more-4013"></span></p>
<ul>
<li><a href="#default-loop">Default Loop</a></li>
<li><a href="#query-posts">Loop with query_posts()</a></li>
<li><a href="#wp-query">Loop with WP_Query()</a></li>
<li><a href="#get-posts">Loop with get_posts()</a></li>
</ul>
<p>Each of these looping methods is useful in a variety of situations. They share a lot of the same underlying functionality, and the query parameters are essentially the same. Collectively, these four techniques enable simple loops, multiple loops, and custom loops in your WordPress theme template. A good place to find a default loop, for example, is in your theme&#8217;s <code>index.php</code> file. Its purpose is to loop through the posts stored in the database and echo their contents to the browser. Using WordPress&#8217; template tags, it&#8217;s easy to display post titles, content, meta info, and much more. That said, let&#8217;s examine the four ways to loop with WordPress.</p>
<h3 id="default-loop">The Default Loop</h3>
<p>The default <a href="http://codex.wordpress.org/The_Loop">WordPress loop</a> looks something like this:</p>
<pre><code>&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;

	&lt;div &lt;?php post_class(); ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
		&lt;h1&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
		&lt;?php the_content(); ?&gt;
	&lt;/div&gt;

&lt;?php endwhile; ?&gt;

	&lt;div class="navigation"&gt;
		&lt;div class="next-posts"&gt;&lt;?php next_posts_link(); ?&gt;&lt;/div&gt;
		&lt;div class="prev-posts"&gt;&lt;?php previous_posts_link(); ?&gt;&lt;/div&gt;
	&lt;/div&gt;

&lt;?php else : ?&gt;

	&lt;div &lt;?php post_class(); ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
		&lt;h1&gt;Not Found&lt;/h1&gt;
	&lt;/div&gt;

&lt;?php endif; ?&gt;</code></pre>
<p>So what makes it &#8220;default&#8221;? Mostly because it uses the <em>default query</em> to loop through post content, making it the loop used like 99% of the time for most themes. It tells WordPress to loop through posts and display the information according to context, and as called by the various template tags (<code>the_title</code>, <code>the_content</code>, et al). There are tags available for just about any type of data stored in the database. </p>
<p>Based on the query that is sent, the default loop will display a certain number of posts from a certain category from a certain date, and so on. For example, the number of posts displayed in the first part of the loop is specified in the WP Admin. So if someone requests the second page of your &#8220;Awesome&#8221; category, that information is sent via the query, along with the number of posts, theme template file, and so on.</p>
<p>So the default loop is perfect if you&#8217;re happy with the query that is sent, but it is also possible to customize the query and generate an entirely different set of posts.</p>
<h3 id="query-posts">Loop with query_posts()</h3>
<p>The <a href="http://codex.wordpress.org/Function_Reference/query_posts">query_posts</a> function enables us to modify the query and display our desired results. We can either override the entire query or keep it around and just change a few parameters. Here&#8217;s an example where <code>query_posts</code> is called before the default loop to exclude a specific category:</p>
<pre><code>&lt;?php global $query_string; // required
$posts = query_posts($query_string.'&amp;cat=-9'); // exclude category 9

&lt;?php // DEFAULT LOOP GOES HERE ?&gt;

&lt;?php wp_reset_query(); // reset the query ?&gt;</code></pre>
<p>Say you have a default loop in your <code>index.php</code> theme template, but you want to change the number of posts, exclude two categories, and display the results in ascending order. Easy. Just add some <code>query_posts</code> action <em>before</em> the default loop and <code>wp_reset_query</code> immediately <em>after</em>, like this:</p>
<pre><code>&lt;?php global $query_string; // required
$posts = query_posts($query_string.'&amp;posts_per_page=3&amp;cat=-6,-9&amp;order=ASC');

&lt;?php // DEFAULT LOOP GOES HERE ?&gt;

&lt;?php wp_reset_query(); // reset the query ?&gt;</code></pre>
<p>Here we are keeping the original query around and just overriding a few parameters. There are <a href="http://codex.wordpress.org/Function_Reference/WP_Query#Parameters">many parameters available</a>, so customizing any default loop is accomplished quite easily. If we wanted to completely override the original query, we would replace the second line with something like this:</p>
<p><code>$posts = query_posts('posts_per_page=3&amp;cat=-6,-9&amp;order=ASC');</code></p>
<p>Notice here that we&#8217;ve removed the <code>$query_string</code> from the <code>query_posts</code> parameters. This essentially erases the default query and replaces it with one that contains only those variables included in <code>query_posts</code>. This means no paging information will be available, so remove the original query only if you know what you&#8217;re doing.</p>
<p><strong>When to use?</strong> Use <code>query_posts</code> to modify the type of posts that are returned for a single loop. It’s perfect for limiting the number of posts, excluding posts from a certain category or tag, and so on. If <em>more than one loop</em> is required, multiple <code>query_posts</code> loops <em>could</em> work, but there is a much better way to do it using <code>WP_Query</code>.</p>
<h3 id="wp-query">Loop with WP_Query()</h3>
<p>For <em>complete control</em> over the customization of <em>any number</em> of loops, <a href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query</a> is the way to go. When used to modify a default loop, it looks similar to <code>query_posts</code>. For example, let&#8217;s exclude a specific category using <code>WP_Query</code>:</p>
<pre><code>&lt;?php $custom_query = new WP_Query('cat=-9'); // exclude category 9
while($custom_query-&gt;have_posts()) : $custom_query-&gt;the_post(); ?&gt;

	&lt;div &lt;?php post_class(); ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
		&lt;h1&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
		&lt;?php the_content(); ?&gt;
	&lt;/div&gt;

&lt;?php endwhile; ?&gt;
&lt;?php wp_reset_postdata(); // reset the query ?&gt;</code></pre>
<p>It also accepts the same parameters as <code>query_posts</code>, so modifying stuff like number of posts, included/excluded categories, and post order looks quite familiar. As seen in the following examples, <code>WP_Query</code> makes it easy to customize the loop by simply changing the parameter:</p>
<pre><code>$custom_query = new WP_Query('cat=-7,-8,-9'); // exclude any categories
$custom_query = new WP_Query('posts_per_page=3'); // limit number of posts
$custom_query = new WP_Query('order=ASC'); // reverse the post order</code></pre>
<p>As expected, we can combine parameters with <code>WP_Query</code> using the same syntax as both <code>query_posts</code> and <code>get_posts</code>. Here’s the equivalent of our <code>query_posts</code> example:</p>
<p><code>$custom_query = new WP_Query('posts_per_page=3&amp;cat=-6,-9&amp;order=ASC');</code></p>
<p>Notice, however, that with <code>WP_Query</code>, we don’t need the <code>$query_string</code> variable. In addition to using <code>WP_Query</code> to customize the default loop, we can also use it to create and customize multiple loops. Here is a basic example:</p>
<pre><code>&lt;?php // Loop 1
$first_query = new WP_Query('cat=-1'); // exclude category
while($first_query-&gt;have_posts()) : $first_query-&gt;the_post();
...
endwhile;
wp_reset_postdata();

// Loop 2
$second_query = new WP_Query('cat=-2'); // exclude category
while($second_query-&gt;have_posts()) : $second_query-&gt;the_post();
...
endwhile;
wp_reset_postdata();

// Loop 3
$third_query = new WP_Query('cat=-3'); // exclude category
while($third_query-&gt;have_posts()) : $third_query-&gt;the_post();
...
endwhile;
wp_reset_postdata();
?&gt;</code></pre>
<p>Each of these additional loops may be placed anywhere in your theme template &ndash; no need to line them up sequentially. For example, one loop may be placed in your sidebar, another in your footer, and so on. And with the output of each loop easily modified using any of the available parameters, any loop configuration is possible.</p>
<p><strong>When to use?</strong> Use <code>WP_Query</code> for creating multiple, customized loops. By setting up additional instances of <code>WP_Query</code> in your theme, you can create any number of multiple loops, and customize the output of each. </p>
<p>Even so, we don&#8217;t always need to break out the big guns, sometimes we just need a few additional loops displayed around the page. So let&#8217;s put down the bazookas and gather in the garden for some <code>get_posts</code> tea&nbsp;;)</p>
<h3 id="get-posts">Loop with get_posts()</h3>
<p>The easiest, safest way to create multiple loops in your theme is to use <a href="http://codex.wordpress.org/Template_Tags/get_posts">get_posts()</a>. Anywhere you need to display a quick, <em>static</em> set of posts, <code>get_posts</code> is the perfect choice. Think 10 recent posts in the sidebar, or 10 random posts in the footer. <code>get_posts</code> makes it easy. Here again is a query to exclude a specific category:</p>
<pre><code>&lt;?php global $post; // required
$args = array('category' =&gt; -9); // exclude category 9
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
...
endforeach;
?&gt;</code></pre>
<p>This code creates a loop of all posts except those in the excluded category. Of course, excluding a category is just one way to customize your additional, static loops. By using any of the same parameters accepted by <code>WP_Query</code> and <code>query_posts</code>, it&#8217;s possible to create loops that display just about anything you want.</p>
<p>Notice, however, that <code>get_posts</code> requires the use of an <strong>array</strong> for the parameters. The format for multiple parameters looks like this (using our previous example):</p>
<p><code>$args = array('numberposts'=&gt;3, 'category'=&gt;-6,-9,  'order'=&gt;'ASC');</code></p>
<p>Also notice that we&#8217;re using <code>numberposts</code> instead of <code>posts_per_page</code> to limit the number of posts. According to the WP Codex, <code>posts_per_page</code> should work with <code>get_posts</code>, but if it doesn&#8217;t just go with <code>numberposts</code>. There is also a <code>showposts</code> parameter that also seems to work fine with <code>get_posts</code>.</p>
<p><strong>When to use?</strong> Use the <code>get_posts()</code> function to easily create additional, static loops anywhere in your theme. <code>get_posts</code> accepts the same parameters as <code>query_posts</code>, and is perfect for adding static, custom loops to your sidebar, footer, or anywhere else.</p>
<h3>30-Second Summary</h3>
<p>The bottom line for customizing default loops and creating multiple loops:</p>
<ul>
<li>To modify the default loop, use <code>query_posts()</code></li>
<li>To modify loops and/or create multiple loops, use <code>WP_Query()</code></li>
<li>To create static, additional loops, use <code>get_posts()</code></li>
</ul>
<p>If you&#8217;re working with WordPress loops and want to learn more about using them to customize your theme, we cover the topic extensively in our book, <a href="http://digwp.com/" title="Complete WordPress Guide">Digging into WordPress</a>, which is current with WordPress 3.1&nbsp;:)</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/05/loops/">Permalink</a> | <a href="http://digwp.com/2011/05/loops/#comments">31 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/05/loops/&title=4 Ways to Loop with WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/loop/" rel="tag">loop</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a>, <a href="http://digwp.com/tag/query_posts/" rel="tag">query_posts</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/05/loops/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Custom Login/Register/Password Code</title>
		<link>http://digwp.com/2010/12/login-register-password-code/</link>
		<comments>http://digwp.com/2010/12/login-register-password-code/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 06:30:08 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[Admin]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=3102</guid>
		<description><![CDATA[In this DiW post, we transform three slices of code into a clean &#38; stylish tabbed menu that visitors can use to login, register, and recover passwords from anywhere in your site. Too many features &#38; details to explain up front, so check out the working demo to see the finished product. On the menu: [...]]]></description>
			<content:encoded><![CDATA[<p>In this <abbr title="Digging into WordPress">DiW</abbr> post, we transform three slices of code into a clean &amp; stylish tabbed menu that visitors can use to login, register, and recover passwords from anywhere in your site. Too many features &amp; details to explain up front, so <a href="http://digwp.com/custom-login-demo/" title="Custom Login Demo">check out the working demo</a> to see the finished product. On the menu:</p>
<p><span id="more-3102"></span></p>
<ul>
<li><a href="#default">Default WordPress Login Page</a></li>
<li><a href="#moving">Moving the login/register/password form</a></li>
<li><a href="#code">Custom WordPress template code</a></li>
<li><a href="#demo">Implement and Demo</a></li>
<li><a href="#custom">Customizing things</a></li>
<li><a href="#wrap">Wrap up</a></li>
</ul>
<h3 id="default">Default WordPress Login Page</h3>
<p>Out of the box, WordPress uses <code>wp-login.php</code> for logging into the Admin, retrieving lost passwords, and registering for site membership. Each of these activities are handled on the same page, commonly referred to as the WordPress Login Page. </p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-login-01.jpg" alt="[ Screenshot: Default WordPress Login/Register Page ]" /><br /><small>Yep, it&rsquo;s the WordPress Login Page</small></p>
<p>As seen here, the Login Page shows the log-in form by default. Beneath the form are two links, &ldquo;Register&rdquo; and &ldquo;Lost your password?&rdquo;, which enable users to (yep, you guessed it) register and recover their password. </p>
<p>The cool thing about the Login Page is that the page doesn&rsquo;t reload when either of these links are clicked. Instead, the form instantly changes from login to register or password to whatever. All three forms are included on the <code>wp-login.php</code> page and hidden/revealed as-needed using JavaScript. This &ldquo;same-page&rdquo; functionality is key to setting up our own custom login/register/password form elsewhere in our theme.</p>
<h3 id="moving">Moving the login/register/password form</h3>
<p>While it&rsquo;s not a good idea to move the entire <code>wp-login.php</code> file, it is possible to display the login/register/password forms anywhere in your theme. For example, putting the forms in your <code>sidebar.php</code> make it super-easy for visitors to register and login from anywhere in your site (here is an <a href="http://angry-birds.net/" title="Angry Birds Galaxy">example</a>). You could even create a WordPress page for each event: login, registration, and password-recovery pages that are customized/optimized in some really unique, bad-ass way.</p>
<p>The key to mobilizing the login forms is ensuring that they&rsquo;ll work properly regardless of placement (before, after, or within the loop) in your theme template files. We also want to ensure that normal visitors who aren&rsquo;t logged in see the forms, but logged-in users do not (or see alternate content). </p>
<p>Basically, it should work exactly like the default WordPress login functionality, but with one exception: instead of redirecting to the Admin Area (for log-ins) or to the Login Page (for registrations/password-recovery), we want the user to <em>remain on the same page</em>. This enables your guests to log-in, register, and reset passwords without leaving whatever page they happen to be visiting. Here&rsquo;s the code to make it happen..</p>
<h3 id="code">Custom WordPress template code</h3>
<p>Here is the code to display the login/register/password form anywhere in your theme:</p>
<p><a href="http://digwp.com/examples/CustomLoginCode/custom-login-02.txt" title="Custom Login/Register/Password Code">view as plain text</a>
<pre><code>&lt;div id="login-register-password"&gt;

	&lt;?php global $user_ID, $user_identity; get_currentuserinfo(); if (!$user_ID) { ?&gt;

	&lt;ul class="tabs_login"&gt;
		&lt;li class="active_login"&gt;&lt;a href="#tab1_login"&gt;Login&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="#tab2_login"&gt;Register&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="#tab3_login"&gt;Forgot?&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;div class="tab_container_login"&gt;
		&lt;div id="tab1_login" class="tab_content_login"&gt;

			&lt;?php $register = $_GET['register']; $reset = $_GET['reset']; if ($register == true) { ?&gt;

			&lt;h3&gt;Success!&lt;/h3&gt;
			&lt;p&gt;Check your email for the password and then return to log in.&lt;/p&gt;

			&lt;?php } elseif ($reset == true) { ?&gt;

			&lt;h3&gt;Success!&lt;/h3&gt;
			&lt;p&gt;Check your email to reset your password.&lt;/p&gt;

			&lt;?php } else { ?&gt;

			&lt;h3&gt;Have an account?&lt;/h3&gt;
			&lt;p&gt;Log in or sign up! It&amp;rsquo;s fast &amp;amp; &lt;em&gt;free!&lt;/em&gt;&lt;/p&gt;

			&lt;?php } ?&gt;

			&lt;form method="post" action="&lt;?php bloginfo('url') ?&gt;/wp-login.php" class="wp-user-form"&gt;
				&lt;div class="username"&gt;
					&lt;label for="user_login"&gt;&lt;?php _e('Username'); ?&gt;: &lt;/label&gt;
					&lt;input type="text" name="log" value="&lt;?php echo esc_attr(stripslashes($user_login)); ?&gt;" size="20" id="user_login" tabindex="11" /&gt;
				&lt;/div&gt;
				&lt;div class="password"&gt;
					&lt;label for="user_pass"&gt;&lt;?php _e('Password'); ?&gt;: &lt;/label&gt;
					&lt;input type="password" name="pwd" value="" size="20" id="user_pass" tabindex="12" /&gt;
				&lt;/div&gt;
				&lt;div class="login_fields"&gt;
					&lt;div class="rememberme"&gt;
						&lt;label for="rememberme"&gt;
							&lt;input type="checkbox" name="rememberme" value="forever" checked="checked" id="rememberme" tabindex="13" /&gt; Remember me
						&lt;/label&gt;
					&lt;/div&gt;
					&lt;?php do_action('login_form'); ?&gt;
					&lt;input type="submit" name="user-submit" value="&lt;?php _e('Login'); ?&gt;" tabindex="14" class="user-submit" /&gt;
					&lt;input type="hidden" name="redirect_to" value="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;" /&gt;
					&lt;input type="hidden" name="user-cookie" value="1" /&gt;
				&lt;/div&gt;
			&lt;/form&gt;
		&lt;/div&gt;
		&lt;div id="tab2_login" class="tab_content_login" style="display:none;"&gt;
			&lt;h3&gt;Register for this site!&lt;/h3&gt;
			&lt;p&gt;Sign up now for the good stuff.&lt;/p&gt;
			&lt;form method="post" action="&lt;?php echo site_url('wp-login.php?action=register', 'login_post') ?&gt;" class="wp-user-form"&gt;
				&lt;div class="username"&gt;
					&lt;label for="user_login"&gt;&lt;?php _e('Username'); ?&gt;: &lt;/label&gt;
					&lt;input type="text" name="user_login" value="&lt;?php echo esc_attr(stripslashes($user_login)); ?&gt;" size="20" id="user_login" tabindex="101" /&gt;
				&lt;/div&gt;
				&lt;div class="password"&gt;
					&lt;label for="user_email"&gt;&lt;?php _e('Your Email'); ?&gt;: &lt;/label&gt;
					&lt;input type="text" name="user_email" value="&lt;?php echo esc_attr(stripslashes($user_email)); ?&gt;" size="25" id="user_email" tabindex="102" /&gt;
				&lt;/div&gt;
				&lt;div class="login_fields"&gt;
					&lt;?php do_action('register_form'); ?&gt;
					&lt;input type="submit" name="user-submit" value="&lt;?php _e('Sign up!'); ?&gt;" class="user-submit" tabindex="103" /&gt;
					&lt;?php $register = $_GET['register']; if($register == true) { echo '&lt;p&gt;Check your email for the password!&lt;/p&gt;'; } ?&gt;
					&lt;input type="hidden" name="redirect_to" value="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;?register=true" /&gt;
					&lt;input type="hidden" name="user-cookie" value="1" /&gt;
				&lt;/div&gt;
			&lt;/form&gt;
		&lt;/div&gt;
		&lt;div id="tab3_login" class="tab_content_login" style="display:none;"&gt;
			&lt;h3&gt;Lose something?&lt;/h3&gt;
			&lt;p&gt;Enter your username or email to reset your password.&lt;/p&gt;
			&lt;form method="post" action="&lt;?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?&gt;" class="wp-user-form"&gt;
				&lt;div class="username"&gt;
					&lt;label for="user_login" class="hide"&gt;&lt;?php _e('Username or Email'); ?&gt;: &lt;/label&gt;
					&lt;input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /&gt;
				&lt;/div&gt;
				&lt;div class="login_fields"&gt;
					&lt;?php do_action('login_form', 'resetpass'); ?&gt;
					&lt;input type="submit" name="user-submit" value="&lt;?php _e('Reset my password'); ?&gt;" class="user-submit" tabindex="1002" /&gt;
					&lt;?php $reset = $_GET['reset']; if($reset == true) { echo '&lt;p&gt;A message will be sent to your email address.&lt;/p&gt;'; } ?&gt;
					&lt;input type="hidden" name="redirect_to" value="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;?reset=true" /&gt;
					&lt;input type="hidden" name="user-cookie" value="1" /&gt;
				&lt;/div&gt;
			&lt;/form&gt;
		&lt;/div&gt;
	&lt;/div&gt;

	&lt;?php } else { // is logged in ?&gt;

	&lt;div class="sidebox"&gt;
		&lt;h3&gt;Welcome, &lt;?php echo $user_identity; ?&gt;&lt;/h3&gt;
		&lt;div class="usericon"&gt;
			&lt;?php global $userdata; get_currentuserinfo(); echo get_avatar($userdata-&gt;ID, 60); ?&gt;

		&lt;/div&gt;
		&lt;div class="userinfo"&gt;
			&lt;p&gt;You&amp;rsquo;re logged in as &lt;strong&gt;&lt;?php echo $user_identity; ?&gt;&lt;/strong&gt;&lt;/p&gt;
			&lt;p&gt;
				&lt;a href="&lt;?php echo wp_logout_url('index.php'); ?&gt;"&gt;Log out&lt;/a&gt; | 
				&lt;?php if (current_user_can('manage_options')) { 
					echo '&lt;a href="' . admin_url() . '"&gt;' . __('Admin') . '&lt;/a&gt;'; } else { 
					echo '&lt;a href="' . admin_url() . 'profile.php"&gt;' . __('Profile') . '&lt;/a&gt;'; } ?&gt;

			&lt;/p&gt;
		&lt;/div&gt;
	&lt;/div&gt;

	&lt;?php } ?&gt;

&lt;/div&gt;
</code></pre>
<p>Okay, so here are the functional highlights for this hefty chunk of code:</p>
<ul>
<li>Everything is wrapped with <code>&lt;div id="login-register-password"&gt;&lt;/div&gt;</code></li>
<li>If the user is <em>not</em> logged in, the three forms are <em>included</em> in the markup</li>
<li>If the user <em>is</em> logged in, a simple welcome message is displayed</li>
<li>Success messages are displayed after both password recovery and registration</li>
<li>Each form submission sets a generic <code>user-cookie</code></li>
<li>After login or registration, the script redirects the user to the same page</li>
<li>Only one form is shown at a time; JavaScript is used to show and hide forms</li>
</ul>
<p>So if you just throw this thing into your <code>sidebar.php</code> file, you&rsquo;ll see the login form and three links: login, register, and recover-password. The other two forms are included in the markup, but are hidden with <abbr title="Cascading Style Sheets">CSS</abbr> (<code>display:none;</code>). As-is, the three links won&rsquo;t do anything because they require JavaScript to work. Once we sprinkle in some jQuery, the links will toggle the three different forms.</p>
<h3 id="demo">Implement and Demo</h3>
<p>First let&rsquo;s walk through using this code in your theme, and then we&rsquo;ll check out a demo.</p>
<ol>
<li>Place the <a href="http://digwp.com/examples/CustomLoginCode/custom-login-02.txt" title="Custom Login/Register/Password Code">custom login code</a> in your <code>sidebar.php</code> file, or some other location</li>
<li>Grab the <a href="http://digwp.com/examples/CustomLoginCode/custom-login-01.txt" title="jQuery Code">jQuery code</a> (no-conflict mode) and include it in your <code>footer.php</code> file</li>
<li>Include the <a href="http://digwp.com/examples/CustomLoginCode/custom-login-03.txt" title="CSS Code">CSS code</a> in your theme&rsquo;s <code>style.css</code> file, or wherever your styles are located</li>
</ol>
<p>..and done. Once these three items are in place, upload everything to your server and check it out. Here is a <strong><a href="http://digwp.com/custom-login-demo/" title="Custom Login Demo">demo</a></strong> showing this code used on a custom page <em>within</em> the loop. </p>
<p>Note that we have registration disabled here at <a href="http://digwp.com/" title="Digging into WordPress">DigWP.com</a>, so the forms won&rsquo;t be of much use other than to show how the tabs work and how the forms are styled. Here are some screenshots showing the &ldquo;success&rdquo; messages, and also the &ldquo;logged-in&rdquo; display.</p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-login-02.gif" alt="[ Screenshot: Registration Success Message ]" /><br /><small>Message displayed on successful registration</small></p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-login-03.gif" alt="[ Screenshot: Password-Recovery Success Message ]" /><br /><small>Message displayed on successful password-recovery</small></p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-login-04.gif" alt="[ Screenshot: Logged-In View ]" /><br /><small>Content displayed when user is logged into the site</small></p>
<p>Here again is the <a href="http://digwp.com/custom-login-demo/" title="Custom Login Demo">demo</a> and here are the three resource files:</p>
<ul>
<li><a href="http://digwp.com/examples/CustomLoginCode/custom-login-01.txt" title="Custom Login/Register/Password Code">Custom Template Code</a></li>
<li><a href="http://digwp.com/examples/CustomLoginCode/custom-login-02.txt" title="jQuery Code">jQuery Code</a></li>
<li><a href="http://digwp.com/examples/CustomLoginCode/custom-login-03.txt" title="CSS Code">CSS Code</a></li>
</ul>
<h3 id="custom">Customizing things</h3>
<p>One of the main reasons why we like working with actual code instead of widgets or plugins is the ability to easily customize anything we want exactly how we want it. With this implementation, there are basically three things to customize: the jQuery, the custom login code, and the <abbr title="Cascading Style Sheets">CSS</abbr>. As with any code, there are countless ways to modify appearance and functionality, so hopefully you have something specific in mind, and are familiar enough with design to jump in and customize things. If not, no worries &ndash; here are some ideas to help get you started.</p>
<h4>Customizing the login forms</h4>
<p>As-is, the custom login forms redirect to the current page. To get any/all of the forms to redirect to a different page, locate and edit the following line of code:</p>
<pre><code>&lt;input type="hidden" name="redirect_to" value="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;" /&gt;</code></pre>
<p>There are three instances of this hidden input field, which WordPress uses to perform the redirect. The <code>value</code> returns the current <abbr title="Uniform Resource Locator">URL</abbr>, so that&rsquo;s what needs changed for each form to redirect elsewhere.</p>
<p>Another useful modification involves customizing what the logged-in users see. Showing the gravatar and username is kind of neat, but there are tons of cool tricks to help ensure smooth user experience.</p>
<h4>Customizing the jQuery</h4>
<p>The jQuery used to show/hide the different login panels is actually pretty basic and is only used for toggling display states. I suppose there is a way to customize this, but it already handles additional menu items, so maybe you want to change the class names or optimize the script or something.</p>
<p>I do love to look at a nice slice of well-formatted jQuery, however, so I&rsquo;ll further indulge myself by including the entire code snippet right here:</p>
<pre><code>&lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
	$(document).ready(function() {
		$(".tab_content_login").hide();
		$("ul.tabs_login li:first").addClass("active_login").show();
		$(".tab_content_login:first").show();
		$("ul.tabs_login li").click(function() {
			$("ul.tabs_login li").removeClass("active_login");
			$(this).addClass("active_login");
			$(".tab_content_login").hide();
			var activeTab = $(this).find("a").attr("href");
			if ($.browser.msie) {$(activeTab).show();}
			else {$(activeTab).show();}
			return false;
		});
	});
&lt;/script&gt;</code></pre>
<p>A bit heavy-handed perhaps, but works great with no editing required ;)</p>
<h4>Customizing the CSS</h4>
<p>To get that fancy tabbed form menu looking all clean and rounded, <em>much</em> <abbr title="Cascading Style Sheets">CSS</abbr> is used. So instead of posting endless gobs of <abbr title="Cascading Style Sheets">CSS</abbr>, here is the <a href="http://digwp.com/examples/CustomLoginCode/custom-login-03.txt" title="CSS Code">code in plain text</a>. As with any <abbr title="Cascading Style Sheets">CSS</abbr>, the best way to customize things is to open Firebug and start tweaking.</p>
<h4>Just the links</h4>
<p>One last trick: use this code to display <em>links</em> to the default WordPress login/registration page:</p>
<pre><code>&lt;ul&gt;
	&lt;?php wp_register(); ?&gt;
	&lt;li&gt;&lt;?php wp_loginout(); ?&gt;&lt;/li&gt;
	&lt;?php wp_meta(); ?&gt;
&lt;/ul&gt;</code></pre>
<p>Nothing to golf about, but I figured it was worth mentioning.</p>
<h3 id="wrap">Wrap up</h3>
<p>Using the code and techniques in this article, you can provide your readers with a login form anywhere on your site. This makes it easy for your users and visitors to login/logout, register for your site, and recover passwords without leaving their current page. The login code works great as-is or is easily transformed into a snazzily tabbed login menu using a sprinkle of <abbr title="Cascading Style Sheets">CSS</abbr> and a dash of jQuery.</p>
<p>Finally, one of the great things about WordPress is that there is always more than one way to set things up. So if you see a way to improve the code, please share with the community by leaving a comment. Thank you!</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/12/login-register-password-code/">Permalink</a> | <a href="http://digwp.com/2010/12/login-register-password-code/#comments">60 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/12/login-register-password-code/&title=Custom Login/Register/Password Code">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/admin/" rel="tag">Admin</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/12/login-register-password-code/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
		<item>
		<title>ALL AJAX Theme Update</title>
		<link>http://digwp.com/2010/11/all-ajax-theme-update/</link>
		<comments>http://digwp.com/2010/11/all-ajax-theme-update/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 12:18:24 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[book]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=3182</guid>
		<description><![CDATA[One of the themes that is an exclusive download to all you good-looking people that purchased The Book is the ALL AJAX theme. The idea behind it is that the page never* reloads. Whenever an &#8220;internal&#8221; link is clicked, the main content area replaces itself with content that is fetched via Ajax. It always worked, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the themes that is an exclusive download to all you good-looking people that purchased <a href="http://digwp.com/book/"><strong>The Book</strong></a> is the ALL AJAX theme. The idea behind it is that the page never* reloads. Whenever an &#8220;internal&#8221; link is clicked, the main content area replaces itself with content that is fetched via Ajax. </p>
<p>It always worked, but it has undergone an update to be much mo&#8217; better. Shout out to Stewart Heckenberg for contributing to this, this new version is a combo effort of his work and mine. Here is what is now better:</p>
<p><span id="more-3182"></span></p>
<ul>
<li>Scripts properly loaded in the footer</li>
<li>Back button works, which users will expect it to</li>
<li>Hash-tag URL structure enforced when entering via a &#8220;deep link&#8221;</li>
<li>Detection of internal links smarter</li>
</ul>
<p>It may not seem like much, but it kinda is. Most importantly, it&#8217;s just a better user experience all around. Please note the site works perfectly fine with JavaScript disabled as well.</p>
<p><a class="button" href="http://themeclubhouse.digwp.com/index.php?wptheme=All%20AJAX">View Demo</a></p>
<h3>Why?</h3>
<p>The idea is to give a simple framework in case you wanted to do this same kind of thing in your own custom WordPress theme. I feel like this is a fairly easy theme to start from, with it&#8217;s Ã¼ber basic design. The original idea was based on a client request where there was a music player in the sidebar. They wanted it so a user could play a song in the music player, and have it not stop as they clicked around other links. </p>
<h3>How to get it</h3>
<p>We have a brand new downloading system here for the book and it&#8217;s related materials. Everyone who has previously purchased the book has access to it. <a href="http://digwp.com/2010/11/new-updates-downloads-system/">Read the instructions here</a>.</p>
<p>&nbsp;</p>
<hr />
<p><small>*The page still reloads for the submission of a comment.</small></p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/11/all-ajax-theme-update/">Permalink</a> | <a href="http://digwp.com/2010/11/all-ajax-theme-update/#comments">19 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/11/all-ajax-theme-update/&title=ALL AJAX Theme Update">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/ajax/" rel="tag">ajax</a>, <a href="http://digwp.com/tag/book/" rel="tag">book</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/11/all-ajax-theme-update/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Using Google Custom Search in WordPress</title>
		<link>http://digwp.com/2010/10/google-custom-search-in-wordpress/</link>
		<comments>http://digwp.com/2010/10/google-custom-search-in-wordpress/#comments</comments>
		<pubDate>Sat, 02 Oct 2010 17:03:02 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2342</guid>
		<description><![CDATA[Once a WordPress powered site starts getting quite a bit of content, the default built-in search becomes fairly useless. It just isn&#8217;t very smart. If you wrote a comprehensive article about He-Man, but since have written five other articles that just mentioned He-Man in passing, a search for &#8220;He-Man&#8221; will turn up your comprehensive article [...]]]></description>
			<content:encoded><![CDATA[<p>Once a WordPress powered site starts getting quite a bit of content, the default built-in search becomes fairly useless. It just isn&#8217;t very smart. If you wrote a comprehensive article about He-Man, but since have written five other articles that just mentioned He-Man in passing, a search for &#8220;He-Man&#8221; will turn up your comprehensive article sixth. There have been various tweaks and plugins and whatnot to try and make this better. But why not leverage the best search engine ever written instead? </p>
<p><span id="more-2342"></span></p>
<h3>The Basic Setup</h3>
<p>Sign up for the (free) <a href="http://www.google.com/cse/">Google Custom Search Engine</a>. </p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/gcsesignup.png" alt="" title="gcsesignup" width="511" height="383" class="alignnone size-full wp-image-2973" />
</div>
<p>The default code that Google gives you is the pure-JavaScript version. It works fine, but it doesn&#8217;t allow you to separate the search form and the results. I find that kind of ridiculous, as it&#8217;s far more common to want to host the search form on all pages (like in the header or sidebar) and have that take you to a dedicated results page.</p>
<p>To get the code that is the <strong>separate</strong> search form and results, go to the Look and Feel section after creating your CSE. Choose the iframe method, then click the Get Code button. </p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/iframecode.png" alt="" title="iframecode" width="442" height="412" class="alignnone size-full wp-image-2974" /></p>
<p><img src="http://digwp.com/wp-content/uploads/separatecode.png" alt="" title="separatecode" width="550" height="603" class="alignnone size-full wp-image-2975" />
</div>
<p>Fill out the URL of your search results page (this will be a page you&#8217;ll create and publish later). Then take the top bit of code and place it were you want the search form to be. Likely in your sidebar.php file or header.php file, wherever your site design dictates would be good to have a search form. </p>
<p>On this site, we put it in the sidebar, and applied our simple button class to match other buttons on the site. Don&#8217;t be afraid to alter the HTML of the provided code as long as it&#8217;s simple alterations like adding a class name to an input.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/digwpcse.png" alt="" title="digwpcse" width="318" height="77" class="alignnone size-full wp-image-2991" />
</div>
<p>Now create a page in your WordPress admin called &#8220;Search Results&#8221;, in the content, paste in the second bit of code. Make sure the slug you give the page matches the URL you gave the CSE for the location of the results page. If they don&#8217;t match, you can easily change the <code>&lt;form&gt;</code>&#8216;s action attribute in the HTML.</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/matchsearchresultsurl-1.png" alt="" title="matchsearchresultsurl-1" width="512" height="314" class="alignnone size-full wp-image-2977" />
</div>
<p>You&#8217;re all set! Now you can enter search terms on any page of your site, and be taken to a dedicated search results page where the results will be displayed. These results will of course be real Google search results, but limited only to your domain. So that He-Man article? It will almost certainly be on top, because Google&#8217;s importance algorithmic will put it there. </p>
<h3>Using Multiple CSE&#8217;s for Subsections of your Site</h3>
<p>On CSS-Tricks, I have a search area that looks like this:</p>
<div class="image-wrap">
<img src="http://digwp.com/wp-content/uploads/csstrickssearch.png" alt="" title="csstrickssearch" width="384" height="98" class="alignnone size-full wp-image-2992" />
</div>
<p>Notice the little options beneath the search box which indicate in what area you would like to search. It might seem like this is some fancy custom development, but it&#8217;s really not. In reality, it&#8217;s just four different Google Custom Search Engines. Only one of them displays at a time though, depending on which search option is active.</p>
<pre><code>&lt;div id="search-area"&gt;

	&lt;form action="/search-results/" id="search-all" class="header-search-form default-search"&gt;
	    ...
	&lt;/form&gt;

	&lt;form method="get" action="/" id="search-articles" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;form action="/forums/index.php" id="search-forums" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;form action="/snippets/search/" id="search-snippets" class="header-search-form"&gt;
	    ...
	&lt;/form&gt;

	&lt;div id="search-by"&gt;Search: 
		&lt;a href="#search-all" class="cur-search"&gt;All&lt;/a&gt; 
		&lt;a href="#search-articles"&gt;Articles&lt;/a&gt; 
		&lt;a href="#search-forums"&gt;Forums&lt;/a&gt; 
		&lt;a href="#search-snippets"&gt;Snippets&lt;/a&gt;

		&lt;p id="browse-archives"&gt;Or... &lt;a href="/archives/"&gt;browse the archives&lt;/a&gt;&lt;/p&gt;
	&lt;/div&gt;

&lt;/div&gt; &lt;!-- END search-area --&gt;</code></pre>
<p>Notice the class names on the very first <code>&lt;form&gt;</code> includes &#8220;default-search&#8221;. All these forms are hidden by default, and only that class name reveals it. So while there are four forms here, only one displays. Also note the second form actually submits to &#8220;/&#8221; and uses the GET method. This is for &#8220;articles only&#8221; search and uses the default WordPress search. </p>
<p>The other areas (Snippets, Forums, and All) use Google Custom Search Engines. So for those three, I used what is described above in the basic setup. For the Snippets area, I told the CSE to use the domain http://css-tricks.com/snippets/ &#8211; so the results that it returns are limited to that subdirectory.</p>
<p>I use a bit of jQuery to allow the search option links to be clickable to change which area is to be searched. When a new option is selected, hide all forms, show the correct corresponding form, flip around classes to indicate the new option is selected. Easy cheesy.</p>
<pre><code>var $searchByLinks = $("#search-by &gt; a");

$searchByLinks.click(function() {
    var $el = $(this)
    $(".header-search-form").hide();
    $($el.attr("href")).show();
    $searchByLinks.removeClass("cur-search");
    $el.addClass("cur-search");
    return false;
});</code></pre>
<p>Then to keep what users type in all search inputs in sync at all times, another quick bit:</p>
<pre><code>$(".header-search-input").keyup(function() {
    $(".header-search-input").val($(this).val());
});</code></pre>
<p>Now we&#8217;re all set! Users can now search subsections of the site if they choose, and all we had to do was use multiple Google CSE&#8217;s. The user likely doesn&#8217;t even notice and no custom development was needed.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/10/google-custom-search-in-wordpress/">Permalink</a> | <a href="http://digwp.com/2010/10/google-custom-search-in-wordpress/#comments">20 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/10/google-custom-search-in-wordpress/&title=Using Google Custom Search in WordPress">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/google/" rel="tag">google</a>, <a href="http://digwp.com/tag/search/" rel="tag">search</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/10/google-custom-search-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>WordPress 3 Template Hierarchy</title>
		<link>http://digwp.com/2010/09/wordpress-3-template-hierarchy/</link>
		<comments>http://digwp.com/2010/09/wordpress-3-template-hierarchy/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 02:36:42 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[chart]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2887</guid>
		<description><![CDATA[This chart is one entire page in our book, but I thought it would be good to focus on specifically. Template hierarchy has gotten a bit more advanced since the last time we covered it. The idea is that WordPress will look for files in a theme to use to render the current page in [...]]]></description>
			<content:encoded><![CDATA[<p>This chart is one entire page in <a href="http://digwp.com/book/">our book</a>, but I thought it would be good to focus on specifically. Template hierarchy has gotten a bit more advanced since the <a href="http://digwp.com/2010/02/template-heirarchy-chart/">last time we covered it</a>. </p>
<p>The idea is that WordPress will look for files in a theme to use to render the current page in a specific order. For example, let say you have a page for showing posts from a certain category <a href="http://digwp.com/category/security/">like this</a>. On this site, all our category pages are currently the same and use &#8220;archive.php&#8221; template. This file is pretty far down the hierarchy, and is shared with other types of views, for example, tag pages.</p>
<p><span id="more-2887"></span></p>
<p>If we wanted to differentiate our category pages from our tag pages, we could create a file in our theme called &#8220;category.php&#8221; and our site would instantly start using that template for these pages, without us having to do anything else. We could get even more specific, have theme files that are unique to the categories themselves! To make a page specific to our security category, we could make a file called &#8220;category-security.php&#8221; and our site would use that file when viewing that category page. </p>
<p>You could really get fancy with customized pages by having a good understanding of the template hierarchy! Do take care not to get too overly repetitive with your templates though. If you have 10 categories and want to have a different header on each, you may be better off using a single category.php file with <a href="http://codex.wordpress.org/Conditional_Tags">conditional tags</a> rather than 10 separate files.</p>
<p><img src="http://digwp.com/wp-content/uploads/which-template-590.png" alt="" title="which-template-590" width="590" height="478" class="alignnone size-full wp-image-2892" /></p>
<p><a href="http://digwp.com/wp-content/uploads/which-template-fullsize.png">SEE THE FULL SIZE IMAGE</a></p>
<p>This was based on this hierarchy list that was sent to me by <a href="http://ptahdunbar.com/">Ptah Dunbar</a>. Thanks Ptah! </p>
<ul>
<li>404
<ol>
<li>404.php</li>
<li>index.php</li>
</ol>
</li>
<li>Search
<ol>
<li>search.php</li>
<li>index.php</li>
</ol>
</li>
<li>Tax
<ol>
<li>taxonomy-{tax}-{term}.php</li>
<li>taxonomy-{tax}.php</li>
<li>taxonomy.php</li>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
<li>Home
<ol>
<li>home.php</li>
<li>index.php</li>
</ol>
</li>
<li>Attachment
<ol>
<li>mime.php</li>
<li>type.php</li>
<li>mime_type.php</li>
<li>attachment.php</li>
<li>single.php</li>
<li>index.php</li>
</ol>
</li>
<li>Single
<ol>
<li>single-{post-type}.php</li>
<li>single.php</li>
<li>index.php</li>
</ol>
</li>
<li>Page
<ol>
<li>custom_template.php</li>
<li>page-{slug}.php</li>
<li>page-{id}.php</li>
<li>page.php</li>
<li>index.php</li>
</ol>
</li>
<li>Category
<ol>
<li>category-{slug}.php</li>
<li>category-{id}.php</li>
<li>category.php</li>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
<li>Tag
<ol>
<li>tag-{slug}.php</li>
<li>tag-{id}.php</li>
<li>tag.php</li>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
<li>Author
<ol>
<li>author-{author-nicename}.php</li>
<li>author-{author-id}.php</li>
<li>author.php</li>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
<li>Date
<ol>
<li>date.php</li>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
<li>Archive
<ol>
<li>archive.php</li>
<li>index.php</li>
</ol>
</li>
</ul>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/09/wordpress-3-template-hierarchy/">Permalink</a> | <a href="http://digwp.com/2010/09/wordpress-3-template-hierarchy/#comments">20 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/09/wordpress-3-template-hierarchy/&title=WordPress 3 Template Hierarchy">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/chart/" rel="tag">chart</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/09/wordpress-3-template-hierarchy/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>The &#8220;Frameworks&#8221; Discussion</title>
		<link>http://digwp.com/2010/07/the-frameworks-discussion/</link>
		<comments>http://digwp.com/2010/07/the-frameworks-discussion/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 09:20:12 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[Theme]]></category>
		<category><![CDATA[discussion]]></category>
		<category><![CDATA[frameworks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2340</guid>
		<description><![CDATA[I&#8217;ve never been a big fan of &#8220;theme frameworks.&#8221; I quite like hacking up WordPress myself and making it do the things I want it to do. I feel like most theme frameworks have a ton of custom functions for you to &#8220;help&#8221; in doing that kind of stuff. For example, adding a block of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve never been a big fan of &#8220;theme frameworks.&#8221; I quite like hacking up WordPress myself and making it do the things I want it to do. I feel like most theme frameworks have a ton of custom functions for you to &#8220;help&#8221; in doing that kind of stuff. For example, adding a block of text to the sidebar, adjusting the layout, or building a custom menu. </p>
<p><span id="more-2340"></span></p>
<p>So because frameworks have all these helper functions, they are targeted at people who don&#8217;t want to learn how to do all that stuff themselves, but still harness all that power. In other words, non-nerds. These helper functions are used by hand-altering theme files, which is inherently nerdy. So frameworks are for non-nerds, but the only people that know about them (and especially how to use them), are nerds.</p>
<p>Or am I full of crap?</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/07/the-frameworks-discussion/">Permalink</a> | <a href="http://digwp.com/2010/07/the-frameworks-discussion/#comments">66 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/07/the-frameworks-discussion/&title=The &#8220;Frameworks&#8221; Discussion">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/discussion/" rel="tag">discussion</a>, <a href="http://digwp.com/tag/frameworks/" rel="tag">frameworks</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/07/the-frameworks-discussion/feed/</wfw:commentRss>
		<slash:comments>66</slash:comments>
		</item>
		<item>
		<title>WordPress Custom functions.php Template, Part 2</title>
		<link>http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/</link>
		<comments>http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 16:13:36 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1811</guid>
		<description><![CDATA[In a recent post, we show you how to clean up and enhance the functionality of WordPress with a custom functions.php template. In that post, we explain how using a custom functions.php template can speed up development while optimizing many key aspects of WordPress. In this post, we deliver another prime collection of 15 custom [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent post, we show you how to clean up and enhance the functionality of WordPress with a <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/" title="WordPress functions.php Template with 15 Essential Custom Functions">custom functions.php template</a>. In <em>that</em> post, we explain how using a custom <code>functions.php</code> template can speed up development while optimizing many key aspects of WordPress. In <em>this</em> post, we deliver another prime collection of <strong>15 custom functions to enhance your WordPress site</strong>. These functions provide all sorts of useful functionality, including stuff like:</p>
<ul>
<li>Callback function for a custom comments loop</li>
<li>Automatic content insertion in posts and feeds</li>
<li>Spam and delete links for comments when logged in</li>
<li>Buffer period before new posts are added to your feeds</li>
<li>Including an Admin link to the &ldquo;All-Settings&rdquo; page</li>
<li>Removing the version and generator information from your site and feed</li>
</ul>
<p>These new functions <em>extend</em> the functionality of our original <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/" title="WordPress functions.php Template with 15 Essential Custom Functions">functions.php template</a> with <strong>15 more useful functions</strong>. Not everything presented here is going to be necessary for <em>all</em> of your themes, so just grab what you need and add it your own custom <code>functions.php</code> file.</p>
<p><span id="more-1811"></span></p>
<blockquote><p>In this <acronym title="Digging into WordPress">DiW</acronym> article, we extend our original functions.php template with 15 more useful functions.</p></blockquote>
<p>As before, we&rsquo;ll first walk through each of the functions and then unify them into a working <code>functions.php</code> template. To use, just copy and paste the template code at the end of this article or <a href="#functions-download" title="Download the zipped template file">grab a copy of the zipped functions.php file</a> and enjoy a custom collection of functions that will help you <strong>optimize your development process</strong> while enhancing WordPress with some <strong>awesome new functionality</strong>.</p>
<h3>Insert custom content after each post</h3>
<p>If you look at the different things contained within a typical post, you will find many common and repetitive items, such as feed links, copyright information, and <a href="http://perishablepress.com/press/2008/11/23/fully-valid-seo-friendly-social-media-links-for-wordpress/" title="Fully Valid, SEO-Friendly Social Media Links for WordPress">social-media bookmarks</a>. While there&rsquo;s nothing wrong with inserting this content into your <code>single.php</code> template, it is sometimes easier to manage things from the <code>functions.php</code> file. For example, at <a href="http://perishablepress.com/" title="Digital Design and Dialogue">Perishable Press</a>, I include a brief copyright statement at the end of each post. By including the following code in my <code>functions.php</code> template, it&rsquo;s something that happens automatically:</p>
<pre><code>// add custom post content
function add_post_content($content) {
	if(!is_feed() &amp;&amp; !is_home()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_content', 'add_post_content');</code></pre>
<p>The trick here is an old one, but it&rsquo;s extremely useful. By changing the line beginning with &ldquo;<code>$content.</code>&rdquo;, you can add just about any content you like.</p>
<h3>Insert custom content in your feeds</h3>
<p>Just as with the previous method, this function makes it possible to automatically add any content to your feeds. Yes, I know there are plugins that will <a href="http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/" title="Wordpress Plugin : Better Feed">make your feed footers do backflips</a>, but for a simple copyright message or other info, I think it is easier and more efficient to simply toss a few lines into your custom <code>functions.php</code> template:</p>
<pre><code>// add custom feed content
function add_feed_content($content) {
	if(is_feed()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');</code></pre>
<p>As before, you can change the added content to whatever you want by editing the &ldquo;<code>$content</code>&rdquo; variable. Once in place, this will append a dynamic copyright message to each post in your feed. Note: if you happen to be adding the same content to your web pages (using the previous method) <em>and</em> your feeds (using this method), you may combine the two functions like so:</p>
<pre><code>// add custom content to feeds and posts
function add_custom_content($content) {
	if(!is_home()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_excerpt_rss', 'add_custom_content');
add_filter('the_content', 'add_custom_content');</code></pre>
<p>Remember that if you use this combined function that you should remove or comment out both of the individual ones to avoid duplicate output. It will be included but commented out in the complete <code>functions.php</code> template file.</p>
<h3>Completely remove the version number from pages and feeds</h3>
<p>A commonly cited <a href="http://digwp.com/2009/07/remove-wordpress-version-number/" title="How to Remove the WordPress Version Number (The Right Way)">security measure for WordPress-powered sites</a> involves removing the automatically generated version number from appearing in the <code>&lt;head&gt;</code> section of your pages&rsquo; source code. This type of security technique is referred to as &ldquo;security through obscurity,&rdquo; and aims at hiding potentially sensitive information from a would-be attacker. Here&rsquo;s a function that does the job:</p>
<pre><code>// remove version info from head and feeds
function complete_version_removal() {
	return '';
}
add_filter('the_generator', 'complete_version_removal');</code></pre>
<p>This will remove your site&rsquo;s version and generator information from all of your pages and feeds, making it a little bit harder for the bad guys and a little bit safer for you and your visitors.</p>
<h3>Customize the admin footer message</h3>
<p>Customization is what makes your online experience something special. For your own sites and for your clients&rsquo; sites, it is nice to customize the generic admin footer message with something a little more useful, informative, and inspiring. This little gem makes it easy:</p>
<pre><code>// customize admin footer text
function custom_admin_footer() {
	echo '&lt;a href="http://monzilla.biz/"&gt;Website Design by Monzilla Media&lt;/a&gt;';
} 
add_filter('admin_footer_text', 'custom_admin_footer');</code></pre>
<p>Here, for the sake of example, we are including a link to my web-design company, but you can customize the content with just about anything you wish. If this were something I had to do manually for each site, I probably wouldn&rsquo;t do it. But by adding these few lines to my <code>functions.php</code> template, it all happens automatically, without me having to think about it. Nice&nbsp;;)</p>
<h3>Enable HTML markup in user profiles</h3>
<p>When customizing their profiles, users may want to include a hyperlink, bold text, or some other <acronym title="Hypertext markup language">HTML</acronym> markup. By default, WordPress prevents this from happening, but you can easily enable it with this friendly little snippet:</p>
<pre><code>// enable html markup in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');</code></pre>
<p><strong>Note:</strong> as <a href="#comment-4171" title="Jump to comment">miqrogroove</a> points out, enabling <acronym title="Hypertext markup language">HTML</acronym> may be best left to sites that have open user-registration disabled. Use with discretion.</p>
<h3>Delay feed update after posting</h3>
<p>As a chronic perfectionist, I hate it when I post something only to discover an error a few minutes later. By the time I notice, fix and update the post, it&rsquo;s already been beamed out all over the freakin&rsquo; place. To prevent this, I like to take the advice of <a href="http://wpengineer.com/publish-the-feed-later/" title="Publish The Feed Later">WPEngineer</a> and give myself a little buffer period or &ldquo;safety net&rdquo; after publishing my posts.</p>
<pre><code>// delay feed update
function publish_later_on_feed($where) {
	global $wpdb;

	if (is_feed()) {
		// timestamp in WP-format
		$now = gmdate('Y-m-d H:i:s');

		// value for wait; + device
		$wait = '5'; // integer

		// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
		$device = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

		// add SQL-sytax to default $where
		$where .= " AND TIMESTAMPDIFF($device, $wpdb-&gt;posts.post_date_gmt, '$now') &gt; $wait ";
	}
	return $where;
}
add_filter('posts_where', 'publish_later_on_feed');</code></pre>
<p>This code quietly and automatically gives me an extra five minutes before my post is added to my feeds. If you need more or less than five minutes, just edit the &ldquo;<code>$wait</code>&rdquo; variable with the integer of your choice.</p>
<h3>Add an Admin link to the WordPress &ldquo;All-Settings&rdquo; page</h3>
<p>As we&rsquo;ve explained before, WordPress makes it easy to <a href="http://digwp.com/2009/06/edit-your-database-options-from-the-wordpress-admin/" title="Edit Your Database Options from the WordPress Admin">view and edit your blog settings from the Admin area</a>. Thanks to a file named &ldquo;<code>options.php</code>&rdquo;, WordPress will display all of your database settings and enable you to edit a majority of them. To view this page, you need to log in as Admin and enter this <acronym title="Uniform Resource Locator">URL</acronym> in your browser&rsquo;s address bar:</p>
<p><code>http://domain.tld/wp-admin/options.php</code></p>
<p>Having access to your &ldquo;All-Settings&rdquo; page can be extremely helpful, so it&rsquo;s nice to display a direct link for easy access. The following slice of code in your <code>functions.php</code> file is all that&rsquo;s needed:</p>
<pre><code>// admin link for all settings
function all_settings_link() {
	add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');</code></pre>
<p>Once in place, you will see a link to your &ldquo;All Settings&rdquo; page in the WordPress Admin. You can change the name of the link to whatever you prefer by editing the two instances of &ldquo;All Settings&rdquo;.</p>
<h3>Remove all nofollow attributes from comments</h3>
<p>There are <a href="http://perishablepress.com/press/2007/09/23/much-ado-about-nofollow-the-perishable-press-dofollow-series/" title="Much ado about nofollow: The Perishable Press Dofollow Series">a million ways</a> to <a href="http://perishablepress.com/press/2007/09/05/comprehensive-reference-for-wordpress-no-nofollow-dofollow-plugins/" title="Comprehensive Reference for WordPress NoNofollow/Dofollow Plugins">remove nofollow</a> from your comments, but nothing is as <em>easy</em> as this method provided by code guru <a href="http://toscho.de/2009/no-no-no-nofollow/" title="No no no nofollow">Thomas Scholz</a>:</p>
<pre><code>// remove nofollow from comments
function xwp_dofollow($str) {
	$str = preg_replace(
		'~&lt;a ([^&gt;]*)\s*(["|\']{1}\w*)\s*nofollow([^&gt;]*)&gt;~U',
		'&lt;a ${1}${2}${3}&gt;', $str);
	return str_replace(array(' rel=""', " rel=''"), '', $str);
}
remove_filter('pre_comment_content',     'wp_rel_nofollow');
add_filter   ('get_comment_author_link', 'xwp_dofollow');
add_filter   ('post_comments_link',      'xwp_dofollow');
add_filter   ('comment_reply_link',      'xwp_dofollow');
add_filter   ('comment_text',            'xwp_dofollow');</code></pre>
<p>That code will remove all instances of the nefarious &ldquo;<code>nofollow</code>&rdquo; attribute from <em>all</em> comment items, including the author link and comment text. This is a great way to automatically remove nofollow with no plugins or hacking required.</p>
<h3>Enable easy display of your post&rsquo;s word count</h3>
<p>This function enables you to display your post&rsquo;s word count without cluttering up the WordPress loop. To display the word count of your posts, first place this in your <code>functions.php</code> file:</p>
<pre><code>// count words in posts
function word_count() {
	global $post;
	echo str_word_count($post-&gt;post_content);
}</code></pre>
<p>Then, simply place this anywhere in your loop where you would like the number to appear:</p>
<p><code>&lt;?php word_count(); ?&gt;</code></p>
<p>So for example, you could display your post&rsquo;s word count to your visitors with something like:</p>
<p><code>&lt;p&gt;This post contains a whopping &lt;?php word_count(); ?&gt; words.&lt;/p&gt;</code></p>
<p>We can also easily display the word count of other items, such as the post title:</p>
<p><code>&lt;?php echo str_word_count($post-&gt;post_title); ?&gt;</code></p>
<p>Likewise, we can exclude the function from <code>functions.php</code> and get the word count with a single line of code in the loop:</p>
<p><code>&lt;?php echo str_word_count($post-&gt;post_content); ?&gt;</code></p>
<p>Just slap &lsquo;em anywhere in your post loop to display your word count. Not always needed, but nice to have available&nbsp;;)</p>
<h3>Enable delete and spam links for comments</h3>
<p>Managing your comments is easily done via the Admin, but geeks like me also like to see things from the outside, as they appear to the public. Browsing through comments as they appear on your site helps you catch things that may have eluded routine approvals in the Admin&rsquo;s comment listings. To help facilitate this process, I like to include a <a href="http://perishablepress.com/press/2008/12/01/spam-delete-buttons-links-for-old-versions-wordpress/" title="Backwards-Compatible Spam and Delete Buttons for WordPress">backwards-compatible set of spam and delete links</a> next to each comment. This makes it super-easy to cultivate comments live on the site.</p>
<pre><code>// spam &amp; delete links for all versions of wordpress
function delete_comment_link($id) {
	if (current_user_can('edit_post')) {
		echo '| &lt;a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&amp;c='.$id.'"&gt;del&lt;/a&gt; ';
		echo '| &lt;a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&amp;dt=spam&amp;c='.$id.'"&gt;spam&lt;/a&gt;';
	}
}</code></pre>
<p>Once this is included in your <code>functions.php</code> file, displaying the links is as easy as adding this to your comments loop:</p>
<pre><code>&lt;?php delete_comment_link(get_comment_ID()); ?&gt;</code></pre>
<h3>Disable all WordPress feeds</h3>
<p>Thanks to <a href="http://wpengineer.com/disable-wordpress-feed/" title="How to Disable RSS Feeds in WordPress">Frank from WPEngineer</a>, it is easy to disable all feed functionality for your site. Here is the <code>functions.php</code> code:</p>
<p><strong>Note:</strong> Only use this function if you want to <em>disable</em> your feeds! This function will be commented out of the complete <code>functions.php</code> file.</p>
<pre><code>// disable all feeds
function fb_disable_feed() {
	wp_die(__('&lt;h1&gt;Feed not available, please visit our &lt;a href="'.get_bloginfo('url').'"&gt;Home Page&lt;/a&gt;!&lt;/h1&gt;'));
}
add_action('do_feed',      'fb_disable_feed', 1);
add_action('do_feed_rdf',  'fb_disable_feed', 1);
add_action('do_feed_rss',  'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);</code></pre>
<p>This function will quietly and completely disable all of your site&rsquo;s feeds, including all different formats. With this code in place, any request for your feed will return the following message in a nice, big set of <code>&lt;h1&gt;</code> tags:</p>
<blockquote><p>Feed not available, please visit our <a href="http://digwp.com/">Home Page</a>!</p></blockquote>
<p>This message is easily customized by editing the &ldquo;<code>wp_die</code>&rdquo; argument in the function.</p>
<h3>Customize the default gravatars and add your own</h3>
<p>Instead of suffering with the rather dull default gravatars, use this code to customize your own: </p>
<pre><code>// customize default gravatars
function custom_gravatars($avatar_defaults) {

	// change the default gravatar
	$customGravatar1 = get_bloginfo('template_directory').'/images/gravatar-01.png';
	$avatar_defaults[$customGravatar1] = 'Default';

	// add a custom user gravatar
	$customGravatar2 = get_bloginfo('template_directory').'/images/gravatar-02.png';
	$avatar_defaults[$customGravatar2] = 'Custom Gravatar';

	// add another custom gravatar
	$customGravatar3 = get_bloginfo('template_directory').'/images/gravatar-03.png';
	$avatar_defaults[$customGravatar3] = 'Custom gravatar';

	return $avatar_defaults;
}
add_filter('avatar_defaults', 'custom_gravatars');</code></pre>
<p>Of course, you can manually choose a default gravatar by specifying its location via the &ldquo;<code>get_avatar</code>&rdquo; template tag, but this method makes it <em>so</em> much easier. Not only does it replace the default with <em>multiple</em> custom gravatars, but it also goes the extra mile by displaying them in the WordPress Admin area under <strong>Settings &gt; Discussion</strong>. From there, you can select any of your custom gravatars by simply selecting it and clicking the &ldquo;Update&rdquo; button. Nothing could be easier. </p>
<p>A few notes about this function:</p>
<ul>
<li>As is, it adds three new gravatars to choose as defaults.</li>
<li>Each gravatar should be located in your theme&rsquo;s <code>images</code> directory.</li>
<li>You can add as many default gravatars as you would like by emulating the code pattern.</li>
<li>If you keep your custom gravatars someplace else, be sure and edit the image paths accordingly.</li>
</ul>
<h3>Disable automatic formatting in post content via shortcode</h3>
<p><a href="http://www.wprecipes.com/disable-wordpress-automatic-formatting-on-posts-using-a-shortcode" title="Disable WordPress automatic formatting on posts using a shortcode">WPRecipes</a> shows us how to prevent WordPress from automatically formatting chunks of post content by using a shortcode. This is very useful for posting code, poetry, or anything else that you would like to display in raw format. To do this, we add the following snippet to our <code>functions.php</code> file:</p>
<pre><code>// disable auto formatting in posts
function my_formatter($content) {
	$new_content = '';
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

	foreach ($pieces as $piece) {
		if (preg_match($pattern_contents, $piece, $matches)) {
			$new_content .= $matches[1];
		} else {
			$new_content .= wptexturize(wpautop($piece));
		}
	}

	return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);</code></pre>
<p>With that code in place, you can insert unformatted code into your posts via the &ldquo;<code>[raw]</code>&rdquo; shortcode, for example:</p>
<p><code>[raw]Unformatted content[/raw]</code></p>
<p>Anything contained within those shortcodes will be left unformatted when displayed in your posts.</p>
<h3>Escape HTML entities in comments</h3>
<p><a href="http://konstruktors.com/blog/wordpress/1850-automatically-escape-html-entities-of-code-fragments-in-comments/" title="Automatically Escape HTML Entities of Code Fragments in Comments">Kaspars Dambis</a> shows us how to automatically escape encoded <acronym title="Hypertext markup Language">HTML</acronym> entities in the comments that readers leave on your posts. This is especially useful for blogs that deal in heavy code exchanges on post threads. This enables your commentators to simply wrap their code in <code>&lt;code&gt;</code> tags and not have to worry about manually converting characters such as angled brackets (&ldquo;<code>&lt;</code>&rdquo; &amp; &ldquo;<code>&gt;</code>&rdquo;) into their encoded equivalents (&ldquo;<code>&amp;lt;</code>&rdquo; &amp; &ldquo;<code>&amp;gt;</code>&rdquo;).</p>
<pre><code>// escape html entities in comments
function encode_code_in_comment($source) {
	$encoded = preg_replace_callback('/&lt;code&gt;(.*?)&lt;\/code&gt;/ims',
	create_function('$matches', '$matches[1] = preg_replace(array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "", $matches[1]); 
	return "&lt;code&gt;" . htmlentities($matches[1]) . "&lt;/"."code&gt;";'), $source);
	if ($encoded)
		return $encoded;
	else
		return $source;
}
add_filter('pre_comment_content', 'encode_code_in_comment');</code></pre>
<p>Once in place in your <code>functions.php</code> file, simply tell your commentators to wrap their code snippets in <code>&lt;code&gt;</code> tags. In other words, any content wrapped in <code>&lt;code&gt;</code> tags will be encoded automatically. Plus, in order to prevent unwanted <code>&lt;br</code>&nbsp;<code>/&gt;</code> tags, this function also automatically removes line breaks after the opening <code>&lt;code&gt;</code> tag and before the closing <code>&lt;code&gt;</code> tag.</p>
<h3>Custom comments display callback function</h3>
<p>When designing your theme&rsquo;s <code>comments.php</code> file, you can use either WordPress&rsquo; default code or create your own for full control over your comments display. If you&rsquo;re rolling your own, it is often easiest to grab a solid comments-display template and customize it to suit your needs. After many iterations and much refining, here is the comments callback function that I usually begin with:</p>
<pre><code>// custom comments callback function
function custom_comments_callback($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?&gt;

	&lt;li &lt;?php comment_class(); ?&gt; id="comment-&lt;?php comment_ID(); ?&gt;"&gt;
		&lt;div class="comment-wrap"&gt;
			&lt;?php echo get_avatar(get_comment_author_email(), $size = '50', $default = bloginfo('stylesheet_directory').'/images/gravatar.png'); ?&gt;

			&lt;div class="comment-intro"&gt;
            			&lt;?php printf(__('%s'), get_comment_author_link()); ?&gt; &amp;ndash; &lt;a class="comment-permalink" href="&lt;?php echo htmlspecialchars(get_comment_link($comment-&gt;comment_ID)); ?&gt;"&gt;&lt;?php comment_date('F j, Y'); ?&gt; @ &lt;?php comment_time(); ?&gt;&lt;/a&gt;&lt;?php edit_comment_link('Edit', ' &amp;ndash; ', ''); ?&gt;
			&lt;/div&gt;
			&lt;?php if ($comment-&gt;comment_approved == '0') : ?&gt;

			&lt;p class="comment-moderation"&gt;&lt;?php _e('Your comment is awaiting moderation.'); ?&gt;&lt;/p&gt;
			&lt;?php endif; ?&gt;

			&lt;div class="comment-text"&gt;&lt;?php comment_text(); ?&gt;&lt;/div&gt;

			&lt;div class="reply" id="comment-reply-&lt;?php comment_ID(); ?&gt;"&gt;
				&lt;?php comment_reply_link(array_merge($args, array('reply_text'=&gt;'Reply', 'login_text'=&gt;'Log in to Reply', 'add_below'=&gt;'comment-reply', 'depth'=&gt;$depth, 'max_depth'=&gt;$args['max_depth']))); ?&gt; 

			&lt;/div&gt;
		&lt;/div&gt;

&lt;?php } // WP adds the closing &lt;/li&gt;</code></pre>
<p>There&rsquo;s a lot going on here, so let&rsquo;s highlight the important stuff:</p>
<ul>
<li><strong>Do not</strong> add the closing <code>&lt;/li&gt;</code> element &ndash; <a href="http://digwp.com/2010/02/custom-comments-html-output/" title="Custom Comments HTML Output">WordPress does this for you</a>.</li>
<li>This function calls for a default gravatar in your theme&rsquo;s &ldquo;<code>images</code>&rdquo; directory. Either ensure you&rsquo;ve got the required image in place or edit the code to reflect your site&rsquo;s reality.</li>
<li>Once this function is included in your <code>functions.php</code> file, you can display it by using the following comments loop in your <code>comments.php</code> file:</li>
</ul>
<pre><code>&lt;?php if (have_comments()) : ?&gt;
&lt;ol class="commentlist"&gt;
	&lt;?php wp_list_comments('type=comment&amp;style=ol&amp;callback=custom_comments_callback'); ?&gt;
&lt;/ol&gt;
&lt;?php else : ?&gt;</code></pre>
<p>Notice the third parameter for the <code>wp_list_comments</code> template tag? that&rsquo;s what makes it go. Also important is the &ldquo;<code>style=ol</code>&rdquo; parameter, which tells WordPress that we are using an ordered list for comments and that it should automatically close the markup with a <code>&lt;/li&gt;</code> element.</p>
<h3>Putting it all together..</h3>
<p>As promised, here is the full-meal deal &ndash; the entire collection neatly organized into a single chunk of code:</p>
<pre><code>&lt;?php // custom functions.php template @ digwp.com

// add custom post content
function add_post_content($content) {
	if(!is_feed() &amp;&amp; !is_home()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_content', 'add_post_content');


// add custom feed content
function add_feed_content($content) {
	if(is_feed()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');


/* add custom content to feeds and posts
function add_custom_content($content) {
	if(!is_home()) {
		$content .= '&lt;p&gt;This article is copyright &amp;copy; '.date('Y').'&amp;nbsp;'.bloginfo('name').'&lt;/p&gt;';
	}
	return $content;
}
add_filter('the_excerpt_rss', 'add_custom_content');
add_filter('the_content', 'add_custom_content'); */


// remove version info from head and feeds
function complete_version_removal() {
	return '';
}
add_filter('the_generator', 'complete_version_removal');


// customize admin footer text
function custom_admin_footer() {
	echo '&lt;a href="http://monzilla.biz/"&gt;Website Design by Monzilla Media&lt;/a&gt;';
} 
add_filter('admin_footer_text', 'custom_admin_footer');


// enable html markup in user profiles
remove_filter('pre_user_description', 'wp_filter_kses');


// delay feed update
function publish_later_on_feed($where) {
	global $wpdb;

	if (is_feed()) {
		// timestamp in WP-format
		$now = gmdate('Y-m-d H:i:s');

		// value for wait; + device
		$wait = '5'; // integer

		// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
		$device = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

		// add SQL-sytax to default $where
		$where .= " AND TIMESTAMPDIFF($device, $wpdb-&gt;posts.post_date_gmt, '$now') &gt; $wait ";
	}
	return $where;
}
add_filter('posts_where', 'publish_later_on_feed');


// admin link for all settings
function all_settings_link() {
	add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');


// remove nofollow from comments
function xwp_dofollow($str) {
	$str = preg_replace(
		'~&lt;a ([^&gt;]*)\s*(["|\']{1}\w*)\s*nofollow([^&gt;]*)&gt;~U',
		'&lt;a ${1}${2}${3}&gt;', $str);
	return str_replace(array(' rel=""', " rel=''"), '', $str);
}
remove_filter('pre_comment_content',     'wp_rel_nofollow');
add_filter   ('get_comment_author_link', 'xwp_dofollow');
add_filter   ('post_comments_link',      'xwp_dofollow');
add_filter   ('comment_reply_link',      'xwp_dofollow');
add_filter   ('comment_text',            'xwp_dofollow');


// count words in posts
function word_count() {
	global $post;
	echo str_word_count($post-&gt;post_content);
}


// spam &amp; delete links for all versions of wordpress
function delete_comment_link($id) {
	if (current_user_can('edit_post')) {
		echo '| &lt;a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&amp;c='.$id.'"&gt;del&lt;/a&gt; ';
		echo '| &lt;a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&amp;dt=spam&amp;c='.$id.'"&gt;spam&lt;/a&gt;';
	}
}


/* disable all feeds
function fb_disable_feed() {
	wp_die(__('&lt;h1&gt;Feed not available, please visit our &lt;a href="'.get_bloginfo('url').'"&gt;Home Page&lt;/a&gt;!&lt;/h1&gt;'));
}
add_action('do_feed',      'fb_disable_feed', 1);
add_action('do_feed_rdf',  'fb_disable_feed', 1);
add_action('do_feed_rss',  'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1); */


// customize default gravatars
function custom_gravatars($avatar_defaults) {

	// change the default gravatar
	$customGravatar1 = get_bloginfo('template_directory').'/images/gravatar-01.png';
	$avatar_defaults[$customGravatar1] = 'Default';

	// add a custom user gravatar
	$customGravatar2 = get_bloginfo('template_directory').'/images/gravatar-02.png';
	$avatar_defaults[$customGravatar2] = 'Custom Gravatar';

	// add another custom gravatar
	$customGravatar3 = get_bloginfo('template_directory').'/images/gravatar-03.png';
	$avatar_defaults[$customGravatar3] = 'Custom gravatar';

	return $avatar_defaults;
}
add_filter('avatar_defaults', 'custom_gravatars');


// disable auto formatting in posts
function my_formatter($content) {
	$new_content = '';
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

	foreach ($pieces as $piece) {
		if (preg_match($pattern_contents, $piece, $matches)) {
			$new_content .= $matches[1];
		} else {
			$new_content .= wptexturize(wpautop($piece));
		}
	}

	return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);


// escape html entities in comments
function encode_code_in_comment($source) {
	$encoded = preg_replace_callback('/&lt;code&gt;(.*?)&lt;\/code&gt;/ims',
	create_function('$matches', '$matches[1] = preg_replace(array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "", $matches[1]); 
	return "&lt;code&gt;" . htmlentities($matches[1]) . "&lt;/"."code&gt;";'), $source);
	if ($encoded)
		return $encoded;
	else
		return $source;
}
add_filter('pre_comment_content', 'encode_code_in_comment');


// custom comments callback function
function custom_comments_callback($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?&gt;

	&lt;li &lt;?php comment_class(); ?&gt; id="comment-&lt;?php comment_ID(); ?&gt;"&gt;
		&lt;div class="comment-wrap"&gt;
			&lt;?php echo get_avatar(get_comment_author_email(), $size = '50', $default = bloginfo('stylesheet_directory').'/images/gravatar.png'); ?&gt;

			&lt;div class="comment-intro"&gt;
            			&lt;?php printf(__('%s'), get_comment_author_link()); ?&gt; &amp;ndash; &lt;a class="comment-permalink" href="&lt;?php echo htmlspecialchars(get_comment_link($comment-&gt;comment_ID)); ?&gt;"&gt;&lt;?php comment_date('F j, Y'); ?&gt; @ &lt;?php comment_time(); ?&gt;&lt;/a&gt;&lt;?php edit_comment_link('Edit', ' &amp;ndash; ', ''); ?&gt;
			&lt;/div&gt;
			&lt;?php if ($comment-&gt;comment_approved == '0') : ?&gt;

			&lt;p class="comment-moderation"&gt;&lt;?php _e('Your comment is awaiting moderation.'); ?&gt;&lt;/p&gt;
			&lt;?php endif; ?&gt;

			&lt;div class="comment-text"&gt;&lt;?php comment_text(); ?&gt;&lt;/div&gt;

			&lt;div class="reply" id="comment-reply-&lt;?php comment_ID(); ?&gt;"&gt;
				&lt;?php comment_reply_link(array_merge($args, array('reply_text'=&gt;'Reply', 'login_text'=&gt;'Log in to Reply', 'add_below'=&gt;'comment-reply', 'depth'=&gt;$depth, 'max_depth'=&gt;$args['max_depth']))); ?&gt; 

			&lt;/div&gt;
		&lt;/div&gt;

&lt;?php } // WP adds the closing &lt;/li&gt;

?&gt;</code></pre>
<h3 id="functions-download">Download the custom functions.php template file</h3>
<p>If you prefer, you can download this code in a ready-to-go <code>functions.php</code> file in zipped format:</p>
<h4><a href="http://digwp.com/plugins/custom-functions-template-2.zip">Download zipped functions.php Template File</a></h4>
<h3>Also..</h3>
<p>If this post is useful to <acronym title="Digging into WordPress">DiW</acronym> readers, I’ll be writing up yet another article (part 3!) with even more useful custom functions. If that happens, the third set of functions will include some slightly more advanced techniques. <a href="http://feeds2.feedburner.com/DiggingIntoWordpress" title="Grab the Feed!">Stay tuned</a>!</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/">Permalink</a> | <a href="http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/#comments">17 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/&title=WordPress Custom functions.php Template, Part 2">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/template/" rel="tag">template</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>WordPress functions.php Template with 15 Essential Custom Functions</title>
		<link>http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/</link>
		<comments>http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 06:25:58 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=1699</guid>
		<description><![CDATA[When designing WordPress themes, I always add a common set of custom functions to the theme&#8217;s functions.php file. This speeds up development time because I don&#8217;t have to hunt for and individually copy the same slew of functions for every theme. I just drop in a copy of my functions.php template and build up from [...]]]></description>
			<content:encoded><![CDATA[<p>When designing WordPress themes, I always add <strong>a common set of custom functions</strong> to the theme&rsquo;s <code>functions.php</code> file. This speeds up development time because I don&rsquo;t have to hunt for and individually copy the same slew of functions for <em>every</em> theme. I just drop in a copy of my <strong>functions.php template</strong> and build up from there. This takes care of all those little things that always need to be done:</p>
<ul>
<li>Include jQuery</li>
<li>Enable threaded comments</li>
<li>Add feed links to the header</li>
<li>Disable unused widget areas</li>
<li>Adding Google Analytics to the footer</li>
<li>Stop the &ldquo;Read More&rdquo; link from jumping to the middle of the next page&nbsp;;)</li>
</ul>
<p>One of the things that I like about these functions is that they&rsquo;re all so concise, simple, and effective. The <code>functions.php</code> template file currently contains 15 different functions and is a continual work in progress. Not everyone is going to need or use everything in the file, but the idea is to modify this template into something that works for you. It&rsquo;s a starting point with some really useful functions.</p>
<p><span id="more-1699"></span></p>
<p>In this <acronym title="Digging into WordPress">DiW</acronym> article, we first provide an explanation of each of the 15 functions and then bring them all together into the working <code>functions.php</code> template. Just copy and paste the template code at the end of this article or <a href="#functions-download" title="Download the zipped template file">grab a copy of the zipped <code>functions.php</code> file</a> and enjoy a custom collection of functions that will help you optimize your development process while enhancing WordPress with essential functionality.</p>
<h3>Add feed links to header</h3>
<p>Since version 2.8, WordPress can add all relevant feed links (main, comments, categories, et al) to your <code>&lt;head&gt;</code> area. It doesn&rsquo;t happen by default, however, because you have to add the following snippet to make it work:</p>
<pre><code>// add feed links to header
if (function_exists('automatic_feed_links')) {
	automatic_feed_links();
} else {
	return;
}</code></pre>
<p>This will check to see if you&rsquo;re using a version of WordPress that is compatible, and then enable the automatic feed links. A couple of notes: first, this method assumes that you are not manually including any feed links in your <code>&lt;head&gt;</code>. Also, I read a <a href="http://core.trac.wordpress.org/ticket/12364" title="Deprecate automatic_feed_links() in favor of add_theme_support()">recent Trac ticket</a> that looked like this functionality was being integrated with <code>add_theme_support</code>, so keep your eyes open for that.</p>
<h3>Automatic jQuery inclusion</h3>
<p>We&rsquo;ve discussed <a href="http://digwp.com/2009/06/including-jquery-in-wordpress-the-right-way/" title="Including jQuery in WordPress (The Right Way)">how to include jQuery the right way</a> by placing a little snippet in your document head, but here is a way to do it from your theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>// smart jquery inclusion
if (!is_admin()) {
	wp_deregister_script('jquery');
	wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false);
	wp_enqueue_script('jquery');
}</code></pre>
<p>This code ensures that only one copy of jQuery is included, and calls it from Google&rsquo;s servers to save bandwidth and take advantage of any primed caches that happen to be visiting. Note that this function needs to be located <em>before</em> the threaded-comments function in order for it to work.</p>
<h3>Enable threaded comments</h3>
<p>As we explain in <a href="http://digwp.com/book/" title="Get the Book!">the book</a>, enabling threaded comments requires adding a snippet of code into your <code>&lt;head&gt;</code> area just before the <code>wp_head</code> tag. After a little experimenting, I discovered that you can include this snippet from the <code>functions.php</code> file:</p>
<pre><code>// enable threaded comments
function enable_threaded_comments(){
	if (!is_admin()) {
		if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
			wp_enqueue_script('comment-reply');
		}
}
add_action('get_header', 'enable_threaded_comments');</code></pre>
<p>This helps keep your document <code>&lt;head&gt;</code> a little cleaner. Note that this function needs to be located <em>after</em> the jQuery-inclusion function in order for it to work.</p>
<p>Thanks to <a href="http://soupgiant.com/">Peter Wilson</a> for this <a href="http://bigredtin.com/wordpress/including-wordpress-comment-reply-js/" title="Including WordPress' comment-reply.js (the right way)">awesome technique</a>!</p>
<h3>Remove unwanted crap from the head section</h3>
<p>As we&rsquo;ve <a href="http://digwp.com/2009/07/remove-wordpress-version-number/" title="How to Remove the WordPress Version Number (The Right Way)">mentioned before</a>, WordPress spits out a <em>ton</em> of crap in the document <code>&lt;head&gt;</code> &ndash; stuff like the version number and <acronym title="Windows Live Writer">WLW</acronym>, <acronym title="Really Simple Discovery">RSD</acronym>, and index links. To clean things up, we add this nice little snippet into the <code>functions.php</code> template:</p>
<pre><code>// remove junk from head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);</code></pre>
<h3>Add Google Analytics to the footer</h3>
<p>Another annoying task that has to be done for all of the sites I create is adding Google Analytics code to the <code>footer.php</code> file. Recently it occurred to me to just add the code to my <code>functions.php</code> and never worry about it again:</p>
<pre><code>// add google analytics to footer
function add_google_analytics() {
	echo '&lt;script src="http://www.google-analytics.com/ga.js" type="text/javascript"&gt;&lt;/script&gt;';
	echo '&lt;script type="text/javascript"&gt;';
	echo 'var pageTracker = _gat._getTracker("UA-XXXXX-X");';
	echo 'pageTracker._trackPageview();';
	echo '&lt;/script&gt;';
}
add_action('wp_footer', 'add_google_analytics');</code></pre>
<p>A couple of notes here: first, obviously you want to replace the &ldquo;<code>UA-123456-1</code>&rdquo; with your actual <acronym title="Google Analytics">GA</acronym> code. Second, you may want to check out the <a href="http://perishablepress.com/press/2010/01/24/3-ways-track-google-analytics/" title="3 Ways to Track Web Pages with Google Analytics">three currently available Analytics options</a> and modify the code accordingly. Currently, this function is using the newer &ldquo;<code>ga.js</code>&rdquo; tracking code, but that is easily changed to either of the other methods.</p>
<h3>Custom excerpt length</h3>
<p>Instead of using the default 55-word limit, this function enables you to specify any length for your excerpts.</p>
<pre><code>// custom excerpt length
function custom_excerpt_length($length) {
	return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');</code></pre>
<p>Just set your preferred number of words by editing the &ldquo;<code>20</code>&rdquo; to whatever you wish.</p>
<h3>Custom excerpt &ldquo;continue&rdquo; string</h3>
<p>Or whatever they call that funky bracketed ellipses thing &ldquo;<code>[...]</code>&rdquo; that is appended to your post excerpts by default. I like to remove the brackets, but with this <code>functions.php</code> snippet you can change it to anything:</p>
<pre><code>// custom excerpt ellipses for 2.9+
function custom_excerpt_more($more) {
	return '...';
}
add_filter('excerpt_more', 'custom_excerpt_more');

/* custom excerpt ellipses for 2.8-
function custom_excerpt_more($excerpt) {
	return str_replace('[...]', '...', $excerpt);
}
add_filter('wp_trim_excerpt', 'custom_excerpt_more'); 
*/</code></pre>
<p>As you can see, there are two different versions of this code, depending on your version of WordPress. We like to stay current, so we commented out the older method but left it there in case you need it. For either version of this technique, just replace the &ldquo;<code>...</code>&rdquo; with &ldquo;pants on the ground&rdquo; or whatever happens to suit your needs.</p>
<h3>No &ldquo;more&rdquo; jumping for the &ldquo;read more&rdquo; link</h3>
<p>One of the weirdest things that WordPress does is &ldquo;jump&rdquo; the reader to the location of the &ldquo;<code>&lt;!--</code><code>more--&gt;</code>&rdquo; tag on the single-post-view when the &ldquo;read more&rdquo; link is clicked. It&rsquo;s just awkward &#8212; if the jump was on the <em>same</em> page, it would make sense, but to load a new page and then take the reader halfway down without explaining anything is just wrong. In any case, here is a nice little function that will stop the jumping once and for all:</p>
<pre><code>// no more jumping for read more link
function no_more_jumping($post) {
	return '&lt;a href="'.get_permalink($post-&gt;ID).'" class="read-more"&gt;'.'Continue Reading'.'&lt;/a&gt;';
}
add_filter('excerpt_more', 'no_more_jumping');
add_filter('the_content_more_link', 'remove_more_jump_link');</code></pre>
<p>Nothing else needs to be done for this to work &ndash; just plug it in and enjoy your new &ldquo;jumpless&rdquo; functionality. Note that this is also a convenient place to customize the &ldquo;read more&rdquo; link with whatever attributes or custom text you wish.</p>
<h3>Add a favicon to your blog</h3>
<p>You just <em>gotsta</em> have a favicon for your blog, and this code makes it super easy to do. Just create your image and upload to site&rsquo;s root directory. The following code in your <code>functions.php</code> file makes it so by adding the required line to your <code>&lt;head&gt;</code> area:</p>
<pre><code>// add a favicon to your 
function blog_favicon() {
	echo '&lt;link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" /&gt;';
}
add_action('wp_head', 'blog_favicon');</code></pre>
<p>Feel free to change the directory to whatever you desire. Also make sure that the <code>wp_head</code> is present within your theme&rsquo;s <code>header.php</code> file.</p>
<h3>Add a different favicon for your blog&rsquo;s Admin area</h3>
<p>While we&rsquo;re here, let&rsquo;s add a unique favicon to our Admin pages, so that they are easier to recognize when bookmarked or working with tabs. Just create a favicon and upload to your theme&rsquo;s <code>/images/</code> directory. This code takes care of the rest:</p>
<pre><code>// add a favicon for your admin
function admin_favicon() {
	echo '&lt;link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/images/favicon.png" /&gt;';
}
add_action('admin_head', 'admin_favicon');</code></pre>
<p>As before, feel free to change the directory to whatever you desire. It might be best to keep your admin favicon in a separate directory than your blog favicon. Just sayin&rsquo;.</p>
<h3>Custom Admin Login logo</h3>
<p>You know that snazzy blue WordPress logo that is branding your various login pages? Yeh, you can change that to whatever you want. Just create your custom login image, name it &ldquo;<code>custom-login-logo.png</code>&rdquo;, and upload it to your theme&rsquo;s <code>/images/</code> directory. This code will take care of the rest:</p>
<pre><code>// custom admin login logo
function custom_login_logo() {
	echo '&lt;style type="text/css"&gt;
	h1 a { background-image: url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
	&lt;/style&gt;';
}
add_action('login_head', 'custom_login_logo');</code></pre>
<p>The key here is to make sure that the path and image names match that of your setup. Also, when creating your image, you may want to keep in mind the properties of the original: 30px length, 31px height, transparent <acronym title="Graphic Interchange Format">GIF</acronym> format, and header background color of #464646 (for the image matte).</p>
<h3>Disable unused widget areas</h3>
<p><a href="http://justintadlock.com/archives/2009/03/06/disable-widget-areas-without-touching-theme-templates" title="Disable widget areas (sidebars) without touching theme templates">Justin Tadlock</a> shares this handy function for removing unwanted widget areas from our theme &ndash; a <em>must</em> for customizing  existing themes:</p>
<pre><code>// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
	//if (is_home())
		$sidebars_widgets = array(false);
	return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');</code></pre>
<p>This code is plug-&amp;-play &ndash; no other modifications need to be made. Note: if you only want to disable widgets on your Home page, then remove the two comment slashes (&ldquo;<code>//</code>&rdquo;) from the third line.</p>
<h3>Kill the WordPress update nag</h3>
<p>This is one of my favorites, but I know it&rsquo;s not for everyone. In any case, you know that &ldquo;Please update now..&rdquo; message that appears on every page in the WordPress Admin when new versions are available? This sweet little function kills it dead (or disables it, actually):</p>
<pre><code>// kill the admin nag
if (!current_user_can('edit_users')) {
	add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
	add_filter('pre_option_update_core', create_function('$a', "return null;"));
}</code></pre>
<p>Feel free to comment this one out or remove it if you rely on the Admin nag to keep you informed of changes.</p>
<h3>Include category ID in body_class and post_class</h3>
<p>By default, WordPress <code>body_class</code> and <code>post_class</code> do not include the ID of the category of the current post. This custom function changes all that:</p>
<pre><code>// category id in body and post class
function category_id_class($classes) {
	global $post;
	foreach((get_the_category($post-&gt;ID)) as $category)
		$classes [] = 'cat-' . $category-&gt;cat_ID . '-id';
		return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');</code></pre>
<p>Even if you aren&rsquo;t using it, it&rsquo;s a nice function to have around, which is why it&rsquo;s included here for this custom <code>functions.php</code> template of essential functions. Keywords in the house because we can.</p>
<h3>Get the first category ID</h3>
<p>Another useful function when working with different categories is the ability to get the first category ID of the current post. This function makes it happen:</p>
<pre><code>// get the first category id
function get_first_category_ID() {
	$category = get_the_category();
	return $category[0]-&gt;cat_ID;
}</code></pre>
<p>Strictly plug-&amp;-play: just use <code>&lt;?php get_first_category_ID(); ?&gt;</code> in your theme template file to access the data.</p>
<h3>Putting it all together..</h3>
<p>As promised, here is the full-meal deal &ndash; the entire collection neatly organized into a single chunk of code:</p>
<pre><code>&lt;?php // custom functions.php template @ digwp.com

// add feed links to header
if (function_exists('automatic_feed_links')) {
	automatic_feed_links();
} else {
	return;
}


// smart jquery inclusion
if (!is_admin()) {
	wp_deregister_script('jquery');
	wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '1.3.2');
	wp_enqueue_script('jquery');
}


// enable threaded comments
function enable_threaded_comments(){
	if (!is_admin()) {
		if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
			wp_enqueue_script('comment-reply');
		}
}
add_action('get_header', 'enable_threaded_comments');


// remove junk from head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);


// add google analytics to footer
function add_google_analytics() {
	echo '&lt;script src="http://www.google-analytics.com/ga.js" type="text/javascript"&gt;&lt;/script&gt;';
	echo '&lt;script type="text/javascript"&gt;';
	echo 'var pageTracker = _gat._getTracker("UA-XXXXX-X");';
	echo 'pageTracker._trackPageview();';
	echo '&lt;/script&gt;';
}
add_action('wp_footer', 'add_google_analytics');


// custom excerpt length
function custom_excerpt_length($length) {
	return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');


// custom excerpt ellipses for 2.9+
function custom_excerpt_more($more) {
	return '...';
}
add_filter('excerpt_more', 'custom_excerpt_more');

/* custom excerpt ellipses for 2.8-
function custom_excerpt_more($excerpt) {
	return str_replace('[...]', '...', $excerpt);
}
add_filter('wp_trim_excerpt', 'custom_excerpt_more'); 
*/


// no more jumping for read more link
function no_more_jumping($post) {
	return '&lt;a href="'.get_permalink($post-&gt;ID).'" class="read-more"&gt;'.'Continue Reading'.'&lt;/a&gt;';
}
add_filter('excerpt_more', 'no_more_jumping');


// add a favicon to your 
function blog_favicon() {
	echo '&lt;link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" /&gt;';
}
add_action('wp_head', 'blog_favicon');


// add a favicon for your admin
function admin_favicon() {
	echo '&lt;link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('stylesheet_directory').'/images/favicon.png" /&gt;';
}
add_action('admin_head', 'admin_favicon');


// custom admin login logo
function custom_login_logo() {
	echo '&lt;style type="text/css"&gt;
	h1 a { background-image: url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
	&lt;/style&gt;';
}
add_action('login_head', 'custom_login_logo');


// disable all widget areas
function disable_all_widgets($sidebars_widgets) {
	//if (is_home())
		$sidebars_widgets = array(false);
	return $sidebars_widgets;
}
add_filter('sidebars_widgets', 'disable_all_widgets');


// kill the admin nag
if (!current_user_can('edit_users')) {
	add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
	add_filter('pre_option_update_core', create_function('$a', "return null;"));
}


// category id in body and post class
function category_id_class($classes) {
	global $post;
	foreach((get_the_category($post-&gt;ID)) as $category)
		$classes [] = 'cat-' . $category-&gt;cat_ID . '-id';
		return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');


// get the first category id
function get_first_category_ID() {
	$category = get_the_category();
	return $category[0]-&gt;cat_ID;
}

?&gt;</code></pre>
<h3 id="functions-download">Download the custom functions.php template file</h3>
<p>If you prefer, you can download this code in a ready-to-go <code>functions.php</code> file in zipped format:</p>
<h4><a href="http://digwp.com/plugins/custom-functions-template.zip">Download zipped functions.php Template File</a></h4>
<h3>Also..</h3>
<p>If this post is useful to <acronym title="Digging into WordPress">DiW</acronym> readers, I&rsquo;ll be writing up another article (part 2) with even more useful custom functions. If that happens, the second set of functions will include some slightly more advanced techniques as opposed to these &ldquo;must-have&rdquo; template functions. <a href="http://feeds2.feedburner.com/DiggingIntoWordpress" title="Grab the Feed!">Stay tuned</a>! <strong>Update:</strong> <a href="http://digwp.com/2010/04/wordpress-custom-functions-php-template-part-2/" title="WordPress Custom functions.php Template, Part 2">Custom functions.php Part 2 is here</a>!</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/">Permalink</a> | <a href="http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/#comments">80 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/&title=WordPress functions.php Template with 15 Essential Custom Functions">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/template/" rel="tag">template</a>, <a href="http://digwp.com/tag/theme/" rel="tag">Theme</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/03/wordpress-functions-php-template-custom-functions/feed/</wfw:commentRss>
		<slash:comments>80</slash:comments>
		</item>
	</channel>
</rss>

