<?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>Refactord</title>
	<atom:link href="http://www.refactord.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.refactord.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 26 Jun 2011 20:02:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Create A Skewed Title With CSS and Jquery</title>
		<link>http://www.refactord.com/create-a-skewed-title-with-css-and-jquery?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-a-skewed-title-with-css-and-jquery</link>
		<comments>http://www.refactord.com/create-a-skewed-title-with-css-and-jquery#comments</comments>
		<pubDate>Sun, 26 Jun 2011 17:21:57 +0000</pubDate>
		<dc:creator>Kyle G</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Typography]]></category>
		<category><![CDATA[CSS 3]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.refactord.com/?p=133</guid>
		<description><![CDATA[Web typography sometimes seems limiting, but using jQuery combined with CSS 3 will you the ability to create something that breaks from the norm. The effect is to use jQuery and a plugin call Letting JS to wrap elements (letters) with a unique class and target each with unique styling. The styling is CSS 3; [...]]]></description>
			<content:encoded><![CDATA[<p>Web typography sometimes seems limiting, but using jQuery combined with CSS 3 will you the ability to create something that breaks from the norm.  The effect is to use jQuery and a plugin call Letting JS to wrap elements (letters) with a unique class and target each with unique styling.  The styling is CSS 3; taking advantage of the transform property.  This will allow us to move elements (letters) along the X and Y axis as well as skewing them around the Y axis.  </p>
<h2>Lettering JS Overview</h2>
<p>If you visit the <a href="http://letteringjs.com/">lettering.js</a> website, you will find the title of the site is pure type and is a great example of what this plugin can do for you.  It allows you to individually target either letters, words, or lines of content by assigning classes to each.  The title on this site uses Lettering JS to individually target each character in the title allowing individual styling be applied to each.</p>
<h2>CSS 3 transform</h2>
<p>The CSS 3 transform property gives you the ability to: rotate elements, move elements along the X and Y axis, transform elements via a transformation matrix, scaling, and skewing around the X and Y axis.  This property is recognized by most modern browsers and is <a href="http://msdn.microsoft.com/en-us/ie/ff468705#_CSS3_2D_Transforms" target="_blank">supported</a> by IE 9.  </p>
<p>When using transform, you have the task of targeting each browser to ensure you style will be applied.  Here is a quick look at one of the ways from the example that transform is used:</p>
<pre name='code' class="css">
.char1 {
    -webkit-transform: translate(0px, -13px) skewY(-15deg);
    -o-transform: translate(0px, -13px) skewY(-15deg);
    -moz-transform: translate(0px, -13px) skewY(-15deg);
    -ms-transform: translate(0px, -13px) skewY(-15deg);
    transform: translate(0px, -13px) skewY(-15deg);
}</pre>
<p>There are 5 different methods to ensure translate will be supported in the popular browsers; these are Webkit (-webkit-transform:), Opera (-o-transform), Mozilla Firefox (-moz-transform), IE (-ms-transform), and a general transform.  The values for transform we use are translate and skewY and need more explanation.  </p>
<p>Translate move elements along the X and Y axis.  These values are surrounded by parenthesis with the first value is for the X axis and the second is the Y axis.  In the example we use a value of -13px on the y axis which moves the element up, the opposite to a regular XY graph.  </p>
<p>SkewY rotates an element around the Y axis and the value is the degree the element will be rotated.  There is a general skew we could also use and it accepts values similar to translate, but there is no need because the Y axis is all we will need to create the look.   </p>
<h2>Creating the Skewed Title</h2>
<p>To begin, we need to create a HTML document and include the jQuery library.  I am also going to include an external style sheet and create a H1 title call &#8220;skew example&#8221; with an ID of &#8220;skew-title&#8221;.  Here is the code:</p>
<pre name='code' class='html'>
&lt;!doctype html&gt;
&lt;html lang=en&gt;
&lt;head&gt;
    &lt;ink href="css/syle.css" rel="stylesheet" media="all" /&gt;
    &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">&lt;/script&gt;
    &lt;title>Skew Example&lt;/title&gt;
    &lt;meta charset=utf-8&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1 id="skew-title">Skew Example&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3>Adding Lettering JS</h3>
<p>Next, we need to download lettering JS and include it in our document.  Then we use javascript to get &#8220;skew-title&#8221; and call the lettering function.  By default this wraps each character in a span assigning it a class of &#8220;char&#8221; plus the number of the character.  For example the first letter in the title would have the class of &#8220;char1&#8243; representing that is is the first character in the string.  These new classes created by lettering JS will allow us to style each character individually.  Here is the code:</p>
<pre name='code' class='html'>
&lt;html lang=en&gt;>
&lt;head&gt;
    &lt;link href="css/syle.css" rel="stylesheet" media="all" /&gt;
    &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/jquery.lettering.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
        $(document).ready(function() {
            $('#skew-title').lettering();
        });
    &lt;/script&gt;

    &lt;title&gt;Skew Example&lt;/title&gt;
    &lt;meta charset=utf-8&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1 id="skew-title"&gt;Skew Example&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If we take a closer look at the result of calling Lettering JS on our title, we will find that each letter is now wrapped in a span.  Each span has a class of <strong>char</strong> plus what ever number character it is.  Here is the result:</p>
<p><img src="http://www.refactord.com/wp-content/uploads/2011/06/skew-example.jpg" alt="" title="skew-example" width="500" height="300" class="alignnone size-full wp-image-162" alt='lettering js result' /></p>
<h3>Creating the CSS</h3>
<p>To begin I just create some general styling of the H1 tag, giving it a certain font size and text-transform of uppercase.</p>
<pre name='code' class='css'>
h1 {
    font-size: 65px;
    text-transform: uppercase;
}
</pre>
<h3>CSS Transform Methods</h3>
<p>To create the skew on just the first letter, we are going to use the span class assigned by Lettering JS to target the first letter, char1.  Here is the code from the example:</p>
<pre name='code' class='css'>
.char1 {
    -webkit-transform: translate(0px, -15px) skewY(-15deg);
    -o-transform:translate(0px, -13px) skewY(-15deg);
    -moz-transform:translate(0px, -13px) skewY(-15deg);
    -ms-transform:translate(0px, -13px) skewY(-15deg);
    transform: translate(0px, -13px) skewY(-15deg);
    display: inline-block;
    text-align: center;
    background: #5E6553;
    border: 1px solid #B6B9B0;
    color:#B6B9B0;
    width: 65px;
}
</pre>
<p>Reviewing the code, you will find the translate and skew methods from above but we have added more styling.  We added color to the font, gave it a background color, width, aligned the text to the center, and gave it a border.  One other property was used to change the display to inline-block.  <strong>This is required to make each letter function like a block and without the styling would not properly be applied. </strong> </p>
<h3>Styling the rest</h3>
<p>After style the first letter you should be able to see how to style the rest.  The best thing to actually do is to play with it.  Try different scaling and skews to create something unique.  Thanks for reading.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.refactord.com/create-a-skewed-title-with-css-and-jquery/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating The Author Tag Cloud</title>
		<link>http://www.refactord.com/creating-the-author-tag-cloud?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-the-author-tag-cloud</link>
		<comments>http://www.refactord.com/creating-the-author-tag-cloud#comments</comments>
		<pubDate>Wed, 15 Jun 2011 05:44:12 +0000</pubDate>
		<dc:creator>Kyle G</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Plugin]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[WordPress Rewrite Rules]]></category>

		<guid isPermaLink="false">http://www.refactord.com/?p=108</guid>
		<description><![CDATA[I just released this sites first plugin: Author Tag Cloud, which create a tag cloud by an author containing the tags he or she used on their posts. Beyond that it creates a new permalink structure to drill down into content and find post based on author and tag. It also creates a template tag [...]]]></description>
			<content:encoded><![CDATA[<p>I just released this sites first plugin: <a href="http://wordpress.org/extend/plugins/authors-tag-cloud/">Author Tag Cloud</a>, which create a tag cloud by an author containing the tags he or she used on their posts.  Beyond that it creates a new permalink structure to drill down into content and find post based on author and tag.  It also creates a template tag that creates the author tag cloud.  Enough about what it does I think we should see how it actually works!  </p>
<h2>Why create this plugin</h2>
<p>One of the reason why I created this plugin is I read several blogs some which I return to reread a post.  But when I return to the site, I find that there has been several posts since I last visited or it has been a year or so since I read it.  Well, to be honest I am an idiot and probably should of bookmarked it.  Here is the deal, I might know the author or topic of the post but will have to dig, going back several pages will I find what I am looking for.  </p>
<p>Since I am lazy and would like some other way to drill down, mainly I know the author and the topic, I figured why not have WordPress do that for me.  </p>
<h2>Where to Start to create author tag cloud</h2>
<p>To begin, I created a new plugin folder in my <strong>wp-content/plugins/</strong> folder and named it <strong>author-tag-cloud</strong>.  I then create a new PHP document named <strong>author-tag-cloud.php</strong>.  After that I created the <a href="http://codex.wordpress.org/Writing_a_Plugin#Standard_Plugin_Information">standard information</a> in that file.  I am going to create a new class called <strong>author_tag_cloud</strong> which I first to see if it exists already, which is not a bad practice as there could be a remote possibility someone else developing on WordPress is using that class name.  I create the class and then call a new instance of the class.  Here is the code so far:  </p>
<pre name='code' class='php'>
/*
Plugin Name: Author Tag Cloud
Plugin URI:
Description: my description
Author: Kyle G
Author URI :
Version: 1.0
*/

if(!class_exists('Author_tag_cloud')){
    class Author_tag_cloud {

    }
    $author_tag_cloud = new Author_tag_cloud;
}
</pre>
<h2>Adding the new permalink structure</h2>
<p>This is where the post on <a href="http://www.refactord.com/adding-rewrite-rules-to-wordpress">adding permalinks to WordPress</a> will come in handy becuase I want to create a new permalink structure that looks like:</p>
<p><strong>http://www.mysite.com/author/author-slug/tag/tag-slug</strong></p>
<p>But this also needs to accept paging.  For example what if the author has 100 posts with a specific tag and the setting only allow for 10 to show on a given page.  This mean there are pages and we must account for this in our permalink structure.  </p>
<p>To create this new structure I need to import the class from the previous post.  I drop that into a new folder called includes.  I then need to create a regular expression and query string for how it will work.  I will not go into detail how I created it, that is completely separate topic and here is the rules I am going to create:</p>
<pre name='code' class='php'>
'rules' => array(
                    'tag/([^/]+)/author/([^/]+)/?$' => 'index.php?tag=$matches[1]&#038;author_name=$matches[2]',
                    'tag/([^/]+)/author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&#038;author_name=$matches[2]&#038;paged=$matches[3]',
                    'author/([^/]+)/tag/([^/]+)/?$' => 'index.php?author_name=$matches[1]&#038;tag=$matches[2]',
                    'author/([^/]+)/tag/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&#038;tag=$matches[2]&#038;paged=$matches[3]'
                    )
</pre>
<p><strong>Remember Every Regular Expression in Parentheses Is a Query String Match and if are looking to create your own query strings you should know the query vars available to you. </strong></p>
<p>Now that I have the rules done, I going to create a constructor function in my class and a new method call add_rules().  Also in the constructor function, I am going to call this method add_rules().  You could do this in the constructor but I just prefer to do it this way.  I import my class that I added to my includes files and call a new instance of my class passing it these rules.  Take a look at the constructor method and add_rules methods:</p>
<pre name='code' class='php'>
function  __construct() {
            $this->add_rules();
        }

        function add_rules(){

            include('includes/refactord-add-rewrite-rules.php');

            $options = array(
                'rules' => array(
                    'tag/([^/]+)/author/([^/]+)/?$' => 'index.php?tag=$matches[1]&#038;author_name=$matches[2]',
                    'tag/([^/]+)/author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&#038;author_name=$matches[2]&#038;paged=$matches[3]',
                    'author/([^/]+)/tag/([^/]+)/?$' => 'index.php?author_name=$matches[1]&#038;tag=$matches[2]',
                    'author/([^/]+)/tag/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&#038;tag=$matches[2]&#038;paged=$matches[3]'
                    )
            );

            $add_rewrite_rules = new Refactord_add_rewrite_rules($options);
</pre>
<p><strong>If you activate this plugin, these new permalink structures will be working.  Pretty easy right!</strong></p>
<h2>Creating the template tag for the author tag cloud</h2>
<p>I am not going to go into to much detail about every line of code because it basically takes the wp_tag_cloud function and modifies it to create the author tag cloud.  To do it on your own check out the wp_tag_cloud function in the wp-<a target="_blank" href="http://core.trac.wordpress.org/browser/tags/3.1.3/wp-includes/category-template.php">includes/category-template.php</a>.  Here is our modified version:</p>
<pre name='code' class='php'>
function author_tag_cloud( $args = '' ) {
        $defaults = array(
            'author_id' => 0,
            'smallest' => 8,
            'largest' => 22,
            'unit' => 'pt',
            'number' => 45,
            'format' => 'flat',
            'separator' => "\n",
            'orderby' => 'name',
            'order' => 'ASC',
            'exclude' => '',
            'include' => '',
            'link' => 'view',
            'taxonomy' => 'post_tag',
            'echo' => true
        );
        $args = wp_parse_args( $args, $defaults );

        //get the author slug
        $author = get_userdata($args['author_id']);

        if(!$author)
            return;

        $args['author-slug'] = $author->user_nicename;

        //$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags

        $tags = author_get_terms($args['taxonomy'], $args);

        if ( empty( $tags ) )
                return;

        foreach ( $tags as $key => $tag ) {
//                    if ( 'edit' == $args['link'] )
//                            $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
//                    else
//                            $link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
            $link = get_bloginfo('url') . "/author/" . $args['author-slug'] . "/tag/" . $tag->slug;

            if ( is_wp_error( $link ) )
                    return false;

            $tags[ $key ]->link = $link;
            $tags[ $key ]->id = $tag->term_id;
        }

        $return = generate_author_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args

        $return = apply_filters( 'author_tag_cloud', $return, $args );

        if ( 'array' == $args['format'] || empty($args['echo']) )
                return $return;

        echo $return;
}
</pre>
<p>If you compare wp_tag_cloud with our function, you will first see the author_id parameter which allows us to tell what author we are talking about.  Next, we try to get the user_data on this author_id.  This is done as a check to see if this ID even exists and if doesn&#8217;t just return.  Next, we call a new function which we need to create to get the author tags called author_get_terms.  The link is then edited to reflect our new permalink structure.  We also change out the generate_wp_tag_cloud with another function we will create.  Lastly, we change the filter so if needed this can be filtered. </p>
<h2>Finding the tags by the author</h3>
<p>In the above function, we called a new function that we need to create called: author_get_terms().  This is basically an SQL call to get rows.  Take a look at the function:</p>
<pre name='code' class='php'>
function author_get_terms($taxonomy, $r){
            global $wpdb;

            return $wpdb->get_results(
                "SELECT posts.ID, taxonomy.term_id, terms.name,terms.slug, terms.term_group,
relationships.term_taxonomy_id,  taxonomy.taxonomy,  taxonomy.description, COUNT(terms.name) AS count

                FROM {$wpdb->prefix}posts AS posts
                JOIN {$wpdb->prefix}term_relationships AS relationships
                JOIN {$wpdb->prefix}term_taxonomy AS taxonomy
                JOIN {$wpdb->prefix}terms AS terms

                WHERE posts.post_author = " . $r['author_id'] . "
                AND posts.post_status = 'publish'
                AND posts.post_type = 'post'
                AND relationships.object_id = posts.ID
                AND relationships.term_taxonomy_id = taxonomy.term_taxonomy_id
                AND taxonomy.taxonomy = '{$taxonomy}'

                AND terms.term_id = taxonomy.term_id
                GROUP BY terms.name
                ORDER BY count DESC"
            );
        }
</pre>
<p>It is a pretty complicated SQL statement that involves joining several tables and grouping the results.  I am not going to cover how this SQL statement was formulated and more than likely there is a better way to do this, but it works.  My recommendation is to read up on joins and try them on your local WordPress install to see what you can come up with.  </p>
<h2>The generate_author_tag_cloud Function</h2>
<p>This is an exact copy of wp_generate_tag_cloud().  The only reason why I did this exact copy is to maybe later work on both to modify these functions even more.  </p>
<h2>Plugin Complete!</h2>
<p>We completed the plugin and actually didn&#8217;t really reinvent the wheel. We took preexisting WordPress functions to create our template tags and used our add permalinks class to create our new structure.  Thanks for reading and please subscribe to our feed, we will continue to add more and more tutorials. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.refactord.com/creating-the-author-tag-cloud/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Google Fonts to WordPress</title>
		<link>http://www.refactord.com/adding-google-fonts-to-wordpress?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-google-fonts-to-wordpress</link>
		<comments>http://www.refactord.com/adding-google-fonts-to-wordpress#comments</comments>
		<pubDate>Mon, 13 Jun 2011 00:23:48 +0000</pubDate>
		<dc:creator>Kyle G</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Google Fonts]]></category>

		<guid isPermaLink="false">http://www.refactord.com/?p=80</guid>
		<description><![CDATA[If you haven&#8217;t seen the great API Google has out for fonts, the chances are when you do you will want to start using it. The Google Font API allows you to add new and great fonts to your web pages and frees you from the limited choices you had before. This site uses many [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t seen the great API Google has out for fonts, the chances are when you do you will want to start using it.  The Google Font API allows you to add new and great fonts to your web pages and frees you from the limited choices you had before.  This site uses many custom fonts and you can see that unique fonts will effect the way your sites look.  </p>
<h2>How to use Google Fonts</h2>
<p>There could be nothing simpler than using this API.  If you read the documentation you will see just how quick and easy it is.  To begin you need to need to check out the <a target="_blank" href="http://www.google.com/webfonts">available fonts</a> and choose the one you like.  After that you simple click the tag that says &#8220;use this font&#8221; and Google provides you the code to use the font you have selected.  </p>
<p>This includes a style sheet link to place in the head of your document and some example code on how to call the font on a element.  That is it.</p>
<h2>Incorporating this into WordPress</h2>
<p>Now to add this WordPress we could simply down this code in the wp_header file in our theme but it would be better to register this style sheet with WordPress and have WordPress manage it.  We can do this by using wp_register_style() function and wp_enqueue_style();</p>
<p>If you have not seen these functions no worry.  They are pretty simple to use and have great resources to read up on.  But the gist is that these function allow you to manage the style sheets through WordPress.  I use often to include a style sheet on one page but exclude it from another.  This is gaining control of what is loaded by WordPress. </p>
<p>To begin, I want this code in my functions.php file and I use an action hook on the init hook to call wp_enqueue_style() and wp_register_style().  Here is the code: take a look and we will go into more detail.</p>
<pre name='code' class='php'>
add_action('init', 'theme_js_css');

 function theme_js_css(){
    wp_register_style( 'googleFonts', 'http://fonts.googleapis.com/css?family=Rokkitt|Amaranth|Raleway:100|Brawler');

    wp_enqueue_style( 'googleFonts' );
 }</pre>
<p>The wp_register_style first accepts the handle or what the style sheet will be called.  The second parameter is the URL path to the style sheet; in our case the Google Fonts.  This function does accept a third parameter, not used here, to call an array of dependent style sheets.  </p>
<p>Lastly we simple call the wp_enqueue_style( &#8216;googleFonts&#8217; ), which calls the style sheet with the handle we created in wp_register_style.  That is it and now these fonts are available to us, we just need to style our elements.  </p>
<p>One note on the code we used.  You will see that pipe character is being used between several font names.  Google allows use to do this to call multiple fonts in one.  Also, a nice little feature.  </p>
<h3>There is also a plugin</h3>
<p>You can also incorporate Google Fonts Plugin for WordPress found in the WordPress <a target="_blank" href="http://wordpress.org/extend/plugins/wp-google-fonts/">repository</a>.  This plugin allows you to select custom fonts from the Google API and create CSS on the spot to style the elements.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.refactord.com/adding-google-fonts-to-wordpress/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Rewrite Rules to WordPress</title>
		<link>http://www.refactord.com/adding-rewrite-rules-to-wordpress?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adding-rewrite-rules-to-wordpress</link>
		<comments>http://www.refactord.com/adding-rewrite-rules-to-wordpress#comments</comments>
		<pubDate>Sun, 05 Jun 2011 23:25:05 +0000</pubDate>
		<dc:creator>Kyle G</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Rewrite Rules]]></category>

		<guid isPermaLink="false">http://www.refactord.com/?p=46</guid>
		<description><![CDATA[Changing WordPress URL structures is covered in many places with just the basics, but you don&#8217;t really get the behind the scenes. The common tutorials and discussion is on how to create a pretty permalink structure that will increase your site&#8217;s SEO. That is changing the default permalink structure from http://www.yoursite.com/?p=1 to http://www.yoursite.com/hello-world. But as [...]]]></description>
			<content:encoded><![CDATA[<p>Changing WordPress URL structures is covered in many places with just the basics, but you don&#8217;t really get the behind the scenes.  The common tutorials and discussion is on how to create a pretty permalink structure that will increase your site&#8217;s SEO.  That is changing the default permalink structure from <strong>http://www.yoursite.com/?p=1</strong> to <strong>http://www.yoursite.com/hello-world</strong>.  But as you start to develop plugins or even advanced themes, a deeper understanding how WordPress using the URL of any page to display the correct content will allow you to create your own URL structures.  This all leads to the WordPress rewrite rules.  </p>
<p>This tutorial will focus on creating a class to add rewrite rules to WordPress.  We will do this by first going into the basic ideas of the WordPress Query class.  Then learning: how to display the rewrite rules that already exist, how to add rules, and how to flush rules.  </p>
<h3>What You Will Need To Know Before Moving On</h3>
<ul>
<li>Some idea of Regular Expressions</li>
<li>The Basics of the WordPress Permalinks</li>
<li>Object Oriented PHP</li>
</ul>
<h3>WordPress Rewrite Rules Basics and The WordPress Query class</h3>
<p>When you visit any page on a WordPress site, there is a process for finding and displaying the content the visitor is requesting.  This is done by parsing the URL of the request page and page here is any page on the site i.e. post, page, category and etc.  To get a deeper understanding the process, you can read the page <a target="_blank" href="http://codex.wordpress.org/Query_Overview">Query Overview</a> in the WordPress Codex.</p>
<p>The part we are most interested in is the parsing of the URL.  This is done by taking the URL string and trying to find a match from a predesignated array of rules.  The predesignated array of rules are set of regular expressions as the keys and the query strings as the values, which will identify the type of content being requested.  </p>
<p>The benefits of using this structure is WordPress can quickly identify what content is being request.  Other methods could involve pulling data from the database its actually wrong and then going back and forth to the database to find the correct match.  The WordPress&#8217; method of using rewrite rules prevents this and essentially saves several database requests before finding the correct content.  </p>
<p>The parsing of the URL by WordPress first determines all the <a  target="_blank" href="http://codex.wordpress.org/Conditional_Tags">conditional tags</a> like <strong>is_home()</strong>, <strong>is_page()</strong> and so forth.  Then the database query is created.  The query is then preformed and the content is finally displayed.  That is the whole thing in a nut shell.  You will see through this post that we use the $wp_rewrite global in all our class methods.  This is the class WordPress uses to parse the URL and create the query string.  </p>
<h3>Creating Our Rewrite Rules Class</h3>
<p>Before we start, we will want a class that is reusable and will give us the ability to pass options directly into our class.  That means will have a constructor method that accepts an options array and initialization method that will set options that we pass to the class and assigns them to class variables.  </p>
<pre name='code' class='php'>
class Refactord_add_rewrite_rules {

    var $rules = array(); //an array of the rules will be adding
    var $show_rules = FALSE; //use to debug and show rules in the wp_footer hook

    function __construct($options = NULL){
        if(!is_null($options)){
            $this->init($options);
        }
    }
    function init($options){
        foreach($options as $key => $value){
            $this->$key = $value;
        }
    }
}</pre>
<p>Also, we will be putting all our hooks in the init function.  You will see I created to class variables.  The first is called $rules and it will contain an array of the rules we want to add.  The second is $show_rules, which is a boolean that we will use in the upcoming method.</p>
<h3>Lets Just Display the WordPress Rewrite Rules</h3>
<p>When you begin adding rewrite rules to WordPress you will be debugging to get your rules right.  And unless you display the rules there is no way to even to know if your rule was added or where your error could be.  That means that we are going to create a method in our class to display the current rewrite rules at the bottom of the page.  We are adding a check in our init function to see if the $display_rules is TRUE and if it is, call the hook and method to display the rules.  First here is our new method to display the rules:</p>
<pre name='code' class='php'>
function show_rules(){
    global $wp_rewrite;

    echo "&lt;pre&gt;";
    print_r($wp_rewrite->rules);
    echo "&lt;/pre&gt;";
}
</pre>
<p>You will see that we first call in the global of $wp_rewrite.  It contains the rules stored in the class variable &#8220;rules&#8221;.  This is the preexisting array of rewrite rules.  </p>
<p>And here is the new init function:</p>
<pre name='code' class='php'>
function init($options){
    foreach($options as $key => $value){
        $this->$key = $value;
    }

    if($this->show_rules){
        add_action('wp_footer', array(&#038;$this, 'show_rules'), 1);
    }
}
</pre>
<p>If you look at the preexisting rules you will see that it is an array.  Its keys are regular expressions with values that are query strings that WordPress will preform.  This important to note as we will need to create similar patterns to have our rules work.  </p>
<p>Here is a show of the existing rules on my localhost:</p>
<p><img src="http://www.refactord.com/wp-content/uploads/2011/06/rules.png" alt="wordpress rewrite rules" title="rules" width="662" height="250" class="alignnone size-full wp-image-67" /></p>
<h3>Flushing Rules</h3>
<p>To add your own rules you must first flush the rewrite rules.  The rewrite rules are stored in the option database and are not regenerated unless told to.  <strong>If you try to add rules without flushing the existing rules, the rules will not be added</strong>.  There are two methods of having the rewrite rules regenerated.  The first is the click the save but in the permalink settings in the admin.  The other method is to request the rules to be regenerate manually in our class.  </p>
<p>Flushing the rewrite rules is something that is not recommend to do unless necessary.  It creates a drain and will slow down WordPress.  You should only flush rules if needed as in the case of adding new rules.  If you are not adding new rules or taking rules away, you shouldn&#8217;t be flushing the rules.  </p>
<p>That means we will need two new methods in our class.  The first is to test to see if our added rules already exists.  This method will return TRUE or FALSE if the rules we are adding already exist.  The next method will flush the rules.  </p>
<p>The first method is called rules_exist() and takes the rules we are adding, which are in the form of an array.  And tests each of the keys and or values against the existing key and values.  If it finds that neither the key or value in the array of existing rules then it returns FALSE meaning the rules must be flushed.</p>
<pre name='code' class='php'>
function rules_exist(){
            global $wp_rewrite;

            foreach($this->rules as $key => $rule){
                if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){
                        return FALSE;
                }
            }
            return TRUE;
}</pre>
<p>Again we call in the global for the $wp_rewrite class and use that to get the current rules that exist.  </p>
<p>The other method is just a couple lines.  First calling the $wp_rewrite global and then if the rules don&#8217;t exist rules (found by calling the previous method), lets flushed the rules.   </p>
<pre name='code' class='php'>
function flush_rules(){
            global $wp_rewrite;
            if(!$this->rules_exist()){
                $wp_rewrite->flush_rules();
            }
        }
</pre>
<p>With that done we have completed methods to flush and to test if rules exist.  We can then go back to our init method and edit it.  Here is the complete init function:</p>
<pre name='code' class='php'>
function init($options){
            foreach($options as $key => $value){
                $this->$key = $value;
            }

            if(!empty($this->rules)){
                add_action('wp_head', array(&#038;$this, 'flush_rules'));
            }

            if($this->show_rules){
                add_action('wp_footer', array(&#038;$this, 'show_rules'), 1);
            }
        }
</pre>
<p>You see that we don&#8217;t call the functions to flush rules if we aren&#8217;t even trying to add any rules (represent by testing to see if the class variable $rules is empty).  </p>
<h3>Finally Adding the rules</h3>
<p>The adding of the rules is simple enough and here is our method:</p>
<pre name='code' class='php'>
function add_rules(){
    global $wp_rewrite;
    $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
}
</pre>
<p>It simply takes our rules and the preexisting rules, merges them, and sets the new $wp_write class variable rules equal to this new combo.   The last thing to do is to add this method to a hook and we will again to it in the init function.  Here is the init function again:</p>
<pre name='code' class='php'>
function init($options){
            foreach($options as $key => $value){
                $this->$key = $value;
            }

            if(!empty($this->rules)){
                add_action('wp_head', array(&#038;$this, 'flush_rules'));
                add_action('generate_rewrite_rules', array(&#038;$this, 'add_rules'));
            }

            if($this->show_rules){
                add_action('wp_footer', array(&#038;$this, 'show_rules'), 1);
            }
        }
</pre>
<p>We just added the action hook call on &#8216;generate_rewrite_rules&#8217;.  We again only call this if the rules don&#8217;t already exist.  </p>
<h4>Advanced Notes</h4>
<p>I tried to incorporate the  rules_exist method in the init method; adding to the line where we see if the rules we want to add are not empty.  But what I found that in the process is WordPress has not yet got the rules in stored in the $wp_rewrite->rules, so it comes back NULL.  You need to incorporate the  rules_exist method directly in the flushing method to make it work.  </p>
<h3>Finally adding rules</h3>
<p>To finish I am going to leave you with a call I used to create a plugin that display authors and categories together on the same page.  The URL structure I wanted to create was:</p>
<p><strong>http://www.yoursite.com/author/author-slug/category/category-slug/</strong></p>
<p>Here is the rules that I created and called into the class.  </p>
<pre name='code' class='php'>
$options = array(
            'rules' => array(
                'category/([^/]+)/author/([^/]+)/?$' => 'index.php?category_name=$matches[1]&#038;author_name=$matches[2]',
                'category/([^/]+)/author/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?category_name=$matches[1]&#038;author_name=$matches[2]&#038;paged=$matches[3]',
                'author/([^/]+)/category/([^/]+)/?$' => 'index.php?author_name=$matches[1]&#038;category_name=$matches[2]',
                'author/([^/]+)/category/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&#038;category_name=$matches[2]&#038;paged=$matches[3]'
                ),
        );

        $add_rewrite_rules = new Refactord_add_rewrite_rules($options);
</pre>
<p>If you notice that key to our rules array is a regular expression with the value of the query string WordPress will preform.  You should know that the matches in the query string are the regular expressions in the parenthesis.  Looking at the third rule I created, the first match is  <strong>([^/]+)</strong> expression after <strong>category/</strong> and the second match is <strong>([^/]+)</strong> after <strong>/author/</strong>.  These matches get replaced in the query string.  </p>
<p>Knowing this and using the URL structure from above of <strong>http://www.yoursite.com/author/author-slug/category/category-slug/</strong>; the query string would become <strong>index.php?category_name=category-slug&#038;author_name=author-slug</strong>.  This will work check it out.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.refactord.com/adding-rewrite-rules-to-wordpress/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Create A WordPress Meta Box With Jquery UI Datepicker</title>
		<link>http://www.refactord.com/create-a-wordpress-meta-box-with-jquery-ui-datepicker?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-a-wordpress-meta-box-with-jquery-ui-datepicker</link>
		<comments>http://www.refactord.com/create-a-wordpress-meta-box-with-jquery-ui-datepicker#comments</comments>
		<pubDate>Sat, 04 Jun 2011 18:32:25 +0000</pubDate>
		<dc:creator>Kyle G</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Plugin]]></category>
		<category><![CDATA[Datepicker]]></category>
		<category><![CDATA[jQuery UI]]></category>

		<guid isPermaLink="false">http://www.refactord.com/?p=12</guid>
		<description><![CDATA[Adding datepickers is a great way to improve your a WordPress Plugin you are building. This post will go through everything you will need to do to add a Jquery UI Datepicker to your custom meta box. What you will need to know before you move on Plugin Basics such as registering a plugin and [...]]]></description>
			<content:encoded><![CDATA[<p>Adding datepickers is a great way to improve your a WordPress Plugin you are building.   This post will go through everything you will need to do to add a Jquery UI Datepicker to your custom meta box.  </p>
<h3>What you will need to know before you move on</h3>
<ul>
<li>Plugin Basics such as registering a plugin and nounces</li>
<li>Basic understanding of WordPress Action and Filter Hooks</li>
<li>Object Oriented PHP</li>
</ul>
<p>Today we won&#8217;t be covering how to register a plugin or how to write a plugin in the form of a PHP class.  You will need to understand these concepts to really get what we are doing.  </p>
<h3>Getting the jQuery UI Datepicker</h3>
<p>Before we get started we need to select a javascript datepicker.  There are many out but one of the easiest to setup and user is the jQuery UI datepicker.  If you have not seen this great little widget before, you can check some <a target="_blank" href="http://jqueryui.com/demos/datepicker/">examples</a> on the jQuery UI website.  Next, we need to head over to the <a target="_blank" href="http://jqueryui.com/download">download page</a> and select what we will need to download.  This page is divided into 4 major sections and we will not need to download everything in our package.  The first is the <em>UI core</em> which I leave all selected.  The next is <em>interactions</em> which I uncheck everything.  Next is <em>widgets</em> which I uncheck everything but the datepicker.  The last section, <em>effects</em> I uncheck everything as well.  Now just download it.</p>
<h3>Setting Up Your Plugin Files</h3>
<p>To start off I create a new folder in my wp-content/plugins that will contain our code.  I name that folder refactord-datepicker and I also create two folders inside of our new plugin folder called &#8220;css&#8221; and &#8220;js&#8221;.  I then take the jQuery UI custom download and place the custom stylesheet and images directory in the css folder of my new plugin.  I do the same for the JavaScript placing it in the js folder.  Usually, the names of these files are long and if you are going to change the name this is the time.  For this tutorial I will leave them the same name.  Here is a shot of the setup:</p>
<p><img src="http://www.refactord.com/wp-content/uploads/2011/06/setup.jpg" alt="File setup for WordPress Datepicker Meta Box Plugin" title="setup" width="258" height="125" class="alignnone size-full wp-image-28" /></p>
<h3>Coding the Datepicker and Metabox Plugin</h3>
<p>Like mentioned above this will be done in the form of a PHP class.  If you are not familiar with coding plugins this way, you should definitely check it out.   To begin I will create a basic class and call the class.  </p>
<pre name='code' class='php'>
/*
Plugin Name: datepicker
Plugin URI:
Description:
Author:
Version: 1.0
*/

class Refactord_Datepicker {

}

$refactord_Datepicker = new Refactord_Datepicker;
</pre>
<h4>The constructor function</h4>
<p>Nothing special so far but the real meat is done in the constructor function which will first establish some class variable representing the file path to the plugin and the URL to the plugin.  Both of these will come in handy when we create our methods to add JavaScript and CSS.  Also, in the constructor function, all of the hooks with their corresponding methods will be called.  There will be 5 action hooks we will use to add our stylesheets, add our scripts, add the meta box, create the meta box content, and save the data when the post is updated/added.  Here is the code:</p>
<pre name='code' class='php'>
class Refactord_Datepicker {

    var $plugin_dir;
    var $plugin_url;

    function  __construct() {

        $this->plugin_dir = WP_PLUGIN_DIR . "/refactord-datepicker";
        $this->plugin_url = WP_PLUGIN_URL . "/refactord-datepicker";

        add_action( 'admin_print_styles', array($this, 'add_stylesheets') );
        add_action( 'admin_enqueue_scripts', array($this, 'add_js') );
        add_action( 'add_meta_boxes', array( $this, 'datepicker_meta_box' ) );
        add_action( 'admin_head', array($this, 'call_js') );
        add_action( 'save_post', array($this, 'save_data') );
    }
</pre>
<h4>Add the datepicker Stylesheets</h4>
<p>The first method we will create is <em>add_stylesheets</em>.  This will do just exactly that.  Here is the method I created: </p>
<pre name='code' class='php'>
function add_stylesheets(){
        global $post;
        $styleFile = $this->plugin_dir . "/css/jquery-ui-1.8.13.custom.css";

        if(file_exists($styleFile) &#038;&#038; $post->post_type == 'post'){
            wp_register_style('datepicker-css', $this->plugin_url . "/css/jquery-ui-1.8.13.custom.css");
            wp_enqueue_style( 'datepicker-css');
        }
}
</pre>
<p>If you look at this function, we use those class variables to get the URL and file paths to the stylesheet we need to add.  Also, we call in the global for post and test to see if this is a post type we want to add the stylesheet too.  There is no sense in adding this stylesheet to every page of the admin panel.  </p>
<h4>Add the Datepicker JavaScript</h4>
<p>This method is almost the same as the one we used for adding the datepicker stylesheet.  The only changes you will notice is the names of the WordPress functions that we use.  As well as the change in the file name we are calling.  </p>
<pre name='code' class='php'>
function add_js(){
        global $post;
        $jsFile = $this->plugin_dir . "/js/jquery-ui-1.8.13.custom.min.js";

        if(file_exists($jsFile) &#038;&#038; $post->post_type == 'post'){
            wp_register_script('datepicker-js', $this->plugin_url . "/js/jquery-ui-1.8.13.custom.min.js");
            wp_enqueue_script( 'datepicker-js');
        }
    }
</pre>
<h4>Calling the Datepicker JavaScript</h4>
<p>Now that we have both methods to add the needed CSS and JavaScript, we need to now call the JavaScript on the input we will create.  I already know the input ID that we will use: <em>&#8220;#datepicker-field&#8221;</em>, so I simply just call the datepicker function on that field ID.  I don&#8217;t even pass any options.  </p>
<pre name='code' class='php'>
function call_js(){
        global $post;
        if($post->post_type == 'post'){

            echo    '<script>
                        jQuery(document).ready(function() {
                                jQuery( "#datepicker-field" ).datepicker();
                        });
                    </script>';
        }
    }
</pre>
<p>Notice, how we omit the $ for jQuery.  This helps prevent any conflicts the dollar sign might create with other loaded scripts.  </p>
<h4>Adding the meta Box</h4>
<p>Next need to actually create out meta box.  We do that by calling this method that is made of really just one function:</p>
<pre name='code' class='php'>
function datepicker_meta_box(){
        add_meta_box(
             'refactord-datepicker'
            ,'Refactord Datepicker'
            ,array( &#038;$this, 'meta_box_content' )
            ,'post'
            ,'side'
            ,'high'
        );
    }
</pre>
<p>If you are not familiar with this function you definitely need to read the <a target="_blank" href="http://codex.wordpress.org/Function_Reference/add_meta_box">codex</a> on it.  </p>
<h4>The meta box content</h4>
<p>This function fills the meta box we just created with the content it will display.  </p>
<pre name='code' class='php'>
function meta_box_content(){
        global $post;
        // Use nonce for verification
        wp_nonce_field( plugin_basename( __FILE__ ), 'refactord_nounce' );

        // The actual fields for data entry
        echo '<label for="datepicker-field">';
           _e("Pick A date", 'myplugin_textdomain' );
        echo '</label> : ';
        echo '
<input type="text" id="datepicker-field" name="datepicker-field" value="' . get_post_meta($post->ID, 'refactord-datepicker', TRUE) . '" size="20" />';
    }</pre>
<p>We are doing a few things in this function.  One is the use of a WordPress Nounce.  This helps maintain a high level of security for our meta box.  Also, you see the we are use the <em>get_post_meta</em> to get the data we will display in our input field.  This is the give away we will be saving this data to the post meta table.  You also see the ID of <em>datepicker-field</em>, which will allow us to call our js.  Actually if we want we can load this plugin now and it would display our datepicker.  But it wouldn&#8217;t save.</p>
<h4>Saving the Datepicker data</h4>
<p>Lastly, we need to save the data.  </p>
<pre name='code' class='php'>
function save_data($post_id){
        if ( defined( 'DOING_AUTOSAVE' ) &#038;&#038; DOING_AUTOSAVE )
            return;

        if ( !wp_verify_nonce( $_POST['refactord_nounce'], plugin_basename( __FILE__ ) ) )
            return;

        // Check permissions
        if ( 'page' == $_POST['post_type'] ){
            if ( !current_user_can( 'edit_page', $post_id ) )
                return;
        }else{
            if ( !current_user_can( 'edit_post', $post_id ) )
                return;
        }

        $data = $_POST['datepicker-field'];

        update_post_meta($post_id, 'refactord-datepicker', $data, get_post_meta($post_id, 'refactord-datepicker', TRUE));

        return $data;
    }</pre>
<p>You notice there are several security checks and also a check to see if an ajax save is be done.  Also, you will see the post meta is saved using an underscore before the actual name of meta key.  If you do this it prevents this option populating in the custom field box.  </p>
<h4>The completed Datepicker class</h4>
<p>That is it.  We are finished and if you load this up in a WordPress install you will get this:  </p>
<p><img src="http://www.refactord.com/wp-content/uploads/2011/06/finish-300x268.jpg" alt="" title="finish" width="300" height="268" class="alignnone size-medium wp-image-36" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.refactord.com/create-a-wordpress-meta-box-with-jquery-ui-datepicker/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

