<?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>WP Inside Out</title>
	<atom:link href="http://www.wpinsideout.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wpinsideout.com</link>
	<description>A site dedicated to a full understanding of Wordpress</description>
	<lastBuildDate>Sun, 05 Dec 2010 16:06:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Day Ten: Adding A Threshold To the Event Calendar</title>
		<link>http://www.wpinsideout.com/day-ten-adding-a-threshold-to-the-event-calendar</link>
		<comments>http://www.wpinsideout.com/day-ten-adding-a-threshold-to-the-event-calendar#comments</comments>
		<pubDate>Sun, 05 Dec 2010 16:06:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=301</guid>
		<description><![CDATA[In this tutorial we are going to add a threshold to the event calendar, which gives you a bit more control when displaying events. The issue that some readers have with the current event calendar plugin we have designed is &#8230; <a href="http://www.wpinsideout.com/day-ten-adding-a-threshold-to-the-event-calendar">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we are going to add a threshold to the event calendar, which gives you a bit more control when displaying events.  </p>
<p>The issue that some readers have with the current event calendar plugin we have designed is the what to do when event starts maybe at 9:00 PM and goes to 12:30 AM.  Well, using the current event calendar we designed, you would get an event to show on two days on the calendar because is starts one day and really ends the next.  Even though the events are really on two different days, people think of this as happening on the day that it starts.  So to make is user friendly, it would be nice to gain control over this.  </p>
<p>To prevent or even give you control over this we need add what we will call as a threshold.  Basically, we are going to use a option that allows us to determine how many hours into the next day an event can go without being displayed on that day.  </p>
<p>To accomplish a working threshold for our events we are going to need to take the ending time and convert it into seconds.  The threshold we also be in seconds and then we will compare them.  If the event is under our threshold, we will not include it on the calendar and if it above our threshold we will include it on the calendar.  </p>
<h3>Step One: Adding an Int and Constuct Methods to the Calendar Events Class</h3>
<p>To have the option of control over our event threshold, I am going to add a construct and init method to our class.  Here are the methods:</p>
<p><pre><code>function __construct($options = NULL){
&nbsp;&nbsp;&nbsp;&nbsp;if(!is_null($options)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;init($options);
&nbsp;&nbsp;&nbsp;&nbsp;}
}

function init($options){
&nbsp;&nbsp;&nbsp;&nbsp;foreach($options as $key =&gt; $value){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;$key = $value;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;threshold = $this-&gt;time_in_secs($this-&gt;threshold);
}</code></pre></p>
<p>I am also going to add a class variable that I am going to call $threshold.  </p>
<p>This will now allow us to pass an option array directly into the class of use the init function to set our $threshold.  You will also see this <em>$this->threshold = $this->time_in_secs($this->threshold);</em>, we will come back to that.  But you need to know when you are passing the options to the class your time must be in MySQL dateTime format.  You will see why later on.</p>
<h3>Step Two: New Method to Convert MySQL dateTime to Seconds</h3>
<p>If we are going to compare our event ending time with our threshold, seconds is the best way.  Below is the method we will use to convert MySQL dateTime to seconds.  </p>
<p><pre><code>function time_in_secs($time){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list($hr, $mins, $secs) = explode(&quot;:&quot;, $time);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ( $hr * 60 *60 ) + ( $mins * 60 ) + $secs;
}</code></pre></p>
<h3>Step 4: Change the Conditional Statements in Calendar Data Method</h3>
<p>If you remember back from our post on the <a href="http://www.wpinsideout.com/wordpress-custom-event-calendar-displaying-our-events">calendar event class</a>, we had four conditions on how to add our event into the data array.  </p>
<ul>
<li>If an event started and ended on the same date.</li>
<li>If an event started and ended on different days in the same month.</li>
<li>If an event started last month and continued into this month</li>
<li>If an event started this month and continued into next month</li>
</ul>
<p>Those are the four but we are really only interest in two: If an event started and ended on different days in the same month and If an event started last month and continued into this month.  Because if an event starts and ends on the same day the threshold and if the event ends next month, the threshold is not needed.  </p>
<p>So here is modifications to the code for the other two conditions:</p>
<p><pre><code>//event that happens over several days in the current month
if($start_date &lt; $end_date &amp;&amp; $start_month == $end_month){
&nbsp;&nbsp;&nbsp;&nbsp;for($start_date; $start_date &lt;= $end_date; $start_date++){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($start_date == $end_date){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($ending_time &gt; $this-&gt;threshold){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$events[$start_date][] = $row;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$events[$start_date][] = $row;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}

//event that started last month but continues into this month
if($start_month == $prev_month){
&nbsp;&nbsp;&nbsp;&nbsp;for($start_date = 1; $start_date &lt;= $end_date; $start_date++){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($start_date == $end_date){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($ending_time &gt; $this-&gt;threshold){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$events[$start_date][] = $row;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$events[$start_date][] = $row;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}</code></pre></p>
<p>We basically added and if statement to each.  We just see if the end date is the date we want to add to the array and if it above our threshold.  And if it is we add it our calendar.  </p>
<h3>Step Five:  Pass the Options Array to our Class</h3>
<p>We can now go our main plugin file: custom_event_calendar.php and pass the options array, which will currently only be our threshold.  Here is the edited function :</p>
<p><pre><code>function custom_event_calendar($atts){
&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;global $post;

&nbsp;&nbsp;&nbsp;&nbsp;extract(shortcode_atts(array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;link_base&#039; =&gt; get_permalink($post-&gt;ID),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;month&#039; =&gt; get_query_var(&#039;mon&#039;),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;year&#039; =&gt; get_query_var(&#039;yr&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;), $atts));

&nbsp;&nbsp;&nbsp;&nbsp;if(empty($month)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$month = strftime(&#039;%m&#039;, current_time(&#039;timestamp&#039;));
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;if(empty($year)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$year = strftime(&#039;%Y&#039;, current_time(&#039;timestamp&#039;));
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;include(&#039;calendar.php&#039;);

&nbsp;&nbsp;&nbsp;&nbsp;$options = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;link_base&#039; =&gt; $link_base
&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;&nbsp;&nbsp;include(&#039;calendar_events.php&#039;);

&nbsp;&nbsp;&nbsp;&nbsp;$options = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;threshold&#039; =&gt; &quot;7:00:00&quot;
&nbsp;&nbsp;&nbsp;&nbsp;);

&nbsp;&nbsp;&nbsp;&nbsp;$our_events = new Our_calendar_events($options);

&nbsp;&nbsp;&nbsp;&nbsp;$events = $our_events-&gt;calendar_content($month, $year);

&nbsp;&nbsp;&nbsp;&nbsp;$event_calendar = new Event_calendar($options);

&nbsp;&nbsp;&nbsp;&nbsp;return $event_calendar-&gt;create($month, $year, $events);
}</code></pre></p>
<p>You see that I have a MySQL dateTime of 7:00:00 which is 7:00 AM.  So any event that ends before 7:00 AM will not be included.  </p>
<h3>Wrap up</h3>
<p>We are all done.  The only thing that would be nice would be to have an options page to control the threshold.  Here are the files for download and you will also find that I added a style sheet to the calendar.  </p>
<div id='' class='button'><span><a href='http://www.wpinsideout.com/wp-content/uploads/2010/12/custom_event_calendar_day_10.zip'>Event Calendar Day Ten</a></span><div class='clear'></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/day-ten-adding-a-threshold-to-the-event-calendar/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WordPress Custom Event Calendar &#8211; Displaying our events</title>
		<link>http://www.wpinsideout.com/wordpress-custom-event-calendar-displaying-our-events</link>
		<comments>http://www.wpinsideout.com/wordpress-custom-event-calendar-displaying-our-events#comments</comments>
		<pubDate>Sun, 24 Oct 2010 19:23:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=285</guid>
		<description><![CDATA[After the last day, all we had left to do is to get the events that we stored in the database and display them on the calendar. Well, maybe this is easier said than done. In the video tutorial, we &#8230; <a href="http://www.wpinsideout.com/wordpress-custom-event-calendar-displaying-our-events">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><embed src="http://blip.tv/play/hrQrgobkQAA" type="application/x-shockwave-flash" width="600" height="400" allowscriptaccess="always" allowfullscreen="true"></embed></p>
<p>After the last day, all we had left to do is to get the events that we stored in the database and display them on the calendar.  Well, maybe this is easier said than done.  In the video tutorial, we will create a dedicated class just to get the events out of the database and prep them to be displayed on the calendar.  </p>
<h3>Considerations</h3>
<p>There really is four types of events that we need think of:</p>
<ul>
<li>An event that starts and ends on the same day</li>
<li>An event that starts and ends on different days but happens in the currently displayed month</li>
<li>An event that starts in the previous month and ends in the currently displayed month</li>
<li>An event that starts in the currently displayed month that continues into the next month</li>
</ul>
<p>These are fourth unique cases of events that we will use for our calendar and these will determine how we get the data and also organize the data.  </p>
<h3>Our Query</h3>
<p>As you will see in the video tutorial, we need to create a complex MySQL query that will get all the rows that meet the criteria above.  This will involve a join between &#8220;our_events&#8221; table and the &#8220;posts&#8221; table.  The WHERE clause takes into consideration the 4 cases from above.  Here is the our query:</p>
<p><pre><code>&quot;SELECT e.start_date, e.end_date, e.start_time, e.end_time, p.ID, p.post_title, p.post_name, p.post_excerpt
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM {$wpdb-&gt;prefix}our_events AS e
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOIN {$wpdb-&gt;prefix}posts AS p ON e.post_id = p.ID
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE e.event_visible = 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND p.post_status = &#039;publish&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND (e.start_date LIKE &#039;{$year}-{$month}%&#039; OR e.end_date LIKE &#039;{$year}-{$month}%&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY e.start_time, e.start_date&quot;</code></pre></p>
<p>Looking at this query, the part that ensures we meet all the criteria we set above is this section: </p>
<p><code>AND (e.start_date LIKE &#039;{$year}-{$month}%&#039; OR e.end_date LIKE &#039;{$year}-{$month}%&#039;)</code></p>
<p>The logic is: if we have any event that starts in this month, obviously we need get that row, but we also need to get any rows for events that end in this month.  These are the events that started in the previous month and continue into the present month.  </p>
<h3>Notes on Organizing the Data</h3>
<p>The data that we get could be as little as one or two rows but each of these could appear on the calendar multiple times if each happen over several days.  This means we need create an array of $events that represents each day on the calendar which an event happens.  The $keys for the $events array will be the day in which the event happens and the $value will be the $row that was found in the database.   So if we have an event that starts on the 1st and ends on the 2nd we will need to insert this row two times into our array with the keys 1 and 2.  </p>
<p>To make this determination, we need data about the event such as the start date, end date, start month, and end month.  Also to help us make this determination, we will need information about the current month we are displaying: what is the previous month, what is the next month, and how many days does this month have.  If you look at the code, you will see our four cases with into our calendar_data function and how we use the data about the event and month to insert the event into the $events array.  </p>
<h3>Notes on the Calendar Content</h3>
<p>Now, that we have the data organized we need to put the data in a usable format. The data is currently in a multi dimensional array which we will need to convert to content.  To do this we will loop the array on each day and create a un-order list for day.  The unordered list will hold list items created from the rows of data we got from the data base.  </p>
<h3>The files</h3>
<p>Here is the plugin for download so far<br />
<div id='' class='button'><span><a href='http://www.wpinsideout.com/wp-content/uploads/2010/10/custom_event_calendar_day_9.zip'>WordPress Custom Event Calendar Day 9</a></span><div class='clear'></div></div> </p>
<h3>Whats left?</h3>
<p>The calendar might be functioning but sure is not the prettiest thing, next time we will look at styling our calendar for your site.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/wordpress-custom-event-calendar-displaying-our-events/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Adding Custom Post Type Counts To Your Dashboard</title>
		<link>http://www.wpinsideout.com/adding-custom-post-type-counts-to-your-dashboard</link>
		<comments>http://www.wpinsideout.com/adding-custom-post-type-counts-to-your-dashboard#comments</comments>
		<pubDate>Tue, 12 Oct 2010 06:11:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tricks]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=273</guid>
		<description><![CDATA[More than likely when you login into your WordPress Admin the first thing that you look at is the amount of posts, pages, categories, and tags being displayed in the &#8220;Right Now&#8221; meta box. Well, lets be honest you might &#8230; <a href="http://www.wpinsideout.com/adding-custom-post-type-counts-to-your-dashboard">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>More than likely when you login into your WordPress Admin the first thing that you look at is the amount of posts, pages, categories, and tags being displayed in the &#8220;Right Now&#8221; meta box.  Well, lets be honest you might look and you might not.  I guess it would depend on how honest you are with yourself on your lack of publishing.  </p>
<p>Nevertheless, now that we have added custom post types to WordPress, It would be nice to have a number display with your custom post type stats in the right now box in your dashboard.  So again you can monitor how much you aren&#8217;t publishing these either.  To do this we are going to open back up our custom post type class and add a new function.  If you haven&#8217;t read that tutorial don&#8217;t feel bad, just feel out of the loop as you read this tutorial or get up to speed: <a href="http://www.wpinsideout.com/advanced-custom-post-types-php-class-integration">custom post type clas</a>s.</p>
<h3>Step One: Copy WordPress.org</h3>
<p>Well, there is no sense why we should to try to code our own function because WordPress.org already has it done:  <a href='http://codex.wordpress.org/Plugin_API/Action_Reference/right_now_content_table_end'>Right Now Content Table</a>  There you will find the code we are going to take and paste into our class.  After, coping the code, I am going to open our class and paste this at the end.  </p>
<h3>Step Two: Replace Bits and Pieces With Your Class Varaibles</h3>
<p>All we have to do is then find that parts that we can potentially replace with class variables.  Here is the code redone for our class:</p>
<p><pre><code>function add_count() {

&nbsp;&nbsp;if (!post_type_exists($this-&gt;type)) {
&nbsp;&nbsp;&nbsp;&nbsp; return;
&nbsp;&nbsp;}

&nbsp;&nbsp;$num_posts = wp_count_posts( $this-&gt;type );
&nbsp;&nbsp;$num = number_format_i18n( $num_posts-&gt;publish );
&nbsp;&nbsp;$text = _n( $this-&gt;single, $this-&gt;plural, intval($num_posts-&gt;publish) );

&nbsp;&nbsp;if ( current_user_can( &#039;edit_posts&#039; ) ) {
&nbsp;&nbsp;&nbsp;&nbsp;$num = &quot;&lt;a href=&#039;edit.php?post_type=&quot; . $this-&gt;type . &quot;&#039;&gt;$num&lt;/a&gt;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;$text = &quot;&lt;a href=&#039;edit.php?post_type=&quot; . $this-&gt;type . &quot;&#039;&gt;$text&lt;/a&gt;&quot;;
&nbsp;&nbsp;}

&nbsp;&nbsp;echo &#039;&lt;td class=&quot;first b &#039; . $this-&gt;plural . &#039;&quot;&gt;&#039; . $num . &#039;&lt;/td&gt;&#039;;
&nbsp;&nbsp;echo &#039;&lt;td class=&quot;t &#039; . $this-&gt;plural . &#039;&quot;&gt;&#039; . $text . &#039;&lt;/td&gt;&#039;;
&nbsp;&nbsp;echo &#039;&lt;/tr&gt;&#039;;

&nbsp;&nbsp;if ($num_posts-&gt;pending &gt; 0) {
&nbsp;&nbsp;&nbsp;&nbsp;$num = number_format_i18n( $num_posts-&gt;pending );
&nbsp;&nbsp;&nbsp;&nbsp;$text = _n( $this-&gt;single . &#039; Pending&#039;,&nbsp;&nbsp;$this-&gt;plural . &#039; Pending&#039;, intval($num_posts-&gt;pending) );
&nbsp;&nbsp;&nbsp;&nbsp;if ( current_user_can( &#039;edit_posts&#039; ) ) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$num = &quot;&lt;a href=&#039;edit.php?post_status=pending&amp;post_type=&quot; . $this-&gt;type . &quot;&#039;&gt;$num&lt;/a&gt;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$text = &quot;&lt;a href=&#039;edit.php?post_status=pending&amp;post_type=&quot; . $this-&gt;type . &quot;&#039;&gt;$text&lt;/a&gt;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;&lt;td class=&quot;first b &#039; . $this-&gt;plural . &#039;&quot;&gt;&#039; . $num . &#039;&lt;/td&gt;&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;&lt;td class=&quot;t &#039; . $this-&gt;plural . &#039;&quot;&gt;&#039; . $text . &#039;&lt;/td&gt;&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;&lt;/tr&gt;&#039;;
&nbsp;&nbsp;}
}</code></pre></p>
<p>Also, I renamed the function &#8216;add_count&#8217;.  </p>
<h3>Step Three: Use add_action to call our funciton</h3>
<p>The last thing we need to do is to take the code where created the custom post type and add one last hook like this:</p>
<p><code>add_action(&#039;right_now_content_table_end&#039;, array($event_post_type, &#039;add_count&#039;));</code></p>
<p>If you are not sure where this is coming from you did need to read the previous post.  But now if we got everything else right you will see this:</p>
<p><img src="http://www.wpinsideout.com/wp-content/uploads/2010/10/right_now_example.jpg" alt="" title="right_now_example" width="600" height="288" class="alignnone size-full wp-image-274" /></p>
<p>As you see on the bottom in the right now box from my dashboard, the row for &#8220;events&#8221; was added.  Looks like I have 6 events, that is because I am popular.  </p>
<div id='' class='button'><span><a href='http://www.wpinsideout.com/wp-content/uploads/2010/10/add_custom_post_type.zip'>Download The Class</a></span><div class='clear'></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/adding-custom-post-type-counts-to-your-dashboard/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create Your Own Button Shortcode</title>
		<link>http://www.wpinsideout.com/create-your-own-button-shortcode</link>
		<comments>http://www.wpinsideout.com/create-your-own-button-shortcode#comments</comments>
		<pubDate>Fri, 08 Oct 2010 05:07:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Shortcode]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=253</guid>
		<description><![CDATA[Adding buttons is something that a lot site require and having a dedicated shortcode just for buttons, makes the process a snap. Also, it provides a method for the designer to ensure the added button is thematically consistent. It also &#8230; <a href="http://www.wpinsideout.com/create-your-own-button-shortcode">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Adding buttons is something that a lot site require and having a dedicated shortcode just for buttons, makes the process a snap.  Also, it provides a method for the designer to ensure the added button is thematically consistent.  It also adds a nice level of usability to the site.  Today, we are going to create a shortcode to add a custom button.  We will go over the shortcode and the css needed to create a custom button.  </p>
<h3>Step One: Adding the shortcode</h3>
<p>To start adding the shortcode, I am going to open up the functions.php file in my active theme and at the very end of the file I am going to use the function add_shortcode().  This function accepts two parameters.  The first is the name of the shortcode used in the post and the second is the name of the function called when the post is displayed.  </p>
<p><code>add_shortcode(&#039;button&#039;, &#039;create_button&#039;);</code></p>
<p>Now when we enter the button shortcode in post the user-defined function, &#8220;create_button&#8221; will be called.  </p>
<h5>Our Shortcode Function</h5>
<p>We must now create our user define function called create_buttion.  This function will accept two parameters.  The first is $atts which standards for the attributes we passed to our shortcode.  These are done in the manner of the name of the attribute equals the value in quotes.  Here is an example:</p>
<p>[shortcode attribute="value"]</p>
<p>This will allow us to pass some value to our shortcode that will be extracted out and used in the user defined function.  </p>
<p>The second parameter is $content = NULL.  This is content that we what to pass to our short code.  It is set to NULL as a default in case $content is not passed to the shortcode.  To pass content from the shortcode the to function will use a opening and closing shortcode similar to an open and closing HTML tag:</p>
<p>[shortcode]Some Content[/shortcode]</p>
<p>&#8220;Some Content&#8221; then is passed to our function as the second parameter.  Here is the function so far:</p>
<p><pre><code>function create_button($atts, $content = NULL){

}</code></pre></p>
<h5>Extracting the Shortcode Attributes</h5>
<p>To use the data passed in the attributes, first the extract PHP function is called and then the WordPress function shortcode_atts().  This WordPress function, shortcode_atts(), requires two parameters.  The first is an associative array, where the key will become a variable equal to its value in the array.  The second parameter is the attributes passed to the shortcode from the post.  It is important to realize that this method sets values as defaults which can then be overridden by the attributes passed to the shortcode.  </p>
<p>For our function, the first parameter we pass to shortcode_atts() will be an array containing three keys: class, url, and id.
<ul>
<li><strong>class</strong> will be the CSS class of the button.  By default, I set the value to &#8216;button&#8217; and the CSS we will create will use this class.  But if you want several styles of buttons, you can create several classes in your stylesheet and then just assign a class to the button to change the look.</li>
<li><strong>URL</strong> is the URL of where the button will point.  I set the default to &#8216;#button&#8217; but this is really not needed.</li>
<li><strong>id</strong> allows you to assign a specific ID to your button.  This is nice if your are planning to add some custom styling or JavaScript to an individual button. </li>
</ul>
<h3>Finishing The Shortcode Function</h3>
<p>To finish our shortcode function, we just need to take the content and the attributes and return a string of content.  Here is the completed function:</p>
<p><pre><code>function create_button($atts, $content = NULL){

&nbsp;&nbsp;&nbsp;&nbsp;extract( shortcode_atts( array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;class&#039; =&gt; &#039;button&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;url&#039; =&gt; &#039;#&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;id&#039; =&gt; &quot;&quot;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;), $atts ) );

&nbsp;&nbsp;&nbsp;&nbsp;return &quot;&lt;div id=&#039;{$id}&#039; class=&#039;{$class}&#039;&gt;&lt;a href=&#039;{$url}&#039;&gt;&lt;span&gt;{$content}&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&quot;;
}</code></pre></p>
<h3>Step Two: The Button CSS</h3>
<p>Lastly, we need to add the CSS to the active theme&#8217;s stylesheet.  Here is the css I added:</p>
<p><pre><code>.button {
&nbsp;&nbsp;&nbsp;&nbsp;margin:0 0 20px 0;
&nbsp;&nbsp;&nbsp;&nbsp;overflow: hidden;
}
.button span {
&nbsp;&nbsp;&nbsp;&nbsp;background: url(images/grad_two.png) top left repeat-x;
&nbsp;&nbsp;&nbsp;&nbsp;overflow: hidden;
&nbsp;&nbsp;&nbsp;&nbsp;border:1px solid #bbbbbb;
&nbsp;&nbsp;&nbsp;&nbsp;display:block;
&nbsp;&nbsp;&nbsp;&nbsp;float:left;
}

.button a{
&nbsp;&nbsp;&nbsp;&nbsp;color:#325e84;
&nbsp;&nbsp;&nbsp;&nbsp;border:1px solid #fff;
&nbsp;&nbsp;&nbsp;&nbsp;display:block;
&nbsp;&nbsp;&nbsp;&nbsp;float:left;
&nbsp;&nbsp;&nbsp;&nbsp;padding:13px 20px 12px 20px;
&nbsp;&nbsp;&nbsp;&nbsp;text-shadow: #705416 0px 1px 0px;
&nbsp;&nbsp;&nbsp;&nbsp;text-transform: uppercase;
&nbsp;&nbsp;&nbsp;&nbsp;font-family:Helvetica, Arial;
&nbsp;&nbsp;&nbsp;&nbsp;text-decoration: none;
&nbsp;&nbsp;&nbsp;&nbsp;font-size: 1.1em;
&nbsp;&nbsp;&nbsp;&nbsp;letter-spacing: -1px;
&nbsp;&nbsp;&nbsp;&nbsp;font-size:16px;
&nbsp;&nbsp;&nbsp;&nbsp;font-weight: bold;
}

.button a:hover {
&nbsp;&nbsp;&nbsp;&nbsp;color:#6eabdf;
}</code></pre></p>
<h3>The Result of Our Button Shortcode</h3>
<div id='button' class='button'><span><a href='http://www.wpinsideout.com/create-your-own-button-shortcode#button'>Our Button</a></span><div class='clear'></div></div>
<p>Or How Bout:</p>
<div id='' class='button'><span><a href='http://www.wpinsideout.com/wp-content/uploads/2010/10/button_shortcode.zip'>Download as Plugin</a></span><div class='clear'></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/create-your-own-button-shortcode/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Day Eight &#8211; Creating the Calendar Shortcode</title>
		<link>http://www.wpinsideout.com/day-eight-creating-the-calendar-shortcode</link>
		<comments>http://www.wpinsideout.com/day-eight-creating-the-calendar-shortcode#comments</comments>
		<pubDate>Thu, 07 Oct 2010 04:09:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>
		<category><![CDATA[Videos]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=246</guid>
		<description><![CDATA[The next step for our custom event calendar plugin for WordPress is to add a shortcode for displaying our calendar.  I have created a video that goes over the steps and how we are going to use the query vars and shortcode attributes to set the displayed month. <a href="http://www.wpinsideout.com/day-eight-creating-the-calendar-shortcode">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><embed src="http://blip.tv/play/AYKB614A" type="application/x-shockwave-flash" width="600" height="400" allowscriptaccess="always" allowfullscreen="true"></embed></p>
<h3>Adding Shortcodes</h3>
<p>The shortcode API is truly a great thing and in the video I showed how to add the calendar shortcode.  We also saw how to pass attributes from our post to the shortcode to them be parse and used by our custom calendar function. Here is a quick recap of how to add the shortcode.</p>
<h4>To add a shortcode</h4>
<p>Adding a shortcode is done by using the add_shortcode() function in your plugin file or function.php in your theme.  This function accepts two parameters.  The first is the name of the shortcode and the second is the function that will be called when the shortcode is displayed.    So our short code looked like this:</p>
<p><code>add_shortcode( &#039;event_calendar&#039;, &#039;custom_event_calendar&#039;);</code></p>
<p>We next need to create our custom_event_calendar function which can accept a few parameters.  For our plugin, we will only pass it one variable, $atts.  This is an array of attributes that can be passed to our function from the post. If you view the video there is more detail on extracting the attributes.  Here is the code for taking the attributes and extracting the data we will use:</p>
<p><pre><code>function custom_event_calendar($atts){
&nbsp;&nbsp; 
&nbsp;&nbsp;global $post;
&nbsp;&nbsp; 
&nbsp;&nbsp;extract(shortcode_atts(array(
&nbsp;&nbsp;&nbsp;&nbsp;&#039;link_base&#039; =&gt; get_permalink($post-&gt;ID),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;month&#039; =&gt; get_query_var(&#039;mon&#039;),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;year&#039; =&gt; get_query_var(&#039;yr&#039;)
&nbsp;&nbsp;), $atts));
&nbsp;&nbsp;
&nbsp;&nbsp;
}</code></pre></p>
<p>This code will allow use to have some default values for $month and $year, but we need to take care of one more situation.  It is what happens when the user doesn&#8217;t pass a month or year attribute to the shortcode and there is no month or year in the URL string.  We would get an error.  So the assumption is that the month that is being requested is the current month so if $month or $year are empty we need to set these variables to the current month and year.  </p>
<p>We use the current_time() function to set these variable to the current time of WordPress not the current time of the server because these could be different.  </p>
<h3>Adding Our Calendar Script</h3>
<p>The last thing we did was to add our calendar script and actually get the calendar to display.  We first included the calendar script file, then we created an options array.  This options array contains only one variable which we the &#8216;link_base&#8217; or the permalink of the page.  This allows the calendar script to then automatically generate the next and previous links.  </p>
<p>We then create a new instance of our calendar class using our $option array.  Then we simply need to return the method for creating the calendar passing it the $month and $year variables.  Here is the result:</p>
<p><img src="http://www.wpinsideout.com/wp-content/uploads/2010/10/Untitled-1.jpg" alt="" title="Calendar" width="600" height="464" class="alignnone size-full wp-image-248" /></p>
<div id='' class='button'><span><a href='http://www.wpinsideout.com/wp-content/uploads/2010/10/custom_event_calendar_day_8.zip'>Download The Files</a></span><div class='clear'></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/day-eight-creating-the-calendar-shortcode/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Day Seven Again &#8211; Video Adding Query Vars and Rewrite Rules to WordPress</title>
		<link>http://www.wpinsideout.com/day-seven-again-video-adding-query-vars</link>
		<comments>http://www.wpinsideout.com/day-seven-again-video-adding-query-vars#comments</comments>
		<pubDate>Tue, 05 Oct 2010 06:58:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=241</guid>
		<description><![CDATA[Here is a video that goes over day seven in more detail on how to add query vars and rewrite rules to WordPress. <a href="http://www.wpinsideout.com/day-seven-again-video-adding-query-vars">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><embed src="http://blip.tv/play/AYKB6E0A" type="application/x-shockwave-flash" width="600" height="400" allowscriptaccess="always"  allowfullscreen="true"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/day-seven-again-video-adding-query-vars/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Day Seven – Adding Query Vars and Rewrite Rules</title>
		<link>http://www.wpinsideout.com/adding-query-vars-and-rewrite-rules</link>
		<comments>http://www.wpinsideout.com/adding-query-vars-and-rewrite-rules#comments</comments>
		<pubDate>Mon, 27 Sep 2010 07:05:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=229</guid>
		<description><![CDATA[Starting off, this post is all about getting the URL structure we want to make our calendar not only function but have a pretty URL structure. Here is an example of a URL that I want to create: http://www.mysite.com/my-calendar-page/2010/09 The &#8230; <a href="http://www.wpinsideout.com/adding-query-vars-and-rewrite-rules">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Starting off, this post is all about getting the URL structure we want to make our calendar not only function but have a pretty URL structure.  Here is an example of a URL that I want to create:</p>
<p><code>http://www.mysite.com/my-calendar-page/2010/09</code></p>
<p>The only thing I added to the normal URL string is the 2010/09, which the first section is the year and the second section is the month.   It will allow us to easily get for month and year on any page or post to be used in our calendar.  Also, this URL structure will allow us to create links to the previous and next months.</p>
<p> Now, If you were try this on any normal WordPress install, you would get a 404 error, page not found.  This mean WordPress doesn’t know what to do with this URL structure.  So we will need to add it to the rewrite rules.  Also, we will need to add the query vars for year and month to get the data from the URL string.   </p>
<h3>What are Rewrite Rules</h3>
<p>If you remember I wrote a post not too long ago on how to see your WordPress rewrite rules, but it didn’t go into too much detail.  But to start to understand what WordPress rewrite rules are, you have to have an understanding of the Wp_rewrite class.  </p>
<p>The Wp_rewrite class is the method that allows you to have different configurations of URL’s or pretty URL’s.  Also, the Wp_rewrite class helps WordPress determine what page visitor is looking for.  This is done with a set of rules and the URL string.  Currently the URL is telling WordPress to find this post.  Seems simple enough but what happens when you are looking for child pages or archives.  WordPress can take the URL and find a match within the rules to quickly find the correct data to display. </p>
<p>Without the Wp_rewrite class, WordPress would have to perform multiple database queries to find the correct data.  Depending on the data the visitor is looking for this could be quite a few queries before the correct information is found.<br />
Using rewrite rules saves WordPress the hassle of multiple queries.  It can go the rules find a match and instantly know what data to go look for.    </p>
<p>You should also know that these rules live in the database.  And simply adding your rules to the rewrite rules will not be enough most times.  You will need to flush what is save and have the rules regenerated to ensure your rules are adding, but more on this later in the post.  </p>
<h3>Setting Up Your Own Class To Add Rewrite Rules</h3>
<p>I prefer to use a class to handle not only adding rewrite rules but also adding query vars.  The nice thing about a class is that it is predictable and reusable so I can use this example on any plugin needing rewrite rules.</p>
<p>So I am going to start by creating a new file within our main plugin folder called add_rewrite_rules.php.  In this file I am going to create a class that I call the same as the file Add_rewrite_rules. </p>
<p>Since I will be adding query vars and rewrite rules, I am going to create two variables with those names.  </p>
<p>Here is what we have so far:</p>
<p><pre><code>class Add_rewrite_rules{
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;var $query_vars;
&nbsp;&nbsp;&nbsp;&nbsp;var $rules;
}</code></pre></p>
<p>To assign values to these variables I am going to create a construct function that will accept a $config.  I give the $config variable a default value of null.   I then an if statement to see $config is null.  If it isn’t, meaning a value was passed to the construct of the class, we will run our next function passing it the $config.  </p>
<p>Our next function is init function that I that simply takes an array and based on the keys, assigns the values to the class variables that have the same names as the keys.  Below is the class so far:</p>
<p><pre><code> class Add_rewrite_rules{

&nbsp;&nbsp;var $query_vars;
&nbsp;&nbsp;var $rules;

&nbsp;&nbsp;function __construct($options){
&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;init($options);
&nbsp;&nbsp;}

&nbsp;&nbsp;function init($options){
&nbsp;&nbsp;&nbsp;&nbsp;foreach($options as $key =&gt; $value){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;$key = $value;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}</code></pre></p>
<h3>Adding Our Method To Add Query Vars</h3>
<p>The next step is to add a method that adds query vars to WordPress.  As mentioned above, we want to add two query vars, one representing the year and the other representing the month.   The class variable $query_vars should be an array.  </p>
<p>Also, since we will be using this method with a filter hook, our class method to add query vars should accept a variable which I called $query_vars.  Sounds confusing that we have two variables named $query_var.  But, one is a class variable so we will use $this to reference it.  </p>
<p>I then create a loop to go through the array of query vars we want to add and add it to the $query_var array that we passed to our function.  In this case $query_var, the one we passed to our function, is the actual array of query vars WordPress will use.   Here is our method:</p>
<p><pre><code> //filter function to be used with add_filter() with the hook &quot;query_vars&quot;
function add_query_vars($query_vars){

&nbsp;&nbsp;foreach($this-&gt;query_vars as $var){
&nbsp;&nbsp;&nbsp;&nbsp;$query_vars[] = $var;
&nbsp;&nbsp;}

&nbsp;&nbsp;return $query_vars;
}</code></pre></p>
<h3>Examining pre-existing rewrite rules </h3>
<p>If you used the post on how to view rewrite rules, you will find that the rewrite rules are stored in an array.  The keys of the array are regular expressions and the values of the array contain the matches of those regular expressions and the query vars.  Here is what you will see:</p>
<p><img src='http://www.wpinsideout.com/wp-content/uploads/2010/08/display_rewrite_rules.jpg' alt='rewrite_rules'/></p>
<p>To begin to add our rewrite rules we will need two methods, one to add the rules and the other to flush the rules.  </p>
<h3>Adding Our ReWrite Rules to WordPress</h3>
<p>To start, we will just focus on adding the rules.  It is a really simple method within our class that first calls in the $wp_rewrite global variable.  Then it sets the wp_rewrite  class variable rules equal to $this->rules plus the old rules, $wp_rewrite->rules.  Here is our method:</p>
<p><pre><code> function add_rewrite_rules(){
&nbsp;&nbsp;global $wp_rewrite;

&nbsp;&nbsp;$wp_rewrite-&gt;rules = $this-&gt;rules + $wp_rewrite-&gt;rules;
}</code></pre></p>
<h3>Flushing The Rules</h3>
<p>Flushing is the act of having the rewrite rules regenerated.  The rewrite rules live in your wp_options database and just adding rules like we did, will not have them recognized by WordPress.  So we need to tell WordPress to regenerate the rules so ours are added.  There are two methods.</p>
<p>The first method of flushing the rewrite rules is to go to the permalinks screen within the admin and hit save.  This will regenerate the rules and you are all set.  The reason why this is not the best method is because we are creating a plugin that potentially thousands of people are going to use.   If each one has to go hit save in the permalinks screen, well we are not that user friendly. </p>
<p>The other method is to flush the rewrite rules ourselves.  We will do this by creating another method within our class with just this purpose.  </p>
<p>But a quick not on flushing rewrite rules:  Flushing the rules is considered to be a quite laborious process that shouldn’t be done every time the site is displayed.  </p>
<p>This statement makes us have to ensure that our rules are added but not flush the rules every time the site is visited.  This means we will need to add one more method to our class to check to see if our rules have already been added.  Here is the method that I devised:</p>
<p><pre><code> function rules_exist(){
&nbsp;&nbsp;global $wp_rewrite;

&nbsp;&nbsp;$has_rules = TRUE;

&nbsp;&nbsp;foreach($this-&gt;rules as $key =&gt; $value){
&nbsp;&nbsp;&nbsp;&nbsp;if(!in_array($value, $wp_rewrite-&gt;rules)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$has_rules = FALSE;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;}

&nbsp;&nbsp;return $has_rules;
}</code></pre></p>
<p>Now to our flushing function:</p>
<p><pre><code> function flush_rules(){
&nbsp;&nbsp;global $wp_rewrite;

&nbsp;&nbsp;if(!$this-&gt;rules_exist()){
&nbsp;&nbsp;&nbsp;&nbsp;//echo &quot;flushed&quot;; // If want to see this in action uncomment this line and remove this text and you will see it flushed before your eyes
&nbsp;&nbsp;&nbsp;&nbsp;$wp_rewrite-&gt;flush_rules();
&nbsp;&nbsp;}
}</code></pre></p>
<h3>Using the class</h3>
<p>To use the class we just created I am going to go back into my main plugin file: custom_event_calendar.php and include our new file.  I am going to create an $options array.  The first thing I am going to add to my array, is an array of query vars with ‘query_var’ as the key.  The next thing I will add is another array with my ‘rules’.  This contains a regular expression as the key and a value containing a matches array and query vars.    </p>
<p>Let’s go over the regular expression I used and how it is used with the match.  Each section in () is a match.  So the first match is (.+?) which will be the first match, $matches[1],  and will be assigned the query var pagename.  Next match, $matches[2],  is ([0-9]{4}) and will be assigned to the query var yr and the third match, $matches[3], is ([0-9]{1,2}) assigned to the query var mon.  </p>
<p>I then take this options array and create a new instance of my add_rewrite_rules class.  I then use add_action and add_filter to call my methods on specific hooks.  Here is the code:</p>
<p><pre><code> include(EVENT_CALENDAR_DIR . &quot;add_rewrite_rules.php&quot;);

$options = array(
&nbsp;&nbsp;&nbsp;&nbsp;&#039;query_vars&#039; =&gt; array(&#039;yr&#039;, &#039;mon&#039;),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;rules&#039; =&gt; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;(.+?)/([0-9]{4})/([0-9]{1,2})/?$&#039; =&gt; &#039;index.php?pagename=$matches[1]&amp;yr=$matches[2]&amp;mon=$matches[3]&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
&nbsp;&nbsp;);

$rewrite = new Add_rewrite_rules($options);

add_action(&#039;wp_head&#039;, array(&amp;$rewrite, &#039;flush_rules&#039;));
add_action( &#039;generate_rewrite_rules&#039;, array(&amp;$rewrite, &#039;add_rewrite_rules&#039;) );
add_filter( &#039;query_vars&#039;, array(&amp;$rewrite, &#039;add_query_vars&#039;) );</code></pre></p>
<p>And all that will add this to the array:</p>
<p><img src='http://www.wpinsideout.com/wp-content/uploads/2010/09/rewrite_rules.png' alt=''/></p>
<p>And if we change our URL to http://www.example.com/about/2010/10/ we will no longer get a 404 error.  </p>
<h3>Getting the query var data</h3>
<p>Now that we added the year and month we can work with it.  I can simply call </p>
<p><code> $mon = get_query_var(&#039;mon&#039;);</code></p>
<p>And 10 will be assigned to my variable.  </p>
<h3>Get the code:</h3>
<p>Here is the plugin so far: <a href="http://www.wpinsideout.com/wp-content/uploads/2010/09/custom_event_calendar2.zip">custom_event_calenda</a>r</p>
<h3>What’s next?</h3>
<p>We will need to find a calendar script and start creating a short code to display our calendar.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/adding-query-vars-and-rewrite-rules/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Day Six – Repopulating The Event Meta Box</title>
		<link>http://www.wpinsideout.com/repopulating-the-event-meta-box</link>
		<comments>http://www.wpinsideout.com/repopulating-the-event-meta-box#comments</comments>
		<pubDate>Mon, 27 Sep 2010 03:42:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=226</guid>
		<description><![CDATA[In the last day of this tutorial series, we focused on taking the data submitted in our meta box and saving it to the database. We did this by creating a class to handle the data. This class also included &#8230; <a href="http://www.wpinsideout.com/repopulating-the-event-meta-box">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the last day of this tutorial series, we focused on taking the data submitted in our meta box and saving it to the database.   We did this by creating a class to handle the data.  This class also included four methods of converting dates and times back and forth between the inputted format and MySQL format.  On this day we will just be repopulating our event calendar meta box.  </p>
<p>To repopulate our form, we will need to first create a method within event_class to get the data and convert it to the right format.  Then will need to modify our event_meta_options function to use this new method to repopulate the fields in our event calendar meta box.</p>
<h2>The get_event Method Within Our Class</h2>
<p>As the heading implies, I am going to create a new method within our event_class called get_event.  This method will return the data we need to repopulate the form in the correct format.  It will accept one variable, represented by $post_id.</p>
<p>Okay, here is the thing: what happens when we create a new post.  Well, there is no pre-existing data for us to get so this would cause an error.  So we need to somehow find out if the post we are creating already has data and if it doesn&#8217;t return something in its place.</p>
<p>With that said, we will first need to call in the $wpdb object and create a variable, $table_name, to hold the name our table.  Then will use the $wpdb get_row method and store this to a variable.  The method will then use the get_row method from $wpdb to run a query containing a WHERE clause.  The WHERE clause will find the data based on the $post_id that we passed to our function.  </p>
<p>We can then test to see is if our $row is empty.   If it is empty then we can just return an empty array of data with the field names.  So if the post is new this data will populate the fields with values that are empty strings.  </p>
<p>If there is a row, then we will want to create an array for the data.  In this array we us our methods, format_date and format_time, to format the data to the inputted format.  Here is completed function:</p>
<p><pre><code>function get_event($post_id){
&nbsp;&nbsp;global $wpdb;

&nbsp;&nbsp;$table_name = $wpdb-&gt;prefix . &quot;our_events&quot;;

&nbsp;&nbsp;$row = $wpdb-&gt;get_row(&quot;SELECT * FROM {$table_name} WHERE post_id = {$post_id}&quot;, ARRAY_A);
&nbsp;&nbsp;//if the row is empty that means there is no save data so return an empty array
&nbsp;&nbsp;if(empty($row)){
&nbsp;&nbsp;&nbsp;&nbsp;return array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_date&#039; =&gt; &quot;&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_date&#039; =&gt; &quot;&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_time&#039; =&gt; &quot;&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_time&#039; =&gt; &quot;&quot;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;event_visbile&#039; =&gt; 1
&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;}
&nbsp;&nbsp;//if there is a row than take that row and convert the date/time to the input format and return it
&nbsp;&nbsp;return array(
&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_date&#039; =&gt; $this-&gt;format_date($row[&#039;start_date&#039;]),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_date&#039; =&gt; $this-&gt;format_date($row[&#039;end_date&#039;]),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_time&#039; =&gt; $this-&gt;format_time($row[&#039;start_time&#039;]),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_time&#039; =&gt; $this-&gt;format_time($row[&#039;end_time&#039;]),
&nbsp;&nbsp;&nbsp;&nbsp;&#039;event_visbile&#039; =&gt; $row[&#039;event_visible&#039;]
&nbsp;&nbsp;);
}</code></pre></p>
<h2>Modify Our Event_options</h2>
<p>Now that we have our method created within our class, we need to modify our event_option function within custom_event_calendar.php.  </p>
<p>This will begin by making sure that the $post global is pulled into our function.  Then will need to include our file containing our class.  We will create a new instance our class Manage_events.  Next, we will use the method created above to get the data with the $post->ID.  Then all left to do is change the values within our inputs to the associated key within our array.  Here is the completed function:</p>
<p><pre><code>

function event_meta_options(){

&nbsp;&nbsp;global $post;
&nbsp;&nbsp;
&nbsp;&nbsp;include(EVENT_CALENDAR_DIR . &quot;event_class.php&quot;);

&nbsp;&nbsp;$event = new Manage_events;
&nbsp;&nbsp;
&nbsp;&nbsp;$data = $event-&gt;get_event($post-&gt;ID);

&nbsp;&nbsp;$table = &quot;&lt;table&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;tr&gt;&lt;td&gt;&lt;label for=&#039;start_date&#039;&gt;Start Date&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input class=&#039;date_field&#039; type=&#039;text&#039; name=&#039;start_date&#039; value=&#039;&quot; . $data[&#039;start_date&#039;] . &quot;&#039; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;tr&gt;&lt;td&gt;&lt;label for=&#039;end_date&#039;&gt;End Date&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input class=&#039;date_field&#039; type=&#039;text&#039; name=&#039;end_date&#039; value=&#039;&quot; . $data[&#039;end_date&#039;] . &quot;&#039; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;tr&gt;&lt;td&gt;&lt;label for=&#039;start_time&#039;&gt;Start Time&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input class=&#039;time_field&#039; type=&#039;text&#039; name=&#039;start_time&#039; value=&#039;&quot;. $data[&#039;start_time&#039;] .&quot;&#039; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;tr&gt;&lt;td&gt;&lt;label for=&#039;end_time&#039;&gt;End Time&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input&nbsp;&nbsp;class=&#039;time_field&#039; type=&#039;text&#039; name=&#039;end_time&#039; value=&#039;&quot;. $data[&#039;end_time&#039;] .&quot;&#039; /&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;tr&gt;&lt;td&gt;&lt;label for=&#039;event_visible&#039;&gt;Show Event:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;select name=&#039;event_visible&#039;&gt;&lt;option value=&#039;1&#039;&gt;Yes&lt;/option&gt;&lt;option value=&#039;0&#039;&gt;No&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
&nbsp;&nbsp;$table .= &quot;&lt;/table&gt;&quot;;
&nbsp;&nbsp;
&nbsp;&nbsp;echo $table; 
}</code></pre></p>
<p>Now when we have event data already saved for an event, we go to edit the post we will have the form repopulated and see this:<br />
<img src="http://www.wpinsideout.com/wp-content/uploads/2010/09/meta_box.png" alt="" title="meta_box" width="283" height="188" class="alignnone size-full wp-image-227" /></p>
<h3>What Next Next for our Event Calendar Plugin</h3>
<p>Now that we have the majority of the backend of the event calendar plugin complete, we can move to creating the front end functionality.  We will next focus on the URL structure of our site and how we can get some way to use rewrite rules to help get this done.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/repopulating-the-event-meta-box/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Day Five – Including a Class to Handle Saving Data</title>
		<link>http://www.wpinsideout.com/day-five-including-a-class-to-handle-saving-data</link>
		<comments>http://www.wpinsideout.com/day-five-including-a-class-to-handle-saving-data#comments</comments>
		<pubDate>Fri, 24 Sep 2010 03:06:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=219</guid>
		<description><![CDATA[In the last day of this WordPress event calendar series, we added a custom meta box to our event post, but what we need to do now is take the data inputted by the user and save it to the &#8230; <a href="http://www.wpinsideout.com/day-five-including-a-class-to-handle-saving-data">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the last day of this WordPress event calendar series, we added a custom meta box to our event post, but what we need to do now is take the data inputted by the user and save it to the database.  The method that we will is to create a PHP class to manage this action.<br />
Creating the Class</p>
<p>To create our class, I am going to create a new file which I call event_class.php.  Within this PHP file, I added the following code:</p>
<p><pre><code>&lt;?php

if(!class_exists(‘event_class’)){
&nbsp;&nbsp;
&nbsp;&nbsp;class Event_class{

&nbsp;&nbsp;}
}</code></pre></p>
<p>The code starts out with some opening PHP tags and then uses the function class_exists() to see if the class we are about to create already exists.  This is optional, but is there to prevent errors in case we try to reload the class after it has already been included.  Then I created the class called “Event_class”.  Now that this is complete we can start adding our methods to the class.</p>
<h3>A Conflict of Formats</h3>
<p>You might have noticed that the inputs for start date and end date in the meta box are in a format more familiar to users, month/day/year, but the MySQL format is different for dates.  The fields in MySQL for dates use the following format: year-month-day.  So a date input as 9/23/2010 would need to be formatted to 2010-09-23 to be saved to the database.  That means we will need methods within our class to not only take the inputted format and convert it to the MySQL format but back out of MySQL format to the inputted format.   </p>
<p>Also, the inputted time has a different format than the MySQL format for time.  The inputted time uses AM and PM with a 1 – 12 scale, but MySQL is a 24 clock which also includes seconds.  So this means will need two more methods in our class to covert the inputted time to the MySQL format and back from the MySQL format back to the inputted format.  </p>
<p>With all that being said, I know that I need 4 methods off the bat to the class that do nothing that convert dates and times form one format to another.  I am not going to walk through the code with you. The code is below and is well commented.  If you have questions please post them in the comments and I will answer them the best I can.  </p>
<h5>Convert dates from inputted format to MySQL date format</h5>
<p><pre><code>function mysql_date($date){
&nbsp;&nbsp;list($month, $day, $year) = explode(&quot;/&quot;, $date);
&nbsp;&nbsp;return $year . &quot;-&quot; . $month . &quot;-&quot; . $day;
}

&lt;h5&gt;Convert dates from MySQL to the inputted format&lt;/h5&gt;

&lt;code&gt;function format_date($date){
&nbsp;&nbsp;list($year, $month, $day) = explode(&quot;-&quot;, $date);
&nbsp;&nbsp;return $month . &quot;/&quot; . $day . &quot;/&quot; . $year;
}</code></pre></p>
<h5>Convert time from inputted format to MySQL format</h5>
<p><pre><code>function mysql_time($time){
&nbsp;&nbsp;list($time, $am_pm) = explode(&quot; &quot;, $time);
&nbsp;&nbsp;list($hour, $minute) = explode(&quot;:&quot;, $time);

&nbsp;&nbsp;//just make sure the hour is an integer
&nbsp;&nbsp;$hour = (int) $hour;
&nbsp;&nbsp;//we use this in our conditional statements below
&nbsp;&nbsp;$is_am&nbsp;&nbsp; = FALSE;
&nbsp;&nbsp;//use strtoupper to make the test uppercase to test to see if it AM
&nbsp;&nbsp;if(strtoupper($am_pm) == &quot;AM&quot;){
&nbsp;&nbsp;&nbsp;&nbsp;$is_am = TRUE;
&nbsp;&nbsp;}
&nbsp;&nbsp;//test to see if is the 12 am 
&nbsp;&nbsp;if($hour == 12 &amp;&amp; $is_am){
&nbsp;&nbsp;&nbsp;&nbsp;$hour = 0;
&nbsp;&nbsp;}
&nbsp;&nbsp;//test to see if it is the afternoon and if is add 12
&nbsp;&nbsp;if($hour &lt; 12 &amp;&amp; $hour &gt; 0 &amp;&amp; !$is_am){
&nbsp;&nbsp;&nbsp;&nbsp;$hour = $hour + 12;
&nbsp;&nbsp;}
&nbsp;&nbsp;return $hour . &quot;:&quot; . $minute . &quot;:&quot; . &quot;00&quot;;
}</code></pre></p>
<h5>Convert MySQL Time format back to input format</h5>
<p><pre><code>function format_time($time){
&nbsp;&nbsp;list($hour, $minute, $second) = explode(&quot;:&quot;, $time);

&nbsp;&nbsp;//make sure the hour is an integer
&nbsp;&nbsp;$hour = (int) $hour;

&nbsp;&nbsp;$am_pm = &quot;AM&quot;;

&nbsp;&nbsp;//is the hour in the afternoon?
&nbsp;&nbsp;if($hour &gt; 12){
&nbsp;&nbsp;&nbsp;&nbsp;$hour = $hour - 12;
&nbsp;&nbsp;&nbsp;&nbsp;$am_pm = &quot;PM&quot;;
&nbsp;&nbsp;}

&nbsp;&nbsp;//is it 12 in the morning?
&nbsp;&nbsp;if($hour == 0){
&nbsp;&nbsp;&nbsp;&nbsp;$hour = 12;
&nbsp;&nbsp;}

&nbsp;&nbsp;return $hour . &quot;:&quot; . $minute . &quot; &quot; . $am_pm;
}</code></pre></p>
<p>Seems like a lot but these functions will seamlessly get the format we want without having to force the user to use some format that they are unfamiliar like year-month-day or a 24 hour clock.  </p>
<h3>Preventing Form Submission Errors</h3>
<p>Another issue we need to deal with is: if the user inputs a date or time that really isn’t a date or time or a date or time that we didn’t plan on converting.  This would be the case if the user tried to enter “January 25, 2011” or the user just enters something that doesn’t even resemble a date.    We then don’t want to save this data to the database because we can’t convert to the correct format.  Also, if we don’t this, potential errors might happen when the post is saved.  So the methods we will add to our class is to test to see if the inputted date and inputted time are in the correct format that we need before we convert it and save it to the database table.  </p>
<p>The two methods that will add simply return true or false use preg_match and a regular expression.  Below you will find the two functions for testing the date format and time format.</p>
<h5>Is it a valid date inputted?</h5>
<p><pre><code>function valid_date($date){
&nbsp;&nbsp;return preg_match(&#039;/(\d){1,2}/(\d){1,2}/(\d){4}/&#039;, $date) ? TRUE : FALSE;
}</code></pre></p>
<h5>Is it a valid time inputted?</h5>
<p><pre><code>function valid_time($time){
&nbsp;&nbsp;return preg_match(&#039;/(\d){1,2}:(\d){2}\s(AM|PM|am|pm)/&#039;, $time) ? TRUE : FALSE;
}</code></pre></p>
<h3>Class Method to Save the Event information</h3>
<p>The next to add to our class is a method to save the event data, start_time, end_time, start_date, end_date, and event_visible, to the database.  The method is going to accept a variable, which will be an array of post ID, start_date, end_date, start_time, end_time, and  event_visible, that will pass to the method when saving the post.<br />
Then, I created a if statement, which uses the methods from above testing the data in the array, start_date, end_date, start_time, and end_time, to see if they are in the correct format.  If it is not then the function will return false and nothing will be saved.  </p>
<p>If the times and dates are in the correct formats, we will redefine start_day, end_day, start_time, and end_time within our array by using our MySQL convert methods, mysql_date and mysql_time.  </p>
<p>Once the values in the array are in the correct format for MySQL, then it will then move to either updating a pre-existing row or inserting a new row.  We do this by first calling in the $wpdb global database object and then defining a variable called $table which contains the name our event table.  </p>
<p>After that we will try to get a row where the post_id is equal to the post_id in our inputted array.  If $row is empty, then just insert the $data and return TRUE.</p>
<p>“Else”, means that there is a row that already exists so we will use the update method in the $wpdb database object.  This will first accept the name of our $table and then the $data array.  Next, is an array that indicates the where statement.  Then an array for the format of the data we are updating, “%s” for stings and “%d” for integers.  So the post_id is an intger so “%d” but the date and time fields are considered strings or “%s”.  And lastly, an array is inputted which represents the format of the where clause, so again post_id is an integer or “%d”.  Then we just return true.  </p>
<p>Here is the completed function:</p>
<p><pre><code>function save($data){

&nbsp;&nbsp;if($this-&gt;valid_date($data[&#039;start_date&#039;]) &amp;&amp; $this-&gt;valid_date($data[&#039;end_date&#039;]) &amp;&amp; $this-&gt;valid_time($data[&#039;start_time&#039;]) &amp;&amp; $this-&gt;valid_time($data[&#039;end_time&#039;])){

&nbsp;&nbsp;&nbsp;&nbsp;$data[&#039;start_date&#039;] = $this-&gt;mysql_date($data[&#039;start_date&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;$data[&#039;end_date&#039;] = $this-&gt;mysql_date($data[&#039;end_date&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;$data[&#039;start_time&#039;] = $this-&gt;mysql_time($data[&#039;start_time&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;$data[&#039;end_time&#039;] = $this-&gt;mysql_time($data[&#039;end_time&#039;]);

&nbsp;&nbsp;&nbsp;&nbsp;global $wpdb;

&nbsp;&nbsp;&nbsp;&nbsp;$table = $wpdb-&gt;prefix . &quot;our_events&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;$row = $wpdb-&gt;get_row(&quot;SELECT * FROM {$table} WHERE post_id = &quot; . $data[&#039;post_id&#039;], ARRAY_A);

&nbsp;&nbsp;&nbsp;&nbsp;//if the row is empty insert the data
&nbsp;&nbsp;&nbsp;&nbsp;if(empty($row)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$wpdb-&gt;insert( $table, $data );

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;
&nbsp;&nbsp;&nbsp;&nbsp;}else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$wpdb-&gt;update($table, $data, array(&#039;post_id&#039; =&gt; $data[&#039;post_id&#039;]), array(&#039;%d&#039;, &#039;%s&#039;, &#039;%s&#039;, &#039;%s&#039;, &#039;%s&#039;, &#039;%d&#039;), array(&#039;%d&#039;));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;}
&nbsp;&nbsp;return FALSE;
}</code></pre></p>
<h3>Save the Post &#8211; add_action &#8211; save_post</h4>
<p>Yes, we are still not done.  We need to now create a user defined function back in our main plugin file: custom_event_calendar.php.  Our user defined function will also need to be called, so we are going to user the function add_action with the hook “save_post” to call our function.  </p>
<p>The user defined function I named “save_event_data”.  It starts off with an if statement that tests the event_type to see if it is an “event”.  If it is not, there is no need to do anything, but if it is an “event”, we will need to include our class.  I then used our constant for the absolute path to our file that holds our class: event_class.php.  I then created a new instance of our class and stored in the variable $event.  Then I created an array with data we need to save from the superglobal $_POST.  Lastly, I passed the array to the save method within our class.  </p>
<p><pre><code>add_action(&#039;save_post&#039;, &#039;save_event_data&#039;);

function save_event_data(){
&nbsp;&nbsp;
&nbsp;&nbsp;if($_POST[&#039;post_type&#039;] == &quot;event&quot;){
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;include(EVENT_CALENDAR_DIR . &quot;event_class.php&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$event = new Manage_events;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$data = array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;post_id&#039; =&gt; $_POST[&#039;ID&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_date&#039; =&gt; $_POST[&#039;start_date&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_date&#039; =&gt; $_POST[&#039;end_date&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;start_time&#039; =&gt; $_POST[&#039;start_time&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;end_time&#039; =&gt; $_POST[&#039;end_time&#039;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;event_visible&#039; =&gt; $_POST[&#039;event_visible&#039;]
&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$event-&gt;save($data);
&nbsp;&nbsp;}
}</code></pre></p>
<p>Now, when an event is saved and there is valid data in the meta box it will be saved to the database.   So done!</p>
<h3>Moving Forward With Our Event Calendar Plugin</h3>
<p>Okay, that is a lot to make all the things happen.  But using a class like we did will ensure consistent results and one place to go back to if we need to do edits or improve the code.  We actually are still not done, we saved the data but we need to now repopulate the form and this will be tomorrow’s discussion.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/day-five-including-a-class-to-handle-saving-data/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Day Four &#8211; Including JavaScript and Stylesheets In WordPress Admin</title>
		<link>http://www.wpinsideout.com/day-four-including-javascript-and-stylesheets</link>
		<comments>http://www.wpinsideout.com/day-four-including-javascript-and-stylesheets#comments</comments>
		<pubDate>Wed, 22 Sep 2010 01:10:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Plugin Development]]></category>

		<guid isPermaLink="false">http://www.wpinsideout.com/?p=206</guid>
		<description><![CDATA[In the last tutorial, we added a custom post type for our events and created a meta box. To enhance the user experience we are going to add some JavaScript to our meta box. Also, we will need to add &#8230; <a href="http://www.wpinsideout.com/day-four-including-javascript-and-stylesheets">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the last tutorial, we added a custom post type for our events and created a meta box.  To enhance the user experience we are going to add some JavaScript to our meta box.  Also, we will need to add some stylesheets.</p>
<p>The JavaScript that we will be adding are a datepicker and a time selector.  They will create popups when the user clicks on a specific field allowing them to quickly select a date on a calendar or input a time.  The datepicker can be found <a target="_blank" href="http://teddevito.com/demos/calendar.php">here</a> and the time selector <a href="http://pttimeselect.sourceforge.net/example/index.html" target="_blank">here</a>.  You will need not only the JavaScript but also the CSS for each.  </p>
<p><em>An important note about the time selector, the time selector from the above site might not work in the admin right out of the box.  This is due to JavaScript conflicts.  So I change the code a bit and here is a link to <a href="http://www.wpinsideout.com/wp-content/uploads/2010/09/timeselect.zip">download the file</a>.</em></p>
<p>So to begin adding our JavaScript and CSS, I am going to create a folder within our plugin called “js” to holder our JavaScript and a folder called &#8220;css&#8221; to hold our stylesheets.  Inside of each folder I will place the corresponding files.   </p>
<h3>Register the JavaScript and Stylesheets</h3>
<p>After placing the JavaScript and CSS in their folders, we need to now register them with WordPress.  So I will use add_action function to hook into WordPress&#8217; initialization to register the files.  I will also need two user defined functions which I called register_event_javascript for the scripts and register_event_css for the css.  </p>
<p>Within register_event_javascript we will use the function below to register the script.</p>
<p><code>wp_register_script(handle, $src, $deps, $ver, $in_footer); </code></p>
<ul>
<li><strong>$handle</strong> is the name of that we use to reference our JavaScript.  So for the datepicker, we can use the name “datepicker”.</li>
<li><strong>$src</strong> is going to be the URL path to our file.  We will use our defined constant EVENT_CALENDAR_URL plus javascript folder name “js” and the file name to create a path to the file.  </li>
<li><strong>$dep</strong> is an array that contains the javascript libraries that this JavaScript depends on  and in our case both with be jQuery.</li>
<li><strong>$ver</strong> is the version of the javascript we want to call.  This is optional.</li>
<li><strong>$in_footer</strong> is a Boolean which indicates it the JavaScript will be call in the footer.  This is optional as well.</li>
</ul>
<p>So here is the code: </p>
<p><pre><code>add_action(&#039;init&#039;, &#039;register_event_javascript&#039;);

function register_event_javascript(){
&nbsp;&nbsp;wp_register_script(&#039;datepicker&#039;, EVENT_CALENDAR_URL . &#039;js/datepicker.js&#039;, array(&#039;jquery&#039;));
&nbsp;&nbsp;wp_register_script(&#039;timeselect&#039;, EVENT_CALENDAR_URL . &#039;js/timeselect.js&#039;, array(&#039;jquery&#039;));
}</code></pre></p>
<p>Now that our JavaScript are registered, we can register our CSS and we will use a similar function that I will not cover but it is very similar to wp_register_script.</p>
<p><pre><code>add_action(&#039;init&#039;, &#039;register_event_css&#039;);

function register_event_css(){
&nbsp;&nbsp;wp_register_style(&#039;timeselect&#039;, EVENT_CALENDAR_URL . &#039;css/timeselect.css&#039;);
&nbsp;&nbsp;wp_register_style(&#039;datepicker&#039;, EVENT_CALENDAR_URL . &#039;css/datepicker.css&#039;);
}</code></pre></p>
<h3>Including Our Javascript and Stylesheets</h3>
<p>Having our JavaScript and Stylesheets registered with WordPress is only one half of what we need.  Now we to include them on the pages where will use them.  We need to first create two user defined functions; one for the CSS, event_admin_css,  and the other for the JavaScript, event_admin_javascript.  We will then use the add_action function to hook into ‘admin_print_scripts’ and ‘admin_print_styles’ and call these two functions.</p>
<p>Our first user defined function, event_admin_javascript, will first call in the global $post variable.  This will then be used to create an if statement where we will test to see if the post type is equal to event.  This will allow us to only load the JavaScript on the event post type pages within the admin.  Within our if statement, we will use the function wp_enqueue_script, which will accept the defined handle for the script that we want to include.  </p>
<p>Here is the code for just the JavaScript:</p>
<p><pre><code>add_action(&#039;admin_print_scripts&#039;, &#039;event_admin_javascript&#039;);

function event_admin_javascript(){
&nbsp;&nbsp;global $post;
&nbsp;&nbsp;
&nbsp;&nbsp;//test to see if the post type is event and if it is load the javascript
&nbsp;&nbsp;if($post-&gt;post_type == &#039;event&#039;){
&nbsp;&nbsp;&nbsp;&nbsp;wp_enqueue_script(&#039;timeselect&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;wp_enqueue_script(&#039;datepicker&#039;);
&nbsp;&nbsp;}&nbsp;&nbsp;
}</code></pre></p>
<p>Now that is complete we need to bring in the css to make it function correctly.  The code for the css is below and you will only notice one change.  We changed the function from wp_enqueue_script to wp_enqueue_style inputing both handles for our css files.  </p>
<p><pre><code>add_action(&#039;admin_print_styles&#039;, &#039;event_admin_css&#039;);

function event_admin_css(){
&nbsp;&nbsp;global $post;

&nbsp;&nbsp;//test to see if the post type is event and if it is load the javascript
&nbsp;&nbsp;if($post-&gt;post_type == &#039;event&#039;){
&nbsp;&nbsp;&nbsp;&nbsp;wp_enqueue_style(&#039;timeselect&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;wp_enqueue_style(&#039;datepicker&#039;);
&nbsp;&nbsp;}
}</code></pre></p>
<h3>Calling the JavaScript</h3>
<p>The last step to get our JavaScript to work is to call the actual functions.  Now, the one thing we need to make use of is the selectors that we used inside our meta box for the event options.  If you look back over the tutorial from yesterday, you will see that I gave the inputs for start and end dates a class of &#8220;date_field&#8221; and the input for start and end times a class of &#8220;time_field&#8221;.  This will allow us to call our apply our JavaScript to those specific fields.  If you didn&#8217;t do that you need to do it before moving forward.  </p>
<p>The next step is to create another user defined function that will echo out the JavaScript we are calling.  I called the function call_event_javascript and it uses the action hook admin_footer to be called.  Here is the code:</p>
<p><pre><code>add_action(&#039;admin_footer&#039;, &#039;call_event_javascript&#039;);

function call_event_javascript(){
&nbsp;&nbsp;global $post;
&nbsp;&nbsp;//test to see if the post type is event and if it is call the javascript
&nbsp;&nbsp;if($post-&gt;post_type == &#039;event&#039;){
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;script type=&#039;text/javascript&#039;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jQuery(document).ready(function(){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jQuery(&#039;input.time_field&#039;).ptTimeSelect();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jQuery(&#039;input.date_field&#039;).simpleDatepicker();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/script&gt;&quot;;
&nbsp;&nbsp;}
}</code></pre></p>
<p>First, looking at the code above you might notice that I didn&#8217;t use the $ sign so often used with Jquery.  I did this because this can cause conflicts.  Also, I again test to see if the post type is equal to event before I actually echo the JavaScript.  </p>
<p>That is it and here is the datepicker result:</p>
<p><img src="http://www.wpinsideout.com/wp-content/uploads/2010/09/date_picker.jpg" alt="" title="date_picker" width="280" height="296" class="alignnone size-full wp-image-208" /></p>
<p>And here is the time selector:</p>
<p><img src="http://www.wpinsideout.com/wp-content/uploads/2010/09/time_selector.jpg" alt="" title="time_selector" width="311" height="325" class="alignnone size-full wp-image-209" /></p>
<h3>The Next Steps</h3>
<p>While adding JavaScript to our meta box might improve the user&#8217;s experience, we still haven&#8217;t addressed how we are going to save this data to our database.  This is the next step.  We are going to create a class to handle all the functions we will need.  This will include converting the inputted time to a MySQL format and then back to the inputted format.  Also, saving the data to the database and getting the data.  </p>
<p>Also, here is the files so far: <a href="http://www.wpinsideout.com/wp-content/uploads/2010/09/custom_event_calendar1.zip">custom event calendar files.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wpinsideout.com/day-four-including-javascript-and-stylesheets/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

