<?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; PHP</title>
	<atom:link href="http://digwp.com/category/php/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>Import Feed, Display in Multiple Columns</title>
		<link>http://digwp.com/2011/09/feed-display-multiple-columns/</link>
		<comments>http://digwp.com/2011/09/feed-display-multiple-columns/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 16:24:49 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[feeds]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5236</guid>
		<description><![CDATA[Recently I worked on a project where a single RSS feed was imported and displayed in multiple columns on the web page. Certain pages display feed items in two columns, others in groups of three or more. This technique uses WordPress&#8217; built-in fetch_feed functionality to parse external feeds, and a slice of PHP magic to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I worked on a project where a single <abbr title="Really Simple Syndication">RSS</abbr> feed was imported and displayed in <strong>multiple columns</strong> on the web page. Certain pages display feed items in two columns, others in groups of three or more. This technique uses WordPress&#8217; built-in <code>fetch_feed</code> functionality to parse external feeds, and a <em>slice</em> of <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> magic to display them in <em>multiple columns</em>. It&#8217;s flexible too, enabling any number of columns and feed items from anywhere in your theme/template files. For example, you could display any of the following:</p>
<p><span id="more-5236"></span></p>
<ul>
<li><strong>10 feed items</strong> split into <em>two columns of five</em></li>
<li><strong>30 feed items</strong> split into <em>three columns of 10</em></li>
<li><strong>30 feed items</strong> split into <em>five columns of six</em></li>
</ul>
<p>Notice the pattern? The key to showing the same number of posts in each column is to set the total number of feed items as a <em>multiple</em> of the total number of columns. Before explaining how that works, let&#8217;s back up a bit and get <code>fetch_feed()</code> set up..</p>
<h3>Setting up fetch_feed()</h3>
<p>To display feeds in multiple columns, use WordPress&#8217; <code>fetch_feed</code> function, set up like this anywhere in your theme template files:</p>
<pre><code>&lt;?php include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
	$feed = fetch_feed('http://example.com/feed/'); // feed URL
	if (!is_wp_error($feed)) : $feed-&gt;init();
		$feed-&gt;set_output_encoding('UTF-8');	// set encoding
		$feed-&gt;handle_content_type();		// ensure encoding
		$feed-&gt;set_cache_duration(21600);	// six hours in seconds
		$limit = $feed-&gt;get_item_quantity(10);	// get 10 feed items
		$items = $feed-&gt;get_items(0, $limit);	// set array
	endif;
} ?&gt;</code></pre>
<p>There are three things to configure:</p>
<ul>
<li><strong>feed URL</strong> &ndash; replace <code>http://example.com/feed/</code> with the feed URL</li>
<li><strong>cache duration</strong> &ndash; change if needed, default is six hours (in seconds)</li>
<li><strong>number of feed items</strong> &ndash; should be some multiple of the number of columns</li>
</ul>
<p>So for example, getting 10 feed items would enable us to display 1, 2, 5, or 10 columns, with the same number of feed items in each column. So, to display a feed evenly across <em>three</em> columns, some <em>multiple of three</em> is required for the number of feed items. So that would be 3, 6, 9, 12, and so on &ndash; basically any number divisible three.</p>
<p>Read more about the other <code>fetch_feed</code> variables at the <a href="http://codex.wordpress.org/Function_Reference/fetch_feed">WordPress Codex</a>.</p>
<h3>Setting up array_slice()</h3>
<p>Once <code>fetch_feed()</code> is set up, we have an array of <code>$items</code> containing our feed content. To display the items in multiple columns, we use PHP&#8217;s incredibly useful <a href="http://php.net/manual/en/function.array-slice.php">array_slice</a> function, which enables us to <strong>extract slices of the feed array</strong>. The <code>array_slice</code> function accepts three basic parameters, the input array, offset, and length:</p>
<p><code>array_slice($array, $offset, $length);</code></p>
<p>Using the <code>$items</code> array generated from the <code>fetch_feed()</code> function, we calculate the other two <code>array_slice</code> variables depending on the total number of columns and feed items. So for the sake of simplicity, let&#8217;s say we want to display <strong>two columns each with five feed items</strong>. The PHP would look something like this:</p>
<pre><code>// display first five feed items
$blocks = array_slice($items, 0, 5); // grab first five items from the array
foreach ($blocks as $block) {
	echo $block-&gt;get_title();
	echo $block-&gt;get_description();
}

// display next five feed items
$blocks = array_slice($items, 5, 10); // grab next five items from the array
foreach ($blocks as $block) {
	echo $block-&gt;get_title();
	echo $block-&gt;get_description();
}</code></pre>
<p>Likewise, to display <strong>30 feed items in three columns of 10 each</strong>, we would create <em>three</em> <code>foreach</code> loops using the following <code>array_slice</code> values:</p>
<p><code>$blocks = array_slice($items, 0, 10); // first column</code><br />
<code>$blocks = array_slice($items, 10, 20); // second column</code><br />
<code>$blocks = array_slice($items, 20, 30); // third column</code></p>
<p>As you can see, the <em>key</em> to displaying external feeds in multiple columns is configuring <code>array_slice</code> with the correct <code>$offset</code> and <code>$length</code> parameter values. Once you&#8217;ve got that dialed in, you&#8217;re ready to put it all together and make it happen.</p>
<h3>Displaying feed items</h3>
<p>Notice in the previous section that we use the following to display feed items in each column:</p>
<p><code>echo $block-&gt;get_title();</code><br />
<code>echo $block-&gt;get_description();</code></p>
<p>That&#8217;ll display the title and description for each feed item, but you can do much more than that. Here is a more elaborate example showing how different feed items may be included in the markup:</p>
<pre><code>&lt;div class="feed-item"&gt;
	&lt;h1&gt;&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;&lt;?php echo $block-&gt;get_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
	&lt;p&gt;
		&lt;?php echo $block-&gt;get_date("l, F jS, Y"); ?&gt;
		&lt;?php echo substr($block-&gt;get_description(), 0, 200); // limit to 200 characters ?&gt; 
		&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;Continue reading&lt;/a&gt;
	&lt;/p&gt;
&lt;/div&gt;</code></pre>
<p>For each feed item in the loop, this snippet displays the following:</p>
<ul>
<li>Title of the feed item, linked to the post URL</li>
<li>Content of the feed item, truncated to 200 characters</li>
<li>A &#8220;Continue reading&#8221; link, linked to the post URL</li>
<li>Each feed item is wrapped with <code>&lt;div class="feed-item"&gt;</code></li>
</ul>
<p>There is much more available for customizing your imported feeds, see the <a href="http://simplepie.org/wiki/">SimplePie documentation</a> and more specifically the <a href="http://simplepie.org/wiki/reference/start#simplepie_item">item-level data methods</a> for all the details.</p>
<h3>Putting it all together</h3>
<p>Here is the final code for displaying external feed content in multiple columns:</p>
<pre><code>&lt;?php include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
	$feed = fetch_feed('http://example.com/feed/');
	if (!is_wp_error($feed)) : $feed-&gt;init();
		$feed-&gt;set_output_encoding('UTF-8');	// set encoding
		$feed-&gt;handle_content_type();		// ensure encoding
		$feed-&gt;set_cache_duration(21600);	// six hours in seconds
		$limit = $feed-&gt;get_item_quantity(10);	// get 10 feed items
		$items = $feed-&gt;get_items(0, $limit);	// set array
	endif;
}

