<?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>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Getting Comment Info from the WordPress Database</title>
		<link>http://digwp.com/2012/03/getting-comment-info-wordpress-database/</link>
		<comments>http://digwp.com/2012/03/getting-comment-info-wordpress-database/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 17:16:45 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://digwp.com/?p=5861</guid>
		<description><![CDATA[An easy way for visitors to enter their emails is by commenting on a post. We did this recently for people to sign up for a notification email. Instead of using a plugin or custom function for a one-time email list, we just went with WordPress core functionality and used post comments for people to [...]]]></description>
			<content:encoded><![CDATA[<p>An easy way for visitors to enter their emails is by commenting on a post. We did this recently for people to <a href="http://digwp.com/2012/02/notification-list-3-3-printed-books/">sign up for a notification email</a>. Instead of using a plugin or custom function for a one-time email list, we just went with WordPress core functionality and used post comments for people to sign up. Then the trick then is retrieving the comment information from the database for the specific sign-up post. </p>
<p>We did this recently to collect commentators&#8217; email addresses, but could have easily extracted other comment info as well &mdash; comment author, comment date, comment url, and basically anything in the <code>wp_comments</code> table, shown here:</p>
<p><span id="more-5861"></span></p>
<div class="post-box-201203"><img src="http://digwp.com/wp-content/uploads/2012/03/getting-comment-info.jpg" alt="getting-comment-info" width="245" height="428" /><br /><em>columns in the <code>wp_comments</code> table</em></div>
<p>You can easily display and collect any of this information for any specific page or post on your site. All you need is a non-public page (or other theme location) to output the results (&#8220;non-public&#8221; especially if you&#8217;re displaying any email data). In our case we just created a new private page and selected our custom page template. Load the page and <em>viola!</em> &mdash; instant list of all comment author emails for our sign-up post.</p>
<h3>Getting comment info from the database</h3>
<p>So you&#8217;ve got your sign-up post with some comments, and now want to collect the information and send some emails or whatever. To get the information, we need to query the WP database, select our columns from the <code>wp_comments</code> table, and then display the results on our custom page. </p>
<p>For the SQL query, getting data from the comment table is straightforward, but doing so for a <em>specific post</em> requires a dash of voodoo found in an update on <a href="http://webtrickz.com/how-to-extract-commentators-email-address-with-ip-name-from-a-post-in-wordpress-featured/" title="How to Extract commentators Email address with IP &#038; Name from a post in WordPress">this post</a>. To make a long story short, you have to use nested queries with an arbitrary &#8220;AS WHATEVER&#8221; added at the end, as such:</p>
<pre><code>SELECT DISTINCT comment_author, comment_author_email, comment_author_IP 
FROM ( 
SELECT DISTINCT comment_author, comment_author_email, comment_author_IP 
FROM wp_comments WHERE comment_post_ID = 1
) AS WHATEVER</code></pre>
<p>The &#8220;WHATEVER&#8221; is essentially meaningless, so use any name you want. Why? Apparently the &#8220;AS&#8221; clause is required for the nested (or whatever) queries to work their magic. As you can see, this enables us to grab <em>any column</em> from the <code>wp_comments</code> table. In the example query, we&#8217;re selecting the <code>comment_author</code>, <code>comment_author_email</code>, and <code>comment_author_IP</code> columns.</p>
<p>If you have access to the database, you can use a program such as <a href="http://www.phpmyadmin.net/">phpMyAdmin</a> to execute the above query directly. Otherwise, we&#8217;ll go with the WordPress custom-private-page route. Open your page template and add the following code beneath <code>the_content()</code> template tag:</p>
<pre><code>&lt;?php //grab the data
$comment_info = $wpdb-&gt;get_results("SELECT DISTINCT comment_author, comment_author_email, comment_author_IP 
	FROM (SELECT DISTINCT comment_author, comment_author_email, comment_author_IP 
	FROM wp_comments 
	WHERE comment_post_ID = 1
	) AS WHATEVER"); 
// display the results
echo '&lt;ul&gt;';
foreach($comment_info as $info) { 
	echo '&lt;li&gt;&lt;strong&gt;'. $info-&gt;comment_author .'&lt;/strong&gt; - '. $info-&gt;comment_author_email .' - &lt;small&gt;'. $info-&gt;comment_author_IP .'&lt;/small&gt;&lt;/li&gt;'; 
}
echo '&lt;/ul&gt;';
?&gt;</code></pre>
<p>Just pick your post ID and done. When you visit the custom page in a browser, you should see the results of your query displayed as a list, similar to this:</p>
<ul>
<li><strong>Juan Gris</strong> &#8211; <code>juan@hotmail.com</code> &#8211; <small>123.456.789</small></li>
<li><strong>Max Ernst</strong> &#8211; <code>max@gmail.com</code> &#8211; <small>987.654.321</small></li>
<li><strong>Salvador Dali</strong> &#8211; <code>dali@email.com</code> &#8211; <small>456.789.123</small></li>
</ul>
<p>But no need to keep it list format, with a little tweaking, we can output any data using whatever markup works best. For example, to just grab the emails from a nice <code>&lt;pre&gt;</code> list, change the <code>foreach</code> loop to this:</p>
<pre><code>echo '&lt;pre&gt;';
foreach($comment_info as $info) { 
	echo $info-&gt;comment_author_email . "\n";
}
echo '&lt;/pre&gt;';</code></pre>
<p>..and that should give you just the data, with no interfering markup:</p>
<pre><code>juan@hotmail.com
max@gmail.com
dali@email.com</code></pre>
<h3>Customizing</h3>
<p>There are two ways to customize this technique. In the query itself, you can specify which columns you want to display. And then you can also customize the markup, to format the data to suit your specific needs. Sort of a <em>multipurpose</em> method for grabbing post-specific info from the database.</p>
<hr />
<p><small>© 2012 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2012/03/getting-comment-info-wordpress-database/">Permalink</a> | <a href="http://digwp.com/2012/03/getting-comment-info-wordpress-database/#comments">5 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2012/03/getting-comment-info-wordpress-database/&title=Getting Comment Info from the WordPress Database">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/comments/" rel="tag">comments</a>, <a href="http://digwp.com/tag/database/" rel="tag">database</a>, <a href="http://digwp.com/tag/functions/" rel="tag">functions</a>, <a href="http://digwp.com/tag/sql/" rel="tag">sql</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2012/03/getting-comment-info-wordpress-database/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<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>
	</channel>
</rss>