if ($limit == 0) { 
	echo '&lt;p&gt;RSS Feed currently unavailable.&lt;/p&gt;'; 
} else {
	// display first five feed items
	$blocks = array_slice($items, 0, 5);
	foreach ($blocks as $block) { ?&gt;
		&lt;div class="feed-item column-one"&gt;
			&lt;h1&gt;&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;&lt;?php echo $block-&gt;get_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
			&lt;p&gt;
				&lt;?php echo $block-&gt;get_date("l, F jS, Y"); ?&gt;
				&lt;?php echo substr($block-&gt;get_description(), 0, 200); ?&gt; 
				&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;Continue reading&lt;/a&gt;
			&lt;/p&gt;
		&lt;/div&gt;
	&lt;?php } ?&gt;

	// display next five feed items
	$blocks = array_slice($items, 5, 10);
	foreach ($blocks as $block) { ?&gt;
		&lt;div class="feed-item column-two"&gt;
			&lt;h1&gt;&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;&lt;?php echo $block-&gt;get_title(); ?&gt;&lt;/a&gt;&lt;/h1&gt;
			&lt;p&gt;
				&lt;?php echo $block-&gt;get_date("l, F jS, Y"); ?&gt;
				&lt;?php echo substr($block-&gt;get_description(), 0, 200); ?&gt; 
				&lt;a href="&lt;?php echo $block-&gt;get_permalink(); ?&gt;"&gt;Continue reading&lt;/a&gt;
			&lt;/p&gt;
		&lt;/div&gt;
	&lt;?php }
} ?&gt;</code></pre>
<p>As-is, this code displays <strong>two columns of five feed items each</strong>. Using the techniques and info presented in this article, it&#8217;s possible to configure this code to display <em>any number of feed items in any number of columns</em>. This code works when placed in just about any theme template file that makes sense. Probably best when placed <em>outside</em> of the WordPress post loop.</p>
<p>Note that we also verify the feed is available before trying to display anything. After setting up <code>fetch_feed()</code>, we inject the following PHP logic:</p>
<pre><code>if ($limit == 0) { 
	echo '&lt;p&gt;RSS Feed currently unavailable.&lt;/p&gt;'; 
} else {
	// display first five feed items
	// display next five feed items
}</code></pre>
<p>This ensures that no empty markup tags are echoed to the browser if/when the feed is unavailable. It happens, so checking is a good move.</p>
<h3>Instant Summary</h3>
<p>Using <code>array_slice()</code> together with <code>fetch_feed()</code>, it&#8217;s possible to display any number of feed items in any number of columns. This provides much design flexibility for your project.</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/09/feed-display-multiple-columns/">Permalink</a> | <a href="http://digwp.com/2011/09/feed-display-multiple-columns/#comments">12 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/09/feed-display-multiple-columns/&title=Import Feed, Display in Multiple Columns">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/feeds/" rel="tag">feeds</a>, <a href="http://digwp.com/tag/php/" rel="tag">PHP</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/09/feed-display-multiple-columns/feed/</wfw:commentRss>
		<slash:comments>12</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>Create an Articles-Only Feed</title>
		<link>http://digwp.com/2011/08/custom-feeds/</link>
		<comments>http://digwp.com/2011/08/custom-feeds/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 15:33:10 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[feeds]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[query_posts]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5179</guid>
		<description><![CDATA[WordPress makes it easy to publish content in any number of categories, with any number of tags, and with any type of custom post format. So for example, in addition to full articles, you could also offer screencasts, links, side posts, tweets, and all sorts of other peripheral content. Complementary material may work great for [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress makes it easy to publish content in any number of categories, with any number of tags, and with any type of custom post format. So for example, in addition to full articles, you could also offer screencasts, links, side posts, tweets, and all sorts of other peripheral content. Complementary material may work great for visitors surfing around your site, but including all of that extra stuff in your RSS feed dilutes the potency of your main articles. The idea here is that your visitors will subscribe to the more <em>focused</em> content.</p>
<p><span id="more-5179"></span></p>
<p>In this post, we create a custom, &#8220;articles-only&#8221; feed for visitors who want to subscribe to the main content only, without all the side stuff. The technique is actually pretty flexible, and works great to create just about <em>any</em> type of custom WordPress feed content. It&#8217;s pretty easy to do, requiring a whopping two steps to make it happen..</p>
<h3>Step 1: Create the custom page template</h3>
<p>Go to your theme directory and create a new <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> file named <code>article-feed.php</code>. Then add the following code (don&#8217;t worry it&#8217;s actually not that bad):</p>
<pre><code>&lt;?php 
/* 
Template Name: Article Feed
*/
$numposts = 10; // number of posts in feed
$posts = query_posts('showposts='.$numposts.'&amp;cat=3');
$more = 1;

header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '&lt;?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'&gt;';
?&gt;

&lt;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/"
	&lt;?php do_action('rss2_ns'); ?&gt;
&gt;
&lt;channel&gt;
	&lt;title&gt;&lt;?php bloginfo_rss('name'); wp_title_rss(); ?&gt; - Article Feed&lt;/title&gt;
	&lt;atom:link href="&lt;?php self_link(); ?&gt;" rel="self" type="application/rss+xml" /&gt;
	&lt;link&gt;&lt;?php bloginfo_rss('url') ?&gt;&lt;/link&gt;
	&lt;description&gt;&lt;?php bloginfo_rss("description") ?&gt;&lt;/description&gt;
	&lt;lastBuildDate&gt;&lt;?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?&gt;&lt;/lastBuildDate&gt;
	&lt;?php the_generator( 'rss2' ); ?&gt;
	&lt;language&gt;&lt;?php echo get_option('rss_language'); ?&gt;&lt;/language&gt;
	&lt;sy:updatePeriod&gt;&lt;?php echo apply_filters( 'rss_update_period', 'hourly' ); ?&gt;&lt;/sy:updatePeriod&gt;
	&lt;sy:updateFrequency&gt;&lt;?php echo apply_filters( 'rss_update_frequency', '1' ); ?&gt;&lt;/sy:updateFrequency&gt;
	&lt;?php do_action('rss2_head'); ?&gt;
	&lt;?php while( have_posts()) : the_post(); ?&gt;

	&lt;item&gt;
		&lt;title&gt;&lt;?php the_title_rss(); ?&gt;&lt;/title&gt;
		&lt;link&gt;&lt;?php the_permalink_rss(); ?&gt;&lt;/link&gt;
		&lt;comments&gt;&lt;?php comments_link(); ?&gt;&lt;/comments&gt;
		&lt;pubDate&gt;&lt;?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?&gt;&lt;/pubDate&gt;
		&lt;dc:creator&gt;&lt;?php the_author(); ?&gt;&lt;/dc:creator&gt;
&lt;?php the_category_rss(); ?&gt;
		&lt;guid isPermaLink="false"&gt;&lt;?php the_guid(); ?&gt;&lt;/guid&gt;
&lt;?php if (get_option('rss_use_excerpt')) : ?&gt;

		&lt;description&gt;&lt;![CDATA[&lt;?php the_excerpt_rss() ?&gt;]]&gt;&lt;/description&gt;
&lt;?php else : ?&gt;

		&lt;description&gt;&lt;![CDATA[&lt;?php the_excerpt_rss() ?&gt;]]&gt;&lt;/description&gt;
	&lt;?php if ( strlen( $post-&gt;post_content ) &gt; 0 ) : ?&gt;

		&lt;content:encoded&gt;&lt;![CDATA[&lt;?php the_content() ?&gt;]]&gt;&lt;/content:encoded&gt;
	&lt;?php else : ?&gt;

		&lt;content:encoded&gt;&lt;![CDATA[&lt;?php the_excerpt_rss() ?&gt;]]&gt;&lt;/content:encoded&gt;
	&lt;?php endif; ?&gt;
&lt;?php endif; ?&gt;

		&lt;wfw:commentRss&gt;&lt;?php echo get_post_comments_feed_link(); ?&gt;&lt;/wfw:commentRss&gt;
		&lt;slash:comments&gt;&lt;?php echo get_comments_number(); ?&gt;&lt;/slash:comments&gt;
&lt;?php rss_enclosure(); ?&gt;
&lt;?php do_action('rss2_item'); ?&gt;

	&lt;/item&gt;
	&lt;?php endwhile; ?&gt;

&lt;/channel&gt;
&lt;/rss&gt;</code></pre>
<p>This is basically just a typical WordPress feed template, but with some extra query action to make it do what we want. In the first few lines of code, we specify how many posts to display, which categories to include, and whether to display full posts or excerpts:</p>
<pre><code>$numposts = 10; // number of posts in feed
$posts = query_posts('showposts='.$numposts.'&amp;cat=3');
$more = 1;</code></pre>
<p>Using this default code, the custom feed will show 10 posts from category 3. And with <code>$more</code> set to <code>1</code>, the <em>full</em> article will be included in the feed. This is where you customize your new feed to include whatever you want. So for example, if we post articles in three different categories, say with IDs of <code>1</code>, <code>2</code>, and <code>3</code>, we would modify our code to look like this:</p>
<pre><code>$numposts = 10; // number of posts in feed
$posts = query_posts('showposts='.$numposts.'&amp;cat=1,2,3');
$more = 1;</code></pre>
<p>The <code>query_posts</code> function is quite flexible and provides all of the parameters needed to set up just about any customized feed content. You can exclude/include categories, display specific types of posts, change the number of posts displayed, and so much more. For a full list of options, check out <a href="http://codex.wordpress.org/Function_Reference/query_posts">query_posts at the WordPress Codex</a>.</p>
<h3>Step 2: Create the custom Page in the WP Admin</h3>
<p>Once you have the custom Page Template uploaded to your server, log into the WordPress Admin and visit the &#8220;Add New Page&#8221; screen (<strong>Pages &gt; Add New</strong>). Create a new page named &#8220;Articles-Only Feed&#8221; (or whatever), and select the newly added &#8220;articles&#8221; page template from the drop-down menu:</p>
<p><img src="http://digwp.com/wp-content/blog-images/articles-only-template.gif" alt="" /></p>
<p>While you&#8217;re there in the Admin creating this new custom-feed page, take a moment to be mindful of the feed <abbr title="Uniform Resource Locator">URL</abbr> and title, which may be set to whatever works best:</p>
<p><img src="http://digwp.com/wp-content/blog-images/articles-only-page.gif" alt="" /></p>
<p>Once everything is ready, finish it up by publishing the page to your site. Then check that your new feed is working by visiting the URL of the new page you just created in the WP Admin. Here at <a href="http://digwp.com/">DigWP.com</a>, our articles-only feed is available at <a href="http://digwp.com/articles/" title="DigWP.com - Articles Feed"><code>http://digwp.com/articles/</code></a>.</p>
<h3>Wrap-up + additional resources</h3>
<p>In addition to the easy-breezy two-step custom-feed tutorial in this post, here are some additional resources that are sort of related to this general topic:</p>
<ul>
<li><a href="http://digwp.com/2011/04/tumblr-links-post-formats/">Tumblr Links with Post Formats</a></li>
<li><a href="http://digwp.com/2009/09/easy-custom-feeds-in-wordpress/">Easy Custom Feeds in WordPress</a></li>
<li><a href="http://digwp.com/2009/09/tumblr-style-links-for-posts-and-feeds/">How to Implement Tumblr-Style Links for Posts and Feeds</a></li>
</ul>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/08/custom-feeds/">Permalink</a> | <a href="http://digwp.com/2011/08/custom-feeds/#comments">12 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/08/custom-feeds/&title=Create an Articles-Only Feed">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/feeds/" rel="tag">feeds</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</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/08/custom-feeds/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to Redirect Logged-In Users</title>
		<link>http://digwp.com/2011/08/how-to-redirect-logged-in-users/</link>
		<comments>http://digwp.com/2011/08/how-to-redirect-logged-in-users/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 17:18:38 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Admin]]></category>
		<category><![CDATA[redirects]]></category>
		<category><![CDATA[users]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=4731</guid>
		<description><![CDATA[WordPress provides a variety of ways to redirect logged-in users. In this DiW post, we explain each of these methods along with some useful tips and tricks along the way. These techniques enable you to redirect logged-in users to internal pages, external pages, and even return them to the current page. wp_redirect The wp_redirect function [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress provides a variety of ways to redirect logged-in users. In this <abbr title="Digging into WordPress">DiW</abbr> post, we explain each of these methods along with some useful tips and tricks along the way. These techniques enable you to redirect logged-in users to internal pages, external pages, and even return them to the current page.</p>
<p><span id="more-4731"></span></p>
<h3>wp_redirect</h3>
<p>The <a href="http://codex.wordpress.org/Function_Reference/wp_redirect" title="Function Reference/wp redirect">wp_redirect</a> function is for redirecting <em>all users</em> to any absolute <abbr title="Uniform Resource Identifier">URI</abbr>. Absolute URIs are basically <em>full</em> <abbr title="Uniform Resource Locator">URL</abbr>s and look like this:</p>
<ul>
<li><code>http://digwp.com/book/</code></li>
<li><code>http://digwp.com/wp-content/blog-images/redirecting-users.jpg</code></li>
<li><code>ftp://example.com/transfer/</code></li>
</ul>
<p>To redirect users with <code>wp_redirect</code>, place the following <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> snippet in your theme file:</p>
<pre><code>&lt;?php wp_redirect('http://example.com/'); exit; ?&gt;</code></pre>
<p>The parameters for <code>wp_redirect</code> are <em>two</em> in number:</p>
<ul>
<li><strong>$location</strong> = The absolute URI to which the user will be redirected. No default.</li>
<li><strong>$status</strong> = The status code to use. For example, <code>301</code>, <code>302</code>, etc. The default is <code>302</code>.</li>
</ul>
<p>You can use template tags for the <code>$location</code> parameter, for example:</p>
<pre><code>&lt;?php // redirect to the home page
wp_redirect(home_url()); exit; ?&gt;

&lt;?php // redirect back to current page
wp_redirect(get_permalink()); exit; ?&gt;</code></pre>
<p>To use a status code other than the default <code>302</code>, specify it like so:</p>
<pre><code>&lt;?php wp_redirect('http://example.com/', 301); exit; ?&gt;</code></pre>
<p>That code will send the user to <code>example.com</code> along with status code <code>301</code> (Moved Permanently). The default <code>302</code> should be used for <em>temporary</em> redirects. <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" title="10 Status Code Definitions">Learn more about status codes</a>.</p>
<h3 id="auth_redirect">auth_redirect</h3>
<p>Next up, we have <a href="http://codex.wordpress.org/Function_Reference/auth_redirect" title="Function Reference/auth redirect">auth_redirect</a>, which is a &#8220;simple function that requires that the user be logged in before they can access a page.&#8221; So for example, let&#8217;s say you have a &#8220;downloads&#8221; page that&#8217;s meant for logged-in users only. To prevent unauthorized access, we would add the following code to the top of the downloads page template:</p>
<pre><code>&lt;?php auth_redirect(); ?&gt;</code></pre>
<p>This extremely useful function checks whether or not the current user is logged in, and redirects them to the <strong>Login Page</strong> if not. By default, the user will be redirected back to the page from whence they came (e.g., the downloads page). Currently this function accepts no parameters, but having something like <strong>$location</strong> would make it even more awesome&nbsp;;)</p>
<h3>wp_logout_url</h3>
<p>The <a href="http://codex.wordpress.org/Function_Reference/wp_logout_url" title="Function Reference/wp logout url">wp_logout_url</a> function <em>returns</em> the URL of your site&#8217;s <strong>Logout Page</strong>. But it can also redirect the user to any URL using the <code>$redirect</code> parameter, like so:</p>
<pre><code>&lt;?php echo wp_logout_url($redirect); ?&gt;</code></pre>
<p><code>$redirect</code> parameter is a string containing the redirect URL. Here are some examples:</p>
<pre><code>&lt;!-- Logout &amp; redirect to the home page --&gt;
&lt;a href="&lt;?php echo wp_logout_url(home_url()); ?&gt;"&gt;Logout&lt;/a&gt;

&lt;!-- Logout &amp; redirect to current page --&gt;
&lt;a href="&lt;?php echo wp_logout_url(get_permalink()); ?&gt;"&gt;Logout&lt;/a&gt;

&lt;!-- Logout &amp; redirect to specific URL --&gt;
&lt;a href="&lt;?php echo wp_logout_url('http://example.com/'); ?&gt;"&gt;Logout&lt;/a&gt;</code></pre>
<p>The automatic logout link is great, but the subsequent redirect is a super-useful tool that we use in our <a href="http://digwp.com/2010/12/login-register-password-code/" title="Custom Login/Register/Password Code">custom login/register tutorial</a>.</p>
<h3>is_user_logged_in</h3>
<p>Lastly we have the <a href="http://codex.wordpress.org/Function_Reference/is_user_logged_in" title="Function Reference/is user logged in">is_user_logged_in</a> function, which technically doesn&#8217;t do any redirecting, but enables you to <strong>check if the user is logged in</strong>. Here is a simple example that should work in any theme template file:</p>
<pre><code>&lt;?php if (is_user_logged_in()) { echo "logged in"; } else { echo "not logged in" } ?&gt;</code></pre>
<p>The <code>is_user_logged_in</code> function returns <code>TRUE</code> or <code>FALSE</code>, depending on the user&#8217;s status. So to display custom content to logged in users, you could do something like this:</p>
<pre><code>&lt;?php if (is_user_logged_in()) { ?&gt;

	&lt;p&gt;Welcome, registered user!&lt;/p&gt;

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

	&lt;p&gt;Welcome, visitor!&lt;/p&gt;

&lt;?php } ?&gt;</code></pre>
<p>And you can mix-n-match <code>is_user_logged_in</code> with other template tags and PHP snippets to return more refined results. For example, we can serve custom content based on logged status <em>and</em> location:</p>
<pre><code>&lt;?php if (is_user_logged_in()) {

	if (is_page()) {
		echo "Logged-in user visiting a page";

	} elseif (is_feed()) {
		echo "Logged-in user viewing a feed";

	} elseif (is_search()) {
		echo "Logged-in user doing a search";

	} else {
		echo "Logged-in user doing something else";
	}

} else { // user is not logged-in

	if (is_page()) {
		echo "Normal visitor on a page";

	} elseif (is_feed()) {
		echo "Normal visitor viewing a feed";

	} elseif (is_search()) {
		echo "Normal visitor doing a search";

	} else {
		echo "Normal visitor doing something else";
	}

} ?&gt;</code></pre>
<p>This example demonstrates the flexibility of WordPress conditional tags and how <code>is_user_logged_in</code> throws another layer of control to the mix.</p>
<h3>Note about auth_redirect and is_user_logged_in</h3>
<p>As <a href="http://wordpress.org/support/topic/auth_redirect" title="Support Topic: auth_redirect">discussed here</a>, <code>auth_redirect</code> currently seems to be acting kinda weird, where the post-login redirect isn&#8217;t working. Instead of returning the user to the current page after login, the user always ends up back at the Login Page. It <em>looks</em> like the <code>auth_redirect</code> function isn&#8217;t recognizing the new &#8220;logged-in&#8221; user status, and so it just keeps sending the user back to <code>login.php</code>.</p>
<p>So until this is fixed, there is a simple workaround which involves <em>combining</em> <code>auth_redirect</code> with <code>is_user_logged_in</code> to do <a href="#auth_redirect">what auth_redirect <em>should</em> be doing</a>. All that&#8217;s required is the following snippet of code placed at the top of your theme template:</p>
<pre><code>&lt;?php if (!is_user_logged_in()) { auth_redirect(); } ?&gt;</code></pre>
<p>With this technique, <code>auth_redirect</code> executes only if the user is NOT logged in. So yeah it&#8217;s a hack, but it works great and should continue working if/when <code>auth_redirect</code> is fixed.</p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/08/how-to-redirect-logged-in-users/">Permalink</a> | <a href="http://digwp.com/2011/08/how-to-redirect-logged-in-users/#comments">9 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/08/how-to-redirect-logged-in-users/&title=How to Redirect Logged-In Users">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/admin/" rel="tag">Admin</a>, <a href="http://digwp.com/tag/redirects/" rel="tag">redirects</a>, <a href="http://digwp.com/tag/users/" rel="tag">users</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/08/how-to-redirect-logged-in-users/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Graphing Blog Comments Over Time</title>
		<link>http://digwp.com/2011/04/graphing-blog-comments-over-time/</link>
		<comments>http://digwp.com/2011/04/graphing-blog-comments-over-time/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 17:19:45 +0000</pubDate>
		<dc:creator>Chris Coyier</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[graph]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=4267</guid>
		<description><![CDATA[One of my other blocks, CSS-Tricks, has been around a number of years now. There are nearly 1,400 unique pages of content almost all of which have a comment thread. I had a feeling that in the last four years, despite fairly steady growth in traffic and subscribers, that the number of comments per post [...]]]></description>
			<content:encoded><![CDATA[<p>One of my other blocks, CSS-Tricks, has been around a number of years now. There are nearly 1,400 unique pages of content almost all of which have a comment thread. I had a feeling that in the last four years, despite fairly steady growth in traffic and subscribers, that the number of comments per post has dropped. But how to prove it? I don&#8217;t know of a way to easily see that data. </p>
<p>So I built a way to visualize the number of comments per post! I&#8217;m sure all you WordPress smarties have some better fancier way to do this, but this is how I did it. </p>
<p><strong>In a nutshell:</strong> write a <a href="http://codex.wordpress.org/Function_Reference/query_posts">custom query</a> to loop through all your posts and <a href="http://codex.wordpress.org/Template_Tags/get_comments_number">get the number of comments</a>. So combining those two things, I thought I&#8217;d just make a 1px wide element with a height relative to how many comments that post has.</p>
<p><span id="more-4267"></span></p>
<h3>Step 1: Figure out the maximum number of comments</h3>
<p>This will help us set the scale. The post with the maximum number of with have a line with 100% height, and it scales down from there. It&#8217;s easy to figure out the maximum number of comments <strong>after</strong> you looped through all the posts, but we actually need that information <strong>before</strong> we even start the loop so we can set the height during the loop.</p>
<p>Fortunately WordPress keeps the &#8220;comment_count&#8221; for each post in the the database. So we can write a query that will return the post with the most comments and then extract that number into a variable.</p>
<pre><code>$query = "SELECT comment_count FROM " . $wpdb-&gt;posts . " WHERE post_type = 'post' &amp;&amp; post_status = 'publish' ORDER BY comment_count DESC LIMIT 1";
$results = $wpdb-&gt;get_results($query);
$maxComments = $results[0]-&gt;comment_count;</code></pre>
<h3>Step 2: Loop through every single post</h3>
<p>All posts, from the first published to the latest published.</p>
<pre><code>query_posts('posts_per_page=-1&amp;order=ASC');

while (have_posts() ) : the_post();
  // output one line of graph
endwhile;</code></pre>
<h3>Step 3: Output a line for the graph</h3>
<p>Each line of the graph is going to be an anchor link, just so you can click it to go to that post should you wish. We&#8217;ll make those links <code>inline-block</code>, so we can set a <code>height</code> and <code>width</code>. This is the basics of the CSS:</p>
<pre><code>#chart { 
  padding: 100px 0; 
  height: 400px; 
  text-align: center; 
}
#chart &gt; a { 
  width: 1px;
  background: red;  
  vertical-align: bottom; 
  text-decoration: none;
}
#chart &gt; a:hover {
  background: black;
}</code></pre>
<p>Now within the loop, we&#8217;ll output those links (lines of the graph):</p>
<pre><code>$numComments = get_comments_number();
$heightPercentage = (($numComments / $maxComments) * 100);

echo "&lt;a href='";
echo get_permalink();
echo "' style='height: $heightPercentage%;'&gt;";
echo "&lt;span&gt;$numComments&lt;/span&gt;";
echo "&lt;/a&gt;";</code></pre>
<h3>Step 4: A little more&#8230;</h3>
<p>There are a couple more parts to this, like listing out the first and last year at the start and end of the chart (for context) and positioning the span with the number of comments at the top of the graph so you can view numbers on rollover.</p>
<p>I&#8217;m going to embed the code as a GitHub Gist here, so it&#8217;s easy to keep updated once all you smarties tell me all the stuff I&#8217;m doing wrong. This is built as a page template:</p>
<p><script src="https://gist.github.com/945619.js?file=gistfile1.php"></script></p>
<h3>The Graphs!</h3>
<p>Here&#8217;s the Google Analytics chart of CSS-Tricks since launch: </p>
<p><img src="http://digwp.com/wp-content/uploads/csstricksanalyticsgraph.png" alt="" title="csstricksanalyticsgraph" width="590" height="148" class="alignnone size-full wp-image-4269" /></p>
<p>And here&#8217;s the <a href="http://css-tricks.com/comment-graph/">comment graph</a> we just built:</p>
<p><img src="http://digwp.com/wp-content/uploads/csstricksgraph.png" alt="" title="csstricksgraph" width="590" height="188" class="alignnone size-full wp-image-4268" /></p>
<p>Kinda interesting. The charts definitely don&#8217;t follow each other like I would think they might. This isn&#8217;t enough data to infer why that is. You could say social media sites like Twitter have swallowed up some of comment activity. You could also say maybe the quality of the posts have gone down and inspire less comments. Who knows.</p>
<p>Here is <a href="http://digwp.com/comment-graph/">Digging Into WordPress&#8217;s graph</a>:</p>
<p><img src="http://digwp.com/wp-content/uploads/digwpgraph.png" alt="" title="digwpgraph" width="350" height="200" class="alignnone size-full wp-image-4273" /></p>
<h3>Share! Share!</h3>
<p>I&#8217;d love it, especially if you have a blog that&#8217;s been around a year or more, if you snagged this page template and linked up your own graph (or screenshot it) and posted it in the comments. </p>
<p>Maybe with a bunch of blogs graphs we could identify some trends. </p>
<hr />
<p><small>© 2011 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2011/04/graphing-blog-comments-over-time/">Permalink</a> | <a href="http://digwp.com/2011/04/graphing-blog-comments-over-time/#comments">15 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2011/04/graphing-blog-comments-over-time/&title=Graphing Blog Comments Over Time">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/comments/" rel="tag">comments</a>, <a href="http://digwp.com/tag/graph/" rel="tag">graph</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2011/04/graphing-blog-comments-over-time/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Display Total Number of Blogroll Bookmarks</title>
		<link>http://digwp.com/2010/11/show-total-number-blogroll-bookmarks/</link>
		<comments>http://digwp.com/2010/11/show-total-number-blogroll-bookmarks/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 16:32:56 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[blogroll]]></category>
		<category><![CDATA[bookmarks]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=3058</guid>
		<description><![CDATA[Quick WordPress tip for you today! A reader recently asked about displaying the total number of blogroll bookmarks on their site. This sounds simple enough but not everyone meddles with code these days, so here is a nice PHP snippet that will do the job: &#60;?php $numlinks = $wpdb-&#62;get_var("SELECT COUNT(*) FROM $wpdb-&#62;links WHERE link_visible = [...]]]></description>
			<content:encoded><![CDATA[<p>Quick WordPress tip for you today! A reader recently asked about displaying the total number of blogroll bookmarks <a href="http://www.blogschau.de/" title="blogschau.de">on their site</a>. This sounds simple enough but not everyone meddles with code these days, so here is a nice <abbr title="PHP: Hypertext Preprocessor">PHP</abbr> snippet that will do the job:</p>
<p><span id="more-3058"></span></p>
<pre><code>&lt;?php $numlinks = $wpdb-&gt;get_var("SELECT COUNT(*) FROM $wpdb-&gt;links WHERE link_visible = 'Y'");
if (0 &lt; $numlinks) $numlinks = number_format($numlinks); ?&gt;</code></pre>
<p>Place that in your theme file and then customize the output by placing <code>&lt;?php echo $numlinks; ?&gt;</code> wherever you would like to display the results.</p>
<p>A perfect example of this code in use can be seen at <a href="http://www.blogschau.de/" title="blogschau.de">this random bookmark site</a>.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/11/show-total-number-blogroll-bookmarks/">Permalink</a> | <a href="http://digwp.com/2010/11/show-total-number-blogroll-bookmarks/#comments">3 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/11/show-total-number-blogroll-bookmarks/&title=Display Total Number of Blogroll Bookmarks">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/blogroll-2/" rel="tag">blogroll</a>, <a href="http://digwp.com/tag/bookmarks/" rel="tag">bookmarks</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/2010/11/show-total-number-blogroll-bookmarks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Speed Up Your Blogging with WordPress Shortcodes</title>
		<link>http://digwp.com/2010/09/speed-up-blogging-wordpress-shortcodes/</link>
		<comments>http://digwp.com/2010/09/speed-up-blogging-wordpress-shortcodes/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 16:16:02 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[shortcodes]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://diggingintowordpress.com/?p=87</guid>
		<description><![CDATA[Save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog&#8217;s URL, you could place the following code your theme&#8217;s functions.php file: function shortURL() { return 'http://your-site.com/'; } add_shortcode('myurl', 'shortURL'); Now whenever you write a post in &#8220;HTML-mode&#8221;, you can include your blog&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog&rsquo;s <acronym title="Uniform Resource Locator">URL</acronym>, you could place the following code your theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>function shortURL() {
	return 'http://your-site.com/';
}
add_shortcode('myurl', 'shortURL');</code></pre>
<p>Now whenever you write a post in &ldquo;<acronym title="Hypertext Markup Language">HTML</acronym>-mode&rdquo;, you can include your blog&rsquo;s <acronym title="Uniform Resource Locator">URL</acronym> at breakneck speed by simply typing &ldquo;<code>[myurl]</code>&rdquo;. Works great in WordPress 2.5 or better.</p>
<p><span id="more-87"></span></p>
<p><strong>Shortcodes for your favorite links</strong><br />To create a shortcode for your favorite links, we need to include the <code>href</code> attribute information as well as the anchor text for the link itself. We can do this by placing this function in your theme&rsquo;s <code>functions.php</code> file:</p>
<pre><code>function shortLink($atts, $content = null) {
	extract(shortcode_atts(array(
		"href" =&gt; 'http://default-website.com' // default URL
	), $atts));
	return '&lt;a href="'.$href.'"&gt;'.$content.'&lt;/a&gt;';
}
add_shortcode('link', 'shortLink');</code></pre>
<p>Then, when creating a post, emulate the following format to include any links you wish:</p>
<p><code>[link href="http://your-site.com/"]Your Website![/link]</code></p>
<p>..which will output the following code:</p>
<p><code>&lt;a href="http://your-site.com/"&gt;Your Website!&lt;/a&gt;</code></p>
<p>When the <code>href</code> attribute is removed from the shortcode, the default <acronym title="Uniform Resource Locator">URL</acronym> will be used. You may specify the default <acronym title="Uniform Resource Locator">URL</acronym> in the third line of the function (see comment). This makes it easy to add a complete link to your site on the fly by simply typing:</p>
<p><code>[link]Your Website![/link]</code></p>
<p>One of the nice things about using shortcodes instead of hard-coded links is that you can easily change the default <abbr title="Uniform Resource Locator">URL</abbr> without breaking anything.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/09/speed-up-blogging-wordpress-shortcodes/">Permalink</a> | <a href="http://digwp.com/2010/09/speed-up-blogging-wordpress-shortcodes/#comments">6 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/09/speed-up-blogging-wordpress-shortcodes/&title=Speed Up Your Blogging with WordPress Shortcodes">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/shortcodes/" rel="tag">shortcodes</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/2010/09/speed-up-blogging-wordpress-shortcodes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WordPress Security Keys</title>
		<link>http://digwp.com/2010/09/wordpress-security-keys/</link>
		<comments>http://digwp.com/2010/09/wordpress-security-keys/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 07:42:37 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2571</guid>
		<description><![CDATA[In our recent post on pimping the wp-config.php file, we explain that using strong Security Keys is an important part of securing your WordPress installation. In this post, we want to zoom-in on Security Keys and look at what they are, how they work, and how to use them to greatly improve the security of [...]]]></description>
			<content:encoded><![CDATA[<p>In our recent post on <a href="http://digwp.com/2010/08/pimp-your-wp-config-php/" title="Pimp your wp-config.php">pimping the wp-config.php file</a>, we explain that using strong <strong>Security Keys</strong> is an important part of <a href="http://digwp.com/2009/11/how-to-secure-your-new-wordpress-installation/" title="How to Secure Your New WordPress Installation">securing your  WordPress installation</a>. In this post, we want to <em>zoom-in</em> on Security Keys and look at what they are, how they work, and how to use them to greatly improve the security of your site.</p>
<h3>Eight keys, one file, one step..</h3>
<p>In a nutshell, WordPress Security Keys refer to <strong>four authentication keys</strong> and <strong>four hashing salts</strong> (random bits of data) that work together to add an extra layer of security to your cookies and passwords. Security Keys exist as single-line definitions in your WordPress configuration file, the honorable <code>wp-config.php</code>.</p>
<p><span id="more-2571"></span></p>
<p>WordPress Security Keys work OK out-of-the-box, but require a bit of <em>customization</em> to make them <strong>super-strong</strong>. As of WordPress 3.0, there are <strong>eight security keys</strong>, introduced in the following versions:</p>
<ul>
<li><strong>WordPress 2.6</strong>: <code>AUTH_KEY</code>, <code>SECURE_AUTH_KEY</code>, <code>LOGGED_IN_KEY</code></li>
<li><strong>WordPress 2.7</strong>: <code>NONCE_KEY</code></li>
<li><strong>WordPress 3.0</strong>: <code>AUTH_SALT</code>, <code>SECURE_AUTH_SALT</code>, <code>LOGGED_IN_SALT</code>, <code>NONCE_SALT</code></li>
</ul>
<p>These eight Security Keys are located in your <a href="http://digwp.com/2009/06/wordpress-configuration-tricks/" title="WordPress Configuration Tricks">wp-config.php file</a> just after the database credentials. They have their own little section that looks like this:</p>
<pre><code>/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/</code></pre>
<p>As it says in the code comments, you want to replace these default keys with long sequences (60+) of random/unique characters. Each key needs to be <strong>completely random</strong> and different from the others. You can either do this manually or visit the online <a href="https://api.wordpress.org/secret-key/1.1/salt/" title="WordPress Secret Key Service">secret-key service</a> for automatic key-generation. When you visit the key service, you&rsquo;ll get something similar to this:</p>
<pre><code>define('AUTH_KEY',         'pK/7r(|,BK+X=|dzrK}Y|ttP%+8u&lt;$so#|`zUrA*RIxNSfgo$-w|UrQ#)RR8+DEz');
define('SECURE_AUTH_KEY',  ')|IL/&gt;/**t|;6/z,LMC3xs&gt;X5#+,?&gt;ZH#&gt;2F8ik|LziZhk+YogW&lt;h?n^O2W|_I?K');
define('LOGGED_IN_KEY',    'w3+SAaRLl]22i{|#-7&gt;76i&gt;qHV-P5d&lt;{Q3tRh5D2|&lt;L&gt;XKHpbD-I@(51Rd2W|Z#7');
define('NONCE_KEY',        'SPD.R?ynL?qf|NXW#n(jO%kmz=]_+n|DiHrN549u&gt;Ea{v!${-9lhoZ#.z7k(85n:');
define('AUTH_SALT',        '|6f&gt;K{-aje&lt;&lt;FUp{N(s-NOh-}/g&amp;+10/V]1Z7RP*IV6u3SRi-=M*Hf8:L$|.0Nwp');
define('SECURE_AUTH_SALT', 'mo&gt;b+| dr(mY??KSkZe[dOmuoAu|qla&lt;4q]&gt;=EY;6&amp;-YXt#:],T&lt;)0FJuc9FdwtG');
define('LOGGED_IN_SALT',   '*8u:v_n,-L`FJ+qyE*fm`kzw|G%m!B^J|!8?]kK?,#5vO2#*f~PL=|tw?Chg+{o)');
define('NONCE_SALT',       '7+%dZ!tcm{2K+l(JPL7d&lt;-B;.Jb7Cx.lj%9xQ|(ftY*|1+Qgl6q*m%L,n&lt;.8?WXI');</code></pre>
<p>Just copy/paste the entire block of code and replace the eight default keys with the eight random keys. With this step complete, your WordPress Security Keys are providing <em>strong security</em> for your site&rsquo;s cookies and passwords. Of course, there are many other ways to <a href="http://digwp.com/2009/07/optimize-wordpress-performance-with-the-wp-config-php-file/" title="Optimize WordPress Performance with the wp-config.php File">optimize your wp-config.php file</a> if you enjoy that sort of thing&nbsp;;)</p>
<h3>Some Notes on WordPress Security Keys and Salts</h3>
<p>So far we need to remember only <strong>one thing</strong>: replace the default Security Keys with random ones. Simple yes, but there are a few additional things to keep in mind when setting your keys:</p>
<ul>
<li>Security Keys may be changed (or added) at any time</li>
<li>Any logged-in users will need to log in again after changing your keys</li>
<li>The default set of eight Security Keys is also available in the <code>wp-config-sample.php</code> file</li>
<li>Never reveal your Security Keys to anyone &#8211; never post them online</li>
</ul>
<p>Also keep in mind that <code>wp-config.php</code> is normally <em>not modified</em> when updating WordPress. The <code>wp-config-sample.php</code> <strong>is</strong> replaced, so you can use it for reference if needed. If you&rsquo;re running WordPress 3.0 and see fewer than eight security keys, it&rsquo;s totally safe to replace what you have with a complete set. Other than logged-in users needing to log in again, your more-secure WordPress installation will roll on without skipping a beat.</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/09/wordpress-security-keys/">Permalink</a> | <a href="http://digwp.com/2010/09/wordpress-security-keys/#comments">9 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/09/wordpress-security-keys/&title=WordPress Security Keys">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/config/" rel="tag">config</a>, <a href="http://digwp.com/tag/security/" rel="tag">Security</a>, <a href="http://digwp.com/tag/tips/" rel="tag">tips</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/09/wordpress-security-keys/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Pimp your wp-config.php</title>
		<link>http://digwp.com/2010/08/pimp-your-wp-config-php/</link>
		<comments>http://digwp.com/2010/08/pimp-your-wp-config-php/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 18:52:39 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=2416</guid>
		<description><![CDATA[Easily, the most important file in your WordPress installation is the wp-config.php file. It serves as your site&#8217;s base configuration file, controlling key aspects of WordPress&#8217; functionality and enabling WordPress to do mission-critical stuff like connect to the database. Without wp-config.php, WordPress simply won&#8217;t work. So whenever you install WordPress, one of the first things [...]]]></description>
			<content:encoded><![CDATA[<p>Easily, the most <em>important</em> file in your WordPress installation is the <code>wp-config.php</code> file. It serves as your site&rsquo;s <strong>base configuration file</strong>, controlling <em>key aspects</em> of WordPress&rsquo; functionality and enabling WordPress to do mission-critical stuff like connect to the database. Without <code>wp-config.php</code>, WordPress simply won&rsquo;t work. So whenever you install WordPress, one of the <em>first</em> things to do is pimp your <code>wp-config.php</code>.</p>
<p>And it&rsquo;s pretty easy too &ndash; just get your database credentials in place and you&rsquo;re done. The other settings available in the <code>wp-config.php</code> file will work just fine using the default values, but there are some <em>cool things</em> you can do to customize functionality, tighten security, and improve performance. Once you get the basics of <code>wp-config.php</code>, you can really <em>pimp it out</em> to do some awesome stuff. We&rsquo;ll break this down into <strong>four basic parts</strong>:</p>
<ul>
<li><a href="http://digwp.com/2010/08/pimp-your-wp-config-php/#basics" title="There is no wp-config.php..">Basics</a></li>
<li><a href="http://digwp.com/2010/08/pimp-your-wp-config-php/#security" title="Protect wp-config.php">Security</a></li>
<li><a href="http://digwp.com/2010/08/pimp-your-wp-config-php/#settings" title="Main Configuration Settings">Main Settings</a></li>
<li><a href="http://digwp.com/2010/08/pimp-your-wp-config-php/#pimpin" title="Pimp it out!">Additional Tips and Tricks</a></li>
</ul>
<p><span id="more-2416"></span></p>
<h3 id="basics">There is no wp-config.php..</h3>
<p>When you first download WordPress, the <code>wp-config.php</code> file isn&rsquo;t included. Instead, you get a file named &ldquo;<code>wp-config-sample.php</code>&rdquo; (located in the root install-directory) that contains everything you need. Just rename the file to &ldquo;<code>wp-config.php</code>&rdquo; and delete the <code>wp-config-sample.php</code> file from the server if it&rsquo;s already there. Here is a visual:</p>
<p><img src="http://digwp.com/wp-content/blog-images/wp-config-01.jpg" alt="[ Rename the file ]" /></p>
<p>With the <code>wp-config.php</code> file now ready to go, it&rsquo;s time to protect it..</p>
<h3 id="security">Protect wp-config.php</h3>
<p>The first thing you want to do with <code>wp-config.php</code> is protect it. Here is a great way to secure the file using .htaccess:</p>
<pre><code>&lt;Files wp-config.php&gt;
	Order Allow,Deny
	Deny from all
&lt;/Files&gt;</code></pre>
<p>This code should be placed in an .htaccess file located in the directory that contains your <code>wp-config.php</code> file.</p>
<p><img src="http://digwp.com/wp-content/blog-images/wp-config-02.jpg" alt="[ Use the same directory ]" /></p>
<p>Once the .htaccess file is in place, ensure that permissions are <abbr title="change mode">chmod</abbr> <strong>640</strong> for both files. This setting returns a &ldquo;403 Forbidden&rdquo; error to all external requests. Combining proper file permissions with .htaccess protection is an excellent way to <em>secure</em> your <code>wp-config.php</code>.</p>
<h3 id="settings">Main Configuration Settings</h3>
<p>Now that <code>wp-config.php</code> is secure, it&rsquo;s time to add the required database information and then customize for optimum performance. This section covers the first four configuration settings that you&rsquo;ll find <em>already included</em> in your <code>wp-config.php</code>. Then in the next section, we&rsquo;ll explore some awesome techniques and really pimp things out.</p>
<h4>1) Database credentials</h4>
<p>The first and only <em>required</em> step is to add your database credentials. This should appear after the PHP comments near the top of the file:</p>
<pre><code>// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');</code></pre>
<p>For most servers, you&rsquo;ll need only the first three bits of information: database name, username and password. If WordPress can&rsquo;t connect after that, then you may need to change the hostname to whatever your host tells you. Only fiddle with the last two items &ndash; charset and collate &ndash; if you have need and know what you&rsquo;re doing.</p>
<h4>2) Authentication Unique Keys and Salts</h4>
<p>Next up is a section for key/salt definitions. By default it looks like this:</p>
<pre><code>/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/</code></pre>
<p>The eight definitions should be replaced with values generated via the official <a href="https://api.wordpress.org/secret-key/1.1/salt/">WordPress.org secret-key service</a>. Visit that link, refresh the page a few times, and paste the results into place, like so:</p>
<pre><code>/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         ')` 4yDGc w=*:l8@RD167k0+/+@+rgo(mI-x&lt;_~.=WtCm[qD8&lt;3S)TsR}K~bMLNT');
define('SECURE_AUTH_KEY',  'zO?htRkV.k[vBqn&lt;+.,lorpObkc?@|Vi+8`w=A9,|FjPB&amp;5`5|9&lt;[&amp;&amp;WaUB.pwTS');
define('LOGGED_IN_KEY',    'jtr|J-/.-:6CVh*8h.~bNzc3^#A9@hB9G3E/fx/&gt;k)pmTHbES5^Rq(+EINCW w&gt;8');
define('NONCE_KEY',        'O2Vz+br+`6MqQVv1{-g}sy -CF8M/tldEyWV[W}dnebd7m$6.P,m+.G6Ec]{V@Kl');
define('AUTH_SALT',        '&gt;x,, 8wMLIJCH}i94Ib+}~lR%r_))x@LU3~YJwCok++xaSVE@[My(zAg!Fpi4{NR');
define('SECURE_AUTH_SALT', '7[,gAJ#TQmsw:$!R-n-r%4&lt;UvG7%|-7&amp;jt4]XS-[KX&amp;O)|H,err]&lt;{EuFnmP3,M}');
define('LOGGED_IN_SALT',   '9dJP8uW2E2&amp;.^a|@I|[?qbY%z:&amp;jgnH&lt;OUg+t6Tks,Hox=M@~yx+b;~zU)436q=+');
define('NONCE_SALT',       '=Xv~8+!&gt;l:wy`~w0U-6wO2lmG8l5Xg21$J59T$T~)(h5m&lt;5`|/|dVN{j[80QMV60');

/**#@-*/</code></pre>
<p>Don&rsquo;t use the example keys shown here though &ndash; the whole idea is to specify <em>unique</em> phrases to <em>improve</em> security. And it&rsquo;s totally fine to replace these keys at any time &ndash; the worst that will happen is that currently logged-in users will need to log in again. </p>
<p>At this point, we have our database credentials and secret keys all ready to go. Next we want to <em>further improve security</em> by specifying a unique database prefix.</p>
<h4>3) WordPress Database Table prefix</h4>
<p>WordPress is a <em>huge</em> target for <a href="http://digwp.com/2010/07/wordpress-security-lockdown/#pharma-hack" title="WordPress Security Lockdown">malicious scripts</a>, <a href="http://digwp.com/2010/07/media-temple-wordpress-hack/" title="Media Temple WordPress Hack">hacks</a>, and <a href="http://digwp.com/2009/06/spam-link-injection-hacked/" title="Spam Link Injection Hacked (and How I Hopefully Fixed It)">spam</a>. One of the best ways to secure your WordPress database is to change the default table prefix. The default value, as seen below, is &ldquo;<code>wp_</code>&rdquo;:</p>
<pre><code>/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';
</code></pre>
<p>This works, but as the default value, it is heavily targeted by <a href="http://perishablepress.com/press/2009/12/22/protect-wordpress-against-malicious-url-requests/" title="Protect WordPress Against Malicious URL Requests">malicious scripts</a> and <a href="http://perishablepress.com/press/2010/07/14/blackhole-bad-bots/" title="Protect Your Site with a Blackhole for Bad Bots">bad bots</a>. Changing it to something <em>unique</em> essentially <em>immunizes</em> it against automated attacks on anything prefixed with <code>wp_</code>. The more <em>random and unique</em> the better, like a password: &ldquo;<code>h7G3vcDEo3jDf_</code>&rdquo; would be a good example. Note that ending the string with an underscore or some other easily recognizable character is a good way to keep things readable and easy to use.</p>
<h4>4) WordPress Localized Language</h4>
<p>By default WordPress uses the English language, but you can use any available language by following <a href="http://codex.wordpress.org/Installing_WordPress_in_Your_Language" title="Installing WordPress in Your Language">these steps</a>. As a part of that process, you&rsquo;ll specify your language in this part of the <code>wp-config.php</code> file:</p>
<pre><code>/**
 * WordPress Localized Language, defaults to English.
 *
 * Change this to localize WordPress.  A corresponding MO file for the chosen
 * language must be installed to wp-content/languages. For example, install
 * de.mo to wp-content/languages and set WPLANG to 'de' to enable German
 * language support.
 */
define ('WPLANG', '');</code></pre>
<p>It&rsquo;s pretty straightforward: if nothing is specified (as is seen here), English is used; otherwise, you include the <code>.mo</code> language file you are using for the translation.</p>
<h3 id="pimpin">Additional Tips and Tricks</h3>
<p>Now that you&rsquo;ve got the database connection, security keys, table prefix, and language definition dialed in you&rsquo;re ready to rock. Most WordPress sites are in great shape at this point, but there is <em>much</em> more that we can do with <code>wp-config.php</code> to maximize performance and make our lives easier.</p>
<h4>Pimping Post Revisions</h4>
<p>Recent versions of WordPress provides a post-revisioning system that enables users to save different versions of their blog posts and even revert to previously saved versions if necessary. Regardless of how much you do or do not despise this amazingly awesome feature, here are a couple of configurational definitions that may prove useful for you:</p>
<pre><code>// Limit the number of saved revisions
define('WP_POST_REVISIONS', 3); // any integer, but don't go crazy

// Disable the post-revisioning feature
define('WP_POST_REVISIONS', false); // kill the bloat</code></pre>
<h4>Specify the Autosave Interval</h4>
<p>In a similar vein as the post-revisioning feature is WordPress’ actually useful Autosave functionality. By default, WordPress saves your work every 60 seconds, but you can totally modify this setting to whatever you want. Don’t get too crazy though, unless you want to stress-out your server&nbsp;;)</p>
<pre><code>define('AUTOSAVE_INTERVAL', 160); // in seconds, don't go nuts</code></pre>
<h4>Automated Trash</h4>
<p>Since WordPress 2.9, we&rsquo;ve had the &ldquo;Trash&rdquo; feature to help prevent accidents. So now instead of deleting stuff like posts and comments, you send them to the Trash. By default WordPress deletes the Trash every 30 days, but you can set it to whatever you want by adding a line like this to <code>wp-config.php</code>:</p>
<pre><code>define('EMPTY_TRASH_DAYS', 7); // empty weekly</code></pre>
<p>To ride bareback without the safety net, you can disable the Trash feature by specifying a zero value:</p>
<pre><code>define('EMPTY_TRASH_DAYS', 0); // disable trash</code></pre>
<p>By disabling Trash, you&rsquo;ll be deleting stuff permanently the first time, just like way back in the day.</p>
<h4>Automatic Database Repair</h4>
<p>WordPress 2.9 also gave us automatic database repair, which enables you to repair and optimize your database even when not logged in. This functionality should be used on an <em>as-needed</em> basis by first adding the following snippet to <code>wp-config.php</code>:</p>
<pre><code>define('WP_ALLOW_REPAIR', true);</code></pre>
<p>With that in place, visit the following <abbr title="Uniform Resource Locator">URL</abbr> to open the &ldquo;Database Repair&rdquo; page:</p>
<p><code>http://example.com/wp-admin/maint/repair.php</code></p>
<p>There you&rsquo;ll be able to optimize and repair your database without needing to log in to WordPress. Important: While you have  <code>WP_ALLOW_REPAIR</code> set in <code>wp-config.php</code>, the Database Repair page is openly accessible by anyone who finds it. So definitely remove the line to disable the auto-repair functionality after you are done using it.</p>
<h4>Block External Requests</h4>
<p>If you need to prevent WordPress from making external requests, add this snippet to <code>wp-config.php</code>:</p>
<pre><code>define('WP_HTTP_BLOCK_EXTERNAL', true);</code></pre>
<p>This will prevent things from happening that normally happen, like updates, dashboard feeds, and data reporting. Fortunately, it&rsquo;s easy to whitelist (<em>allow</em> access) anything that is needed. Here is an example where we grant access to pingomatic.com:</p>
<pre><code>define('WP_ACCESSIBLE_HOSTS', 'rpc.pingomatic.com');</code></pre>
<h4>Blog Address and Site Address</h4>
<p>By default, these two configurational definitions are not included in the <code>wp-config.php</code> file, but they may be added to improve performance. These two settings were introduced in WordPress version 2.2 and override the database value without actually changing them. Example:</p>
<pre><code>define('WP_HOME', 'http://digwp.com'); // no trailing slash
define('WP_SITEURL', 'http://digwp.com');  // no trailing slash</code></pre>
<p>These settings should match those specified in your WordPress Admin. Once you set them in <code>wp-config.php</code>, they will be &ldquo;grayed-out&rdquo; when displayed in the Admin.</p>
<h4>Debugging WordPress</h4>
<p>Since WordPress version 2.3.1, users have been able to display certain errors and warnings to help with the debugging of their site. As of WordPress version 2.5, enabling error reporting raises the reporting level to <code>E_ALL</code> and activates warnings for deprecated functions. By default (i.e., if no definition is specified in the <code>wp-config.php</code> file), error reporting is disabled.</p>
<pre><code>define('WP_DEBUG', true); // debugging mode: 'true' = enable; 'false' = disable</code></pre>
<p>Adding that snippet to <code>wp-config.php</code> tells WordPress to display warnings and error messages with your web pages. This functionality is <em>extremely useful</em> but infrequently used by plugin and theme developers. If you&rsquo;ve never enabled debugging, you&rsquo;re in for a surprise &ndash; lots of errors even with some of the most popular plugins.</p>
<h4>Error Log Configuration</h4>
<p>Here is an easy way to enable basic error logging for your WordPress-powered site. Create a file called <code>php_error.log</code>, make it server-writable, and place it in the directory of your choice. Then edit the path in the third line of the following code and place into your <code>wp-config.php</code>:</p>
<pre><code>@ini_set('log_errors','On');
@ini_set('display_errors','Off');
@ini_set('error_log','/home/path/domain/logs/php_error.log');</code></pre>
<p>Error logs are powerful tools for keeping an eye on things and expediently resolving issues. It&rsquo;s awesome that WordPress makes this so easy. We cover this in greater depth along with a couple of other methods in <a href="http://digwp.com/2009/07/monitor-php-errors-wordpress/" title="3 Ways to Monitor PHP Errors">this post</a>.</p>
<h4>Increase PHP Memory</h4>
<p>If you are receiving error messages telling you that your &ldquo;Allowed memory size of xxx bytes exhausted,&rdquo; this setting may help resolve the issue. As of WordPress version 2.5, the <code>WP_MEMORY_LIMIT</code> definition enables you to specify the maximum amount of memory that may be used by PHP. Here are some examples:</p>
<pre><code>define('WP_MEMORY_LIMIT', '64M');
define('WP_MEMORY_LIMIT', '96M');
define('WP_MEMORY_LIMIT', '128M');</code></pre>
<p>By default, WordPress will automatically attempt to increase PHP memory up to 32MB, so this setting is only needed for values higher than 32MB. Note that some web hosts disable your ability to increase PHP memory, so you may need to ask (or beg) for them to do it.</p>
<h3>More info, tips and tricks for wp-config</h3>
<p>For more information on the <code>wp-config.php</code> file, <a href="http://codex.wordpress.org/Editing_wp-config.php" title="Editing wp-config.php">check out the WordPress Codex</a>. We also have some <a href="http://digwp.com/2009/06/wordpress-configuration-tricks/" title="WordPress Configuration Tricks">awesome wp-config tricks</a> and <a href="http://digwp.com/2009/07/optimize-wordpress-performance-with-the-wp-config-php-file/" title="Optimize WordPress Performance with the wp-config.php File">wp-config optimization tips</a> for your WordPress enjoyment :)</p>
<hr />
<p><small>© 2010 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2010/08/pimp-your-wp-config-php/">Permalink</a> | <a href="http://digwp.com/2010/08/pimp-your-wp-config-php/#comments">44 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2010/08/pimp-your-wp-config-php/&title=Pimp your wp-config.php">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/config/" rel="tag">config</a>, <a href="http://digwp.com/tag/database/" rel="tag">database</a>, <a href="http://digwp.com/tag/performance/" rel="tag">performance</a>, <a href="http://digwp.com/tag/security/" rel="tag">Security</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2010/08/pimp-your-wp-config-php/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
	</channel>
</rss>

