<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:series="http://unfoldingneurons.com/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Wptuts+</title> <link>http://wp.tutsplus.com</link> <description>WordPress Tutorials</description> <lastBuildDate>Tue, 18 Jun 2013 15:11:18 +0000</lastBuildDate> <language /> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.5</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Wptuts" /><feedburner:info uri="wptuts" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Wptuts</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><title>Tuts+ Jobs is now free!</title><link>http://feedproxy.google.com/~r/Wptuts/~3/x_HpYVj9RXA/</link> <comments>http://wp.tutsplus.com/articles/news/tuts-jobs-is-now-free/#comments</comments> <pubDate>Tue, 18 Jun 2013 15:11:18 +0000</pubDate> <dc:creator>Joel Bankhead</dc:creator> <category><![CDATA[News]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=32049</guid> <description>&lt;p&gt;&lt;strong&gt;Our awesome &lt;a
href="https://jobs.tutsplus.com/"&gt;new job board&lt;/a&gt; is now free and full of enticing opportunities!&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;a
href="https://jobs.tutsplus.com/"&gt;Tuts+ Jobs&lt;/a&gt; is a job board for full time, part time and casual employment opportunities for web and creative professionals. A brand new site to go alongside the &lt;a
href="http://tutsplus.com/"&gt;Tuts+ Educational Network&lt;/a&gt; and the &lt;a
href="http://themeforest.net/"&gt;Envato Marketplaces&lt;/a&gt;, all run by &lt;a
href="http://envato.com/"&gt;Envato&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;There&amp;#8217;s no need to sign up to apply for jobs, and it&amp;#8217;s now free to post a job &amp;#8211; &lt;a
href="https://jobs.tutsplus.com/"&gt;try it now&lt;/a&gt;!&lt;br
/&gt; &lt;span
id="more-32049"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Earn and Learn&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Envato is committed to helping people earn and learn online.&lt;/strong&gt; We provide a wealth of educational material through Tuts+ and Tuts+ Premium, alongside the Envato Marketplaces to help you benefit from the creative skills you learn.&lt;/p&gt;&lt;p&gt;&lt;a
href="https://jobs.tutsplus.com/"&gt;Tuts+ Jobs&lt;/a&gt; furthers this vision perfectly. With Tuts+ you can learn the skills you need to become a creative professional, and with Tuts+ Jobs you can find an amazing job that uses those skills. Our goal here is to take the hassle out of finding a job, so we&amp;#8217;ve made the process as simple as can be!&lt;/p&gt; &lt;figure
class="tutorial_image"&gt; &lt;a
href="http://net.tutsplus.com/?attachment_id=32578" rel="attachment wp-att-32578"&gt;&lt;img
src="http://cdn.tutsplus.com/net.tutsplus.com/uploads/2013/06/600.png" alt="600" width="600" height="189" class="alignnone size-full wp-image-32578" /&gt;&lt;/a&gt;&lt;br
/&gt; &lt;/figure&gt;&lt;hr
/&gt;&lt;h2&gt;Finding a Job&lt;/h2&gt;&lt;p&gt;With &lt;a
href="https://jobs.tutsplus.com/"&gt;Tuts+ Jobs&lt;/a&gt; we&amp;#8217;re committed to making every step of the job-finding process more intuitive, simpler and more efficient. We don&amp;#8217;t want to stand between you and a great opportunity.&lt;/p&gt;&lt;p&gt;It&amp;#8217;s totally free to sign up and start applying for jobs, and if nothing matches your initial search you can receive notifications when jobs come up that match your criteria. We will have full time, part time and casual listings from all around the world. Check the listing to find out where the job is based, as some jobs may offer remote opportunities.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt; &lt;a
href="http://net.tutsplus.com/?attachment_id=32590" rel="attachment wp-att-32590"&gt;&lt;img
src="http://cdn.tutsplus.com/net.tutsplus.com/uploads/2013/06/2.jpg" alt="2" width="600" height="353" class="alignnone size-full wp-image-32590" /&gt;&lt;/a&gt;&lt;br
/&gt; &lt;/figure&gt;&lt;hr
/&gt;&lt;h2&gt;Posting a Job&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Posting a job is now free!&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;If you have a position that you need to fill, or a great part/full-time opportunity, then Tuts+ Jobs is the best way to find a talented and creative individual to do that job. Each 30-day listing is completely free, regardless of the type of job you&amp;#8217;re posting.&lt;/p&gt;&lt;p&gt;Your job listing will be promoted across the entire Tuts+ network. With exposure to our ten million visitors over the course of 30 days, it&amp;#8217;s a brilliant way to reach exactly the type of audience you&amp;#8217;re looking for.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Sign Up, Explore, and Subscribe&lt;/h2&gt;&lt;p&gt;There are lots of great opportunities already on Tuts+ Jobs, and we&amp;#8217;ll be posting even more exciting job positions over the next few weeks, so now is the best time to head over to the site and &lt;a
href="https://jobs.tutsplus.com/"&gt;subscribe to a job search&lt;/a&gt; that interests you. That way you&amp;#8217;ll be the first to know when any new listings show up that match your skill set and requirements.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;a
href="https://jobs.tutsplus.com"&gt;Check out Tuts+ Jobs&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/x_HpYVj9RXA" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/news/tuts-jobs-is-now-free/feed/</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://wp.tutsplus.com/articles/news/tuts-jobs-is-now-free/</feedburner:origLink></item> <item><title>An Introduction to Thesis: Overview</title><link>http://feedproxy.google.com/~r/Wptuts/~3/NuSYHK6zFIQ/</link> <comments>http://wp.tutsplus.com/tutorials/theme-development/an-introduction-to-thesis-overview/#comments</comments> <pubDate>Mon, 17 Jun 2013 13:33:55 +0000</pubDate> <dc:creator>Sarah Frantz</dc:creator> <category><![CDATA[Theme Development]]></category> <category><![CDATA[theme frameworks]]></category> <category><![CDATA[Thesis]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31868</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 1 of 1 in the series &lt;a
href="http://wp.tutsplus.com/series/an-introduction-to-thesis/" class="series-1134" title="An Introduction to Thesis"&gt;An Introduction to Thesis&lt;/a&gt;&lt;/div&gt;&lt;p&gt;If you develop with WordPress you have probably heard the term &amp;#8220;framework&amp;#8221; before, and chances are you&amp;#8217;ve developed with one.&lt;/p&gt;&lt;p&gt;If you haven&amp;#8217;t, you&amp;#8217;re in luck! This tutorial will walk you through the ins and outs of one of the most popular WordPress frameworks: A framework that over 55,000+ people are using &amp;#8211; &lt;a
href="http://diythemes.com/" title="Thesis for WordPress" target="_blank"&gt;Thesis&lt;/a&gt;.&lt;span
id="more-31868"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Thesis was created by &lt;a
href="https://twitter.com/pearsonified" title="Follow Chris Pearson on Twitter" target="_blank"&gt;Chris Pearson&lt;/a&gt; and launched back in March of 2008. The framework at its core was built to be &amp;#8220;search engine friendly&amp;#8221; and extremely fast loading, however many would argue that Thesis&amp;#8217; best feature is its Visual Design Template Editor.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://wp.tutsplus.com/?attachment_id=31872" rel="attachment wp-att-31872"&gt;&lt;img
class="alignnone  wp-image-31872" alt="thesis-settings" src="http://cdn.tutsplus.com/wp.tutsplus.com/uploads/2013/05/thesis-settings.jpg" width="466" height="126" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;p&gt;To fully understand how to develop with Thesis, it’s best to dissect and familiarize yourself with the Thesis settings and the template editor. It&amp;#8217;s also important to grasp the concept of boxes, skins and packages &amp;#8211; which is what this tutorial series will help you understand by walking you through creating your first, customized Thesis skin.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;What We&amp;#8217;ll Be Covering&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Thesis Building Blocks &amp;amp; User Interface&lt;/strong&gt; &amp;#8211; Introducing you to the Thesis user interface as well as building blocks associated with developing for WordPress on the Thesis Framework.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Setting Up Site Information &amp;amp; Useful Tips&lt;/strong&gt; &amp;#8211; A run-down of available &amp;#8220;site&amp;#8221; options you can control using the Thesis Framework as well as tips for setting up your Thesis site. We&amp;#8217;ll also go through the initial stage of creating your skin for development.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Skins &amp;amp; Layout Structure&lt;/strong&gt; &amp;#8211; A comprehensive walk through focusing on creating and assigning page templates, re-using your templates for further development of other pages, and how to prepare your skin for packages.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;CSS &amp;amp; Packages&lt;/strong&gt; &amp;#8211; This section will focus on teaching you how to create and apply a package to your Thesis skin as well as go over how to use the more complicated packages, such as columns.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For the sake of those reading, this tutorial assumes that you’re already familiar with WordPress theme development, HTML and CSS; however, even the most novice user can figure out the basics of creating a skin for their website as Thesis is very &amp;#8220;end user oriented.&amp;#8221;&lt;/p&gt;&lt;p&gt;In fact, the entire framework utilizes “drag and drop” for just about everything, making adjustments is as easy as holding the shift key and dragging boxes around the template editor.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;The Building Blocks of Thesis&lt;/h2&gt;&lt;p&gt;To build your WordPress site using Thesis you need to first understand the basic components of the Thesis Framework. Thesis consists of three main elements:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Skins&lt;/strong&gt; &amp;#8211; Skins are essentially the same concept as WordPress themes; however, a Thesis skin utilizes all of the WordPress functionality from the get go &amp;#8211; that is to say that skins are rooted into WordPress in such a way that you can do more than just &amp;#8220;theme&amp;#8221; your site, you can create admin pages, control functionality and much more.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://wp.tutsplus.com/?attachment_id=31873" rel="attachment wp-att-31873"&gt;&lt;img
class="alignnone  wp-image-31873" alt="thesis-skin-editor" src="http://cdn.tutsplus.com/wp.tutsplus.com/uploads/2013/05/thesis-skin-editor.jpg" width="504" height="472" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Boxes&lt;/strong&gt; &amp;#8211; Boxes are very similar to &amp;#8220;plugins&amp;#8221; or &amp;#8220;widgets&amp;#8221; that can be used in conjunction with your skin. They are bits of code that add dynamic functionality to your site &amp;#8211; for example, you could create a box for a content slider, form, author information, HTML structure, etc.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://wp.tutsplus.com/?attachment_id=31874" rel="attachment wp-att-31874"&gt;&lt;img
class="alignnone  wp-image-31874" alt="thesis-boxes" src="http://cdn.tutsplus.com/wp.tutsplus.com/uploads/2013/05/thesis-boxes.jpg" width="480" height="192" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Packages&lt;/strong&gt; &amp;#8211; Packages add style to all aspects of your skin without actually having to dive into CSS code. In fact, Thesis goes so far as to provide users with an easy interface that allows those who aren&amp;#8217;t CSS ninjas to input their own margins, borders, widths, heights, etc. making customization easy for beginners.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://wp.tutsplus.com/?attachment_id=31875" rel="attachment wp-att-31875"&gt;&lt;img
class="alignnone  wp-image-31875" alt="thesis-packages" src="http://cdn.tutsplus.com/wp.tutsplus.com/uploads/2013/05/thesis-packages.jpg" width="572" height="289" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;It&amp;#8217;s important to also point out that Thesis comes with a rather robust section dedicated solely to handling your website’s other pertinent information, such as built in support for Google Analytics tracking codes, 404 page handling, and SEO options (i.e. setting your title tag, meta description, keywords and robot options).&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Working together, Thesis skins, boxes and packages seamlessly create a WordPress website that utilizes all of the plugin/widget functionality you need &amp;#8211; without all the load time and in a very &amp;#8220;search engine friendly&amp;#8221; way.&lt;/p&gt;&lt;p&gt;Now that you have a basic understanding of Thesis, in our next installment we will dive deeper into setting up our site information using the Thesis settings and start building our first Thesis skin.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/NuSYHK6zFIQ" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/tutorials/theme-development/an-introduction-to-thesis-overview/feed/</wfw:commentRss> <slash:comments>9</slash:comments> <series:name><![CDATA[An Introduction to Thesis]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/tutorials/theme-development/an-introduction-to-thesis-overview/</feedburner:origLink></item> <item><title>The WordPress Coding Standards: Indentation, Space Usage, and Trailing Spaces</title><link>http://feedproxy.google.com/~r/Wptuts/~3/SP7nUpqV75Q/</link> <comments>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-indentation-space-usage-and-trailing-spaces/#comments</comments> <pubDate>Thu, 13 Jun 2013 13:07:52 +0000</pubDate> <dc:creator>Tom McFarlin</dc:creator> <category><![CDATA[Cheat Sheets]]></category> <category><![CDATA[codex]]></category> <category><![CDATA[Coding Standards]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31695</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 4 of 4 in the series &lt;a
href="http://wp.tutsplus.com/series/the-wordpress-coding-standards/" class="series-1128" title="The WordPress Coding Standards"&gt;The WordPress Coding Standards&lt;/a&gt;&lt;/div&gt;&lt;p&gt;The entire purpose of this series to help expose the &lt;a
title="WordPress Coding Standards" href="http://make.wordpress.org/core/handbook/coding-standards/php/" target="_blank"&gt;WordPress Coding Standards&lt;/a&gt;, why they matter, and how to write quality WordPress code. In order to do this, we&amp;#8217;re taking an in-depth look at each section of the WordPress Coding Standards.&lt;/p&gt;&lt;p&gt;So far, we&amp;#8217;ve covered:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Naming Conventions and Function Arguments&lt;/li&gt;&lt;li&gt;Single Quotes and Double Quotes&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Today, we&amp;#8217;re going to be covering the importance of white space. Specifically, we&amp;#8217;re going to cover indentation, space usage, and trailing spaces. As easy as it sounds, these are several of the most ignored or misused aspects of the codings standards.&lt;span
id="more-31695"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;A Word About Whitespace&lt;/h2&gt;&lt;p&gt;Before we look at what&amp;#8217;s going on with the various coding standards, it&amp;#8217;s important to understand why white space is important not only in WordPress, but in programming languages in general.&lt;/p&gt;&lt;p&gt;Simply put, it&amp;#8217;s because it enhances readability.&lt;/p&gt;&lt;p&gt;This is why some programming languages are space or tab delimited, this is why some programming languages ask that you space out certain parts of the code such as function parameters, array indexes, and more.&lt;/p&gt;&lt;p&gt;When it comes to WordPress, the conventions are in place not only for readability, but also to provide a consistent reading and development experience for all who work on WordPress themes, plugins, applications, or the core application itself.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; The purpose of coding standards is to make it so that source code looks as if it&amp;#8217;s written by a single developer as it sets a level of expectations for contributing developers.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Indentation&lt;/h2&gt;&lt;p&gt;As far as indentation is concerned, there&amp;#8217;s nothing particularly new, revolutionary, or different as it relates to WordPress. Generally speaking, you&amp;#8217;ll indent each time that you start a new block.&lt;/p&gt;&lt;p&gt;This means that:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Your functions will be indented within the class&lt;/li&gt;&lt;li&gt;Your conditionals and switch/cases and other blocks will be indented within their functions&lt;/li&gt;&lt;li&gt;Your loops will be indented within their functions, within their conditionals, and so on&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;If you&amp;#8217;re used to writing code in C-style languages, then there&amp;#8217;s nothing particularly new here, right?&lt;/p&gt;&lt;p&gt;An example of what this may look like is exactly this:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function foo( $arguments ) {

	if ( 0 &amp;lt; count( $arguments ) {

		foreach ( $arguments as $arg =&amp;gt; $value ) {
			echo $value;
		}

	}

}
&lt;/pre&gt;&lt;p&gt;According to the Coding Standards:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Your indentation should always reflect logical structure. Use &lt;strong&gt;real tabs&lt;/strong&gt; and &lt;strong&gt;not spaces&lt;/strong&gt;, as this allows the most flexibility across clients.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The key take aways are that the beginning of lines should begin with tabs, and that the code should reflect a &lt;em&gt;logical&lt;/em&gt; structure. Don&amp;#8217;t overcomplicate or try to oversimplify things.&lt;/p&gt;&lt;p&gt;There&amp;#8217;s one other nuance to white space within the WordPress Coding Standards: Tabs should be used at the beginning of the line, but spaces should be used everywhere else. For the most part, this is an easy rule to follow; however, in WordPress, you&amp;#8217;re often going to be creating arrays to pass as arguments.&lt;/p&gt;&lt;p&gt;Ideally, we want each index of the array to line up so that readability is as high as possible but we often have a propensity to use tabs to make it easier to line them up, but this is actually a violation of the coding standards.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
$args = array(
	'ID'           =&amp;gt; 1,
	'post_title'   =&amp;gt; 'The Title',
	'post_content' =&amp;gt; 'The content'
);
&lt;/pre&gt;&lt;p&gt;As such, make sure that you &lt;em&gt;only&lt;/em&gt; use tabs at the beginning of each line.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Space Usage&lt;/h2&gt;&lt;p&gt;As it relates to WordPress, I&amp;#8217;ve found that spaces have been used much more liberally than in other languages. I&amp;#8217;m not saying this simply as an observation, as it was a personal adjustment I needed to make in coming both from .NET and Ruby.&lt;/p&gt;&lt;p&gt;That said, as with most other guidelines in the Codex, there&amp;#8217;s nothing that&amp;#8217;s particularly complex; however, there are some important distinctions that need to be remembered when writing code.&lt;/p&gt;&lt;p&gt;Spaces should be placed in the following places:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;After commas&lt;/li&gt;&lt;li&gt;On both sides of logical operators (that is, &lt;code&gt;||&lt;/code&gt;, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, and &lt;code&gt;!&lt;/code&gt;)&lt;/li&gt;&lt;li&gt;On both sides of comparison operators (that is, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;===&lt;/code&gt;, etc.)&lt;/li&gt;&lt;li&gt;On both sides of assignment operators (namely &lt;code&gt;=&lt;/code&gt;)&lt;/li&gt;&lt;li&gt;On both both sides of the opening and closing parenthesis of functions, conditionals, loops, and so on.&lt;/li&gt;&lt;li&gt;When a variable is being passed as an index of an array, but &lt;em&gt;not&lt;/em&gt; when a literal value (such as a string or an integer)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Most of these are relatively straight forward, but in my experience I have found that the array rules tend to cause the most confusion among developers, so here&amp;#8217;s a quick note of what&amp;#8217;s considered correct and what&amp;#8217;s not:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
// Correct spacing...
$arr[ $x ] = 'foo';
$arr[0]    = 'bar';

// Incorrect spacing...
$arr[$x]  = 'bar';
$arr[ 0 ] = 'bar';
&lt;/pre&gt;&lt;p&gt;As you can see, it&amp;#8217;s nothing terribly complicated but it&amp;#8217;s a relatively easy convention to miss.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Trailing Spaces&lt;/h2&gt;&lt;p&gt;This rule is likely the easiest standard to remember of them all. Simply put, there should be no trailing spaces left at the end of any line of code.&lt;/p&gt;&lt;p&gt;Depending on your IDE, you may have this filter built in; other times, you may need to install a plugin that will perform this action for you whenever you save the file. But whatever the case, it&amp;#8217;s a relatively easy feature to integrate in whatever IDE you&amp;#8217;re using.&lt;/p&gt;&lt;p&gt;Additionally, most editors &amp;#8211; regardless of if it&amp;#8217;s a glorified text editor or a full on IDE &amp;#8211; you should have the ability to enable the display of tabs and spaces.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;img
class="aligncenter size-full wp-image-31697" alt="Viewing Tabs and Whitespace" src="http://cdn.tutsplus.com/wp.tutsplus.com/uploads/2013/05/tabs-whitespace.png" width="680" height="432" /&gt;&lt;/p&gt; &lt;figcaption&gt;Viewing Tabs and Whitespace in Coda 2&lt;/figcaption&gt; &lt;/figure&gt;&lt;p&gt;Again, this is one of the simplest conventions to follow and it&amp;#8217;s likely that your IDE or editor of choice has this feature or a plugin available that will allow you to automatically do this.&lt;/p&gt;&lt;p&gt;If not, you should be able to activate the ability to see tabs, spaces, carriage returns, and so on so that you can easily identify &lt;em&gt;where&lt;/em&gt; the trailing spaces are.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Obviously, white space places a significant role in writing WordPress-based code. In this article, we&amp;#8217;ve explored indentation, tabs, spaces, trailing spaces, and why and how to incorporate them into our projects.&lt;/p&gt;&lt;p&gt;In the next article, we&amp;#8217;re going to continue talking about the subtleties of the WordPress Coding Standards by looking at brace style, regular expressions, and the nuances of using PHP tags through our WordPress code.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/SP7nUpqV75Q" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-indentation-space-usage-and-trailing-spaces/feed/</wfw:commentRss> <slash:comments>5</slash:comments> <series:name><![CDATA[The WordPress Coding Standards]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-indentation-space-usage-and-trailing-spaces/</feedburner:origLink></item> <item><title>Integrating Multiple Choice Quizzes in WordPress – Creating the Backend</title><link>http://feedproxy.google.com/~r/Wptuts/~3/GYnzBFi2Dgk/</link> <comments>http://wp.tutsplus.com/tutorials/plugins/integrating-multiple-choice-quizzes-in-wordpress-creating-the-backend/#comments</comments> <pubDate>Wed, 12 Jun 2013 14:32:19 +0000</pubDate> <dc:creator>Rakhitha Nimesh</dc:creator> <category><![CDATA[Plugins]]></category> <category><![CDATA[plugin development]]></category> <category><![CDATA[quiz]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31221</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 1 of 1 in the series &lt;a
href="http://wp.tutsplus.com/series/integrating-multiple-choice-quizzes-in-wordpress/" class="series-1114" title="Integrating Multiple Choice Quizzes in WordPress"&gt;Integrating Multiple Choice Quizzes in WordPress&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Multiple choice questions are something that most of us have faced at least once in our life. We love them because we can provide correct answers by logically thinking about provided possibilities, even if we don&amp;#8217;t exactly know the correct answer. Also answering takes less time which makes it so popular.&lt;/p&gt;&lt;p&gt;Creating a multiple choice quiz in WordPress can be a very exciting and profitable task. You can use it in your personal blog to attract more visitors, or you can create a premium section with advanced quizzes, or you can create quizzes focusing on popular certification exams. There are numerous possibilities for making it profitable.&lt;span
id="more-31221"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;This plugin won&amp;#8217;t take you through to creating quizzes for building a business, but we have to start somewhere to get where we want to go. So this would be a good start for creating quizzes and making profits out of it.&lt;/p&gt;&lt;p&gt;So let&amp;#8217;s get started.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Functionality of the Plugin&lt;/h2&gt;&lt;p&gt;First we need to gather the requirements of the plugin before going to design or implementation. Basically, we should be able to create multiple choice quizzes and allow users to take the quizzes and get results.&lt;/p&gt;&lt;p&gt;So let&amp;#8217;s take a look at the detailed requirements and components of our plugin:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The plugin should have a backend where we can insert questions and answers dynamically. Also it&amp;#8217;s ideal to have quiz categories to group quizzes into specific sections.&lt;/li&gt;&lt;li&gt;Site admin should be able to configure the quizzes through a settings page.&lt;/li&gt;&lt;li&gt;Users should have a frontend where they can take a quiz.&lt;/li&gt;&lt;li&gt;Once a quiz is completed users should be able to get the scores and results.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This tutorial is going to be constructed as a two part series, where we develop the backend of the plugin in the first part, followed by the plugin frontend in the second part.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Planning the Plugin Backend&lt;/h2&gt;&lt;p&gt;In this part we are going to focus on developing the backend of the plugin where we develop the required data for quizzes.&lt;/p&gt;&lt;p&gt;Usually there is a list of quizzes, each of them containing a specific list of questions. With this plugin we are not going to create quizzes. We will be creating individual questions and assigning them dynamically to quizzes when requested.&lt;/p&gt;&lt;p&gt;Now we can take time to identify the components required for implementing the backend as listed in the following section.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Questions need to be created in the backend with the answers. A custom post type will be the best solution for implementing the questions. So we are going to use a custom post type called &lt;code&gt;wptuts_quiz&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;Each question should have multiple answers and one correct answer. Fields for answers will be created inside a meta box in the custom post creation screen.&lt;/li&gt;&lt;li&gt;Questions will be categorized into various sub-topics hence we need a custom taxonomy called &lt;code&gt;quiz_categories&lt;/code&gt; for the &lt;code&gt;wptuts_quiz&lt;/code&gt; post type.&lt;/li&gt;&lt;li&gt;Then we need to validate the question creation process. We will be using client side jQuery validations when necessary in question creation.&lt;/li&gt;&lt;li&gt;Finally we need a plugin settings page to store the number of questions per quiz and the duration for a quiz.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Having identified the necessary WordPress components, we can directly move into implementing the backend of the quiz plugin.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Creating Questions&lt;/h2&gt;&lt;p&gt;The question creation process consists of four main sections: defining custom post type, defining custom taxonomy, assigning custom fields, and validations. Each of these sections will be discussed with detailed code in the upcoming sections.&lt;/p&gt;&lt;h3&gt;1. Creating the Questions Custom Post Type&lt;/h3&gt;&lt;p&gt;We need the most basic form of custom post type for questions without any advanced configurations. The only thing we need to consider is the selection of the right field for the question.&lt;/p&gt;&lt;p&gt;The default post creation screen contains two fields for title and content. You can choose any of those fields for the question. I am going to choose concatenation of both title and content fields considering advanced possibilities.&lt;/p&gt;&lt;p&gt;We will be creating an object-oriented plugin instead of a functional plugin, so all of the necessary actions, shortcodes and initializations will be done inside the constructor. The following contains the code for implementing the &lt;code&gt;wptuts_quiz&lt;/code&gt; post type.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
class WP_Quiz {

	public $plugin_url;

	public function __construct() {

		$this-&amp;gt;plugin_url = plugin_dir_url( __FILE__ );

		add_action( 'init', array( $this, 'wpq_add_custom_post_type' ) );

	}

	public function wpq_add_custom_post_type() {

		$labels = array(
			'name' =&amp;gt; _x( 'Questions', 'wptuts_quiz' ),
			'menu_name' =&amp;gt; _x( 'WPTuts Quiz', 'wptuts_quiz' ),
			'add_new' =&amp;gt; _x( 'Add New ', 'wptuts_quiz' ),
			'add_new_item' =&amp;gt; _x( 'Add New Question', 'wptuts_quiz' ),
			'new_item' =&amp;gt; _x( 'New Question', 'wptuts_quiz' ),
			'all_items' =&amp;gt; _x( 'All Questions', 'wptuts_quiz' ),
			'edit_item' =&amp;gt; _x( 'Edit Question', 'wptuts_quiz' ),
			'view_item' =&amp;gt; _x( 'View Question', 'wptuts_quiz' ),
			'search_items' =&amp;gt; _x( 'Search Questions', 'wptuts_quiz' ),
			'not_found' =&amp;gt; _x( 'No Questions Found', 'wptuts_quiz' ),
		);

		$args = array(
			'labels' =&amp;gt; $labels,
			'hierarchical' =&amp;gt; true,
			'description' =&amp;gt; 'WP Tuts Quiz',
			'supports' =&amp;gt; array( 'title', 'editor' ),
			'public' =&amp;gt; true,
			'show_ui' =&amp;gt; true,
			'show_in_menu' =&amp;gt; true,
			'show_in_nav_menus' =&amp;gt; true,
			'publicly_queryable' =&amp;gt; true,
			'exclude_from_search' =&amp;gt; false,
			'has_archive' =&amp;gt; true,
			'query_var' =&amp;gt; true,
			'can_export' =&amp;gt; true,
			'rewrite' =&amp;gt; true,
			'capability_type' =&amp;gt; 'post'
		);

		register_post_type( 'wptuts_quiz', $args );
	}
}
&lt;/pre&gt;&lt;p&gt;We have enabled both title and editor fields in the custom post type to use for the question. Since the functions are located inside a class, we have to use &lt;code&gt;$this&lt;/code&gt; in our WordPress actions, filters, and shortcodes to call the functions.&lt;/p&gt;&lt;p&gt;Apart from that all the parameters mentioned in the code are initialized with their default values.&lt;/p&gt;&lt;h3&gt;2. Creating the Question Categories&lt;/h3&gt;&lt;p&gt;In order to group questions into specific sections, we need something similar to default WordPress categories. Hence we will be using a custom taxonomy called &lt;code&gt;quiz_categories&lt;/code&gt;. We need to call the custom taxonomy generation function on the &lt;code&gt;init&lt;/code&gt; action as we did earlier. So the constructor of our plugin class needs to be updated with the following code.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_action( 'init', array( $this,'wpq_create_taxonomies' ), 0 );
&lt;/pre&gt;&lt;p&gt;Then we can implement the &lt;code&gt;wpq_create_taxonomies&lt;/code&gt; function on the &lt;code&gt;wptuts_quiz&lt;/code&gt; post type as shown in the following code.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wpq_create_taxonomies() {

	register_taxonomy(
		'quiz_categories',
		'wptuts_quiz',
		array(
			'labels' =&amp;gt; array(
				'name' =&amp;gt; 'Quiz Category',
				'add_new_item' =&amp;gt; 'Add New Quiz Category',
				'new_item_name' =&amp;gt; &amp;quot;New Quiz Category&amp;quot;
			),
			'show_ui' =&amp;gt; true,
			'show_tagcloud' =&amp;gt; false,
			'hierarchical' =&amp;gt; true
		)
	);

}
&lt;/pre&gt;&lt;p&gt;I have used the default option parameters to create this custom taxonomy. Once the plugin is activated your custom post type and taxonomy will be displayed as shown in the following screen.&lt;/p&gt;&lt;h3&gt;3. Creating the Question Answers&lt;/h3&gt;&lt;p&gt;Next, we have to create multiple answers for each question. In this plugin the maximum number of answers per single question will be limited to five.&lt;/p&gt;&lt;p&gt;You can dynamically assign 1-5 answers for any question. Also we need to specify the correct answer from the provided list of answers. Since this data is associated with our questions, we can use a meta box with custom fields to generate the necessary fields.&lt;/p&gt;&lt;p&gt;As usual we need to update the constructor with the following code:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_action( 'add_meta_boxes', array( $this,'wpq_quiz_meta_boxes' ) );
&lt;/pre&gt;&lt;p&gt;Consider the following code for the implementation of meta boxes with answers fields.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wpq_quiz_meta_boxes() {
	add_meta_box( 'quiz-answers-info', 'Quiz Answers Info', array( $this, 'wpq_quiz_answers_info' ), 'wptuts_quiz', 'normal', 'high' );
}

function wpq_quiz_answers_info() {

	global $post;

	$question_answers = get_post_meta( $post-&amp;gt;ID, '_question_answers', true );

	$question_answers = ( $question_answers == '' ) ? array( '', '', '', '', '' ) : json_decode( $question_answers );

	$question_correct_answer = trim( get_post_meta( $post-&amp;gt;ID, '_question_correct_answer', true ) );

	$html = '&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;question_box_nonce&amp;quot; value=&amp;quot;' . wp_create_nonce( basename( __FILE__ ) ) . '&amp;quot; /&amp;gt;';

	$html .= '&amp;lt;table class=&amp;quot;form-table&amp;quot;&amp;gt;';

	$html .= '&amp;lt;tr&amp;gt;&amp;lt;th style=&amp;quot;&amp;quot;&amp;gt;&amp;lt;label for=&amp;quot;Price&amp;quot;&amp;gt;Correct Answer&amp;lt;/label&amp;gt;&amp;lt;/th&amp;gt;&amp;lt;td&amp;gt;&amp;lt;select class=&amp;quot;widefat&amp;quot; name=&amp;quot;correct_answer&amp;quot; id=&amp;quot;correct_answer&amp;quot; &amp;gt;';

	for ( $i = 1; $i &amp;lt;= 5; $i++ ) {

		if ( $question_correct_answer == $i ) {
			$html .= '&amp;lt;option value=&amp;quot;' . $i . '&amp;quot; selected &amp;gt;Answer ' . $i . '&amp;lt;/option&amp;gt;';
		}
		else {
			$html .= '&amp;lt;option value=&amp;quot;' . $i . '&amp;quot;&amp;gt;Answer ' . $i . '&amp;lt;/option&amp;gt;';
		}

	}

	$html .= '&amp;lt;/select&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;';

	$index = 1;

	foreach ( $question_answers as $question_answer ) {

		$html .= '&amp;lt;tr&amp;gt;&amp;lt;th style=&amp;quot;&amp;quot;&amp;gt;&amp;lt;label for=&amp;quot;Price&amp;quot;&amp;gt;Answer ' . $index . '&amp;lt;/label&amp;gt;&amp;lt;/th&amp;gt;';
		$html .= '&amp;lt;td&amp;gt;&amp;lt;textarea class=&amp;quot;widefat&amp;quot; name=&amp;quot;quiz_answer[]&amp;quot; id=&amp;quot;quiz_answer' . $index . '&amp;quot; &amp;gt;' . esc_textarea( trim( $question_answer ) ) . '&amp;lt;/textarea&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;';

		$index++;

	}

	$html .= '&amp;lt;/tr&amp;gt;';

	$html .= '&amp;lt;/table&amp;gt;';

	echo $html;
}
&lt;/pre&gt;&lt;h3&gt;4. Code Explanation&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Answers of each question will be stored in a JSON encoded string in the &lt;code&gt;post_meta&lt;/code&gt; table with the key &lt;code&gt;_question_answers&lt;/code&gt;. So we access this field using the &lt;code&gt;get_post_meta&lt;/code&gt; function to get the current values.&lt;/li&gt;&lt;li&gt;Then we get the correct answer of the question using a similar method. The correct answer will be stored as a string in the &lt;code&gt;post_meta&lt;/code&gt; table with the key &lt;code&gt;_question_correct_answer&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;Finally, we create the HTML form, which contains the correct answer as a dropdown box and the possible answers as five text area fields.&lt;/li&gt;&lt;li&gt;All the existing values retrieved using the &lt;code&gt;get_post_meta&lt;/code&gt; function will be assigned to the respective fields.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;You should see something similar to the following screen, once you create the meta box.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://cdn.tutsplus.com/wp.tutsplus.com/authors/rakhithanimesh/quiz1.png"&gt;&lt;img
class="alignnone size-full wp-image-31237" alt="quiz1" src="http://cdn.tutsplus.com/wp.tutsplus.com/authors/rakhithanimesh/quiz1.png" width="700" height="394" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;p&gt;Now we have all the data required for our quiz generation plugin. Next step is to save the question data into the database.&lt;/p&gt;&lt;p&gt;But we need some validations prior to that. So let&amp;#8217;s move into validations.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Validating Question Creation&lt;/h2&gt;&lt;p&gt;We don&amp;#8217;t have complex validation rules at this stage in the question creation process. Therefore we are going to use client side jQuery validations before submission.&lt;/p&gt;&lt;p&gt;Here we need the &lt;code&gt;admin_enqueue_scripts&lt;/code&gt; action to be included into the constructor of our plugin.&lt;/p&gt;&lt;p&gt;So update the constructor with following code before we get started.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_action( 'admin_enqueue_scripts', array( $this, 'wpq_admin_scripts' ) );
&lt;/pre&gt;&lt;p&gt;Now look at the following code for including necessary script files for validation.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wpq_admin_scripts() {

	wp_register_script( 'quiz-admin', plugins_url( 'js/quiz.js', __FILE__ ), array( 'jquery' ) );

	wp_enqueue_script( 'quiz-admin' );

}
&lt;/pre&gt;&lt;p&gt;Using &lt;code&gt;wp_register_script&lt;/code&gt; and &lt;code&gt;wp_enqueue_script&lt;/code&gt;, we have a plugin-specific JS file called &lt;strong&gt;quiz.js&lt;/strong&gt; for handling validations. Validation will be done using the jQuery library and hence we have set the built-in jQuery library as a dependency for our plugin-specific JavaScript.&lt;/p&gt;&lt;p&gt;Having included the scripts, let&amp;#8217;s implement the actual validations in the &lt;strong&gt;quiz.js&lt;/strong&gt; file as shown in the following code.&lt;/p&gt;&lt;pre class="brush: jscript; title: ; notranslate"&gt;
jQuery(document).ready(function($) {

	$(&amp;quot;#post-body-content&amp;quot;).prepend('&amp;lt;div id=&amp;quot;quiz_error&amp;quot; class=&amp;quot;error&amp;quot; style=&amp;quot;display:none&amp;quot; &amp;gt;&amp;lt;/div&amp;gt;');

	$('#post').submit(function() {

		if ( $(&amp;quot;#post_type&amp;quot;).val() =='wptuts_quiz' ) {

			return wpq_validate_quizes();

		}

	});

});
&lt;/pre&gt;&lt;p&gt;First, we assign an empty &lt;code&gt;div&lt;/code&gt; element to the post creation screen to display any errors. Then we can call a custom JS function on the post publish action by checking the correct post type using the &lt;code&gt;#post_type&lt;/code&gt; hidden field value.&lt;/p&gt;&lt;p&gt;The following code contains the implementation of the &lt;code&gt;wpq_validate_quizes&lt;/code&gt; function.&lt;/p&gt;&lt;pre class="brush: jscript; title: ; notranslate"&gt;
var wpq_validate_quizes = function() {

	var err = 0;

	$(&amp;quot;#quiz_error&amp;quot;).html(&amp;quot;&amp;quot;);

	$(&amp;quot;#quiz_error&amp;quot;).hide();

	if ( $(&amp;quot;#title&amp;quot;).val() == '' ) {

		$(&amp;quot;#quiz_error&amp;quot;).append(&amp;quot;&amp;lt;p&amp;gt;Please enter Question Title.&amp;lt;/p&amp;gt;&amp;quot;);

		err++;

	}

	var correct_answer = $(&amp;quot;#correct_answer&amp;quot;).val();

	if ( $(&amp;quot;#quiz_answer&amp;quot;+correct_answer).val() == &amp;quot;&amp;quot; ) {

		$(&amp;quot;#quiz_error&amp;quot;).append(&amp;quot;&amp;lt;p&amp;gt;Correct answer cannot be empty.&amp;lt;/p&amp;gt;&amp;quot;);

		err++;

	}

	if ( err &amp;gt; 0 ) {

		$(&amp;quot;#publish&amp;quot;).removeClass(&amp;quot;button-primary-disabled&amp;quot;);

		$(&amp;quot;.spinner&amp;quot;).hide();

		$(&amp;quot;#quiz_error&amp;quot;).show();

		return false;

	}
	else {

		return true;

	}
};
&lt;/pre&gt;&lt;h3&gt;Code Explanation&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;First we have to hide the error container and set its current error message to empty.&lt;/li&gt;&lt;li&gt;Then we check whether the title exists, as title is mandatory for questions.&lt;/li&gt;&lt;li&gt;Next we get the selected correct answer and check whether the answer field related to the correct answer is empty.&lt;/li&gt;&lt;li&gt;When validation errors are generated we display the errors in the specified error container and prevents the submission of the post.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The following image shows the post creation screen with custom validation messages.&lt;/p&gt; &lt;figure
class="tutorial_image"&gt;&lt;a
href="http://cdn.tutsplus.com/wp.tutsplus.com/authors/rakhithanimesh/wpquiz2.png"&gt;&lt;img
class="alignnone size-full wp-image-31226" alt="wpquiz2" src="http://cdn.tutsplus.com/wp.tutsplus.com/authors/rakhithanimesh/wpquiz2.png" width="700" height="413" /&gt;&lt;/a&gt;&lt;/figure&gt;&lt;p&gt;Once the form is successfully validated without errors, we can move onto saving the question details to the database.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Saving Question Details&lt;/h2&gt;&lt;p&gt;WordPress provides an action called &lt;code&gt;save_post&lt;/code&gt;, which will be executed just after the post creation. We can define a custom function on the &lt;code&gt;save_post&lt;/code&gt; action to save the custom field details into the database.&lt;/p&gt;&lt;p&gt;As usual update the constructor with the &lt;code&gt;save_post&lt;/code&gt; action code.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_action( 'save_post', array( $this, 'wpq_save_quizes' ) );
&lt;/pre&gt;&lt;p&gt;Implementation of the &lt;code&gt;wpq_save_quizes&lt;/code&gt; function is given in the following code.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wpq_save_quizes( $post_id ) {

	if ( ! wp_verify_nonce( $_POST['question_box_nonce'], basename( __FILE__ ) ) ) {

		return $post_id;

	}

	if ( defined( 'DOING_AUTOSAVE' ) &amp;amp;&amp;amp; DOING_AUTOSAVE ) {

		return $post_id;

	}

	if ( 'wptuts_quiz' == $_POST['post_type'] &amp;amp;&amp;amp; current_user_can( 'edit_post', $post_id ) ) {

		$question_answers = isset( $_POST['quiz_answer'] ) ? ( $_POST['quiz_answer'] ) : array();

		$filtered_answers = array();

		foreach ( $question_answers as $answer ) {

			array_push( $filtered_answers, sanitize_text_field( trim( $answer ) ) );

		}

		$question_answers = json_encode( $filtered_answers );

		$correct_answer = isset( $_POST['correct_answer'] ) ? sanitize_text_field( $_POST['correct_answer'] ) : &amp;quot;&amp;quot;;

		update_post_meta( $post_id, &amp;quot;_question_answers&amp;quot;, $question_answers );

		update_post_meta( $post_id, &amp;quot;_question_correct_answer&amp;quot;, $correct_answer );

	}
	else {

		return $post_id;

	}
}
&lt;/pre&gt;&lt;p&gt;The post ID is passed automatically to this function. Initially we execute validations for nonce value and autosave. The most important thing before saving is the validation of post type.&lt;/p&gt;&lt;p&gt;If you omit the post type check, this code will be executed for all the post types in your WordPress installation including normal posts, which can result in inconsistent data.&lt;/p&gt;&lt;p&gt;Finally we get the values of our answers fields and correct answer field to be saved to the database using the &lt;code&gt;update_post_meta&lt;/code&gt; function. All the custom fields details will be saved to the &lt;code&gt;post_meta&lt;/code&gt; table.&lt;p&gt;Now we have completed the question creation process. Since we are using randomly generated quizzes, you might need lots of questions to see this plugin in action. So get ready by adding additional questions before we implement the quiz in the next part.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Quiz Settings Page&lt;/h2&gt;&lt;p&gt;Even though it&amp;#8217;s not mandatory, it&amp;#8217;s ideal to have a settings page for our plugin so that the admin can customize according to their preferences.&lt;/p&gt;&lt;p&gt;So let&amp;#8217;s implement a simple settings page to contain the quiz duration and number of questions per quiz. We need to implement the &lt;code&gt;admin_menu&lt;/code&gt; action in the constructor.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_action( 'admin_menu', array( $this, 'wpq_plugin_settings' ) );
&lt;/pre&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wpq_plugin_settings() {

	//create new top-level menu

	add_menu_page( 'WPTuts Quiz Settings', 'WPTuts Quiz Settings', 'administrator', 'quiz_settings', array( $this, 'wpq_display_settings' ) );

}

function wpq_display_settings() {

	$html = '&amp;lt;div class=&amp;quot;wrap&amp;quot;&amp;gt;
		&amp;lt;form method=&amp;quot;post&amp;quot; name=&amp;quot;options&amp;quot; action=&amp;quot;options.php&amp;quot;&amp;gt;
			&amp;lt;h2&amp;gt;Select Your Settings&amp;lt;/h2&amp;gt;' . wp_nonce_field( 'update-options' ) . '

			&amp;lt;table width=&amp;quot;100%&amp;quot; cellpadding=&amp;quot;10&amp;quot; class=&amp;quot;form-table&amp;quot;&amp;gt;
				&amp;lt;tr&amp;gt;
					&amp;lt;td align=&amp;quot;left&amp;quot; scope=&amp;quot;row&amp;quot;&amp;gt;
						&amp;lt;label&amp;gt;Number of Questions&amp;lt;/label&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;wpq_num_questions&amp;quot; value=&amp;quot;' . get_option( 'wpq_num_questions' ) . '&amp;quot; /&amp;gt;
					&amp;lt;/td&amp;gt;
				&amp;lt;/tr&amp;gt;
				&amp;lt;tr&amp;gt;
					&amp;lt;td align=&amp;quot;left&amp;quot; scope=&amp;quot;row&amp;quot;&amp;gt;
						&amp;lt;label&amp;gt;Duration (Mins)&amp;lt;/label&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;wpq_duration&amp;quot; value=&amp;quot;' . get_option( 'wpq_duration' ) . '&amp;quot; /&amp;gt;
					&amp;lt;/td&amp;gt;
				&amp;lt;/tr&amp;gt;
			&amp;lt;/table&amp;gt;

			&amp;lt;p class=&amp;quot;submit&amp;quot;&amp;gt;
				&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;action&amp;quot; value=&amp;quot;update&amp;quot; /&amp;gt;
				&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;page_options&amp;quot; value=&amp;quot;wpq_num_questions,wpq_duration&amp;quot; /&amp;gt;
				&amp;lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;Submit&amp;quot; value=&amp;quot;Update&amp;quot; /&amp;gt;
			&amp;lt;/p&amp;gt;

		&amp;lt;/form&amp;gt;
	&amp;lt;/div&amp;gt;';

	echo $html;
}
&lt;/pre&gt;&lt;p&gt;We can use the &lt;code&gt;add_menu_page&lt;/code&gt; function to add a settings page to the admin area. The &lt;code&gt;wpq_display_settings&lt;/code&gt; function is used to implement the display elements of the settings page.&lt;/p&gt;&lt;p&gt;We have used two text fields for duration and questions per quiz. Form submission and data saving can be handled manually using custom code, but WordPress provides us a simplified method for updating options.&lt;/p&gt;&lt;p&gt;In this method you have to set the form action to the &lt;strong&gt;options.php&lt;/strong&gt; file. Then you have to create a hidden field called action to contain the value of &amp;#8216;&lt;code&gt;update&lt;/code&gt;&amp;#8216;. Finally we need to have another hidden field called &lt;code&gt;page_options&lt;/code&gt; to contain the names of the two text fields to be updated.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Make sure to use these built-in options update techniques in scenarios where the requirements for saving the fields are not complex.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Once the submit button is clicked, form details will be updated automatically without any custom code.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;What&amp;#8217;s Next&lt;/h2&gt;&lt;p&gt;We have completed the backend of our quiz plugin. Now you can create questions and get ready for the next part where we will use these questions to dynamically generate quizzes.&lt;/p&gt;&lt;p&gt;Until then let me know the best possible way to implement a dynamic quiz on the frontend. Keep in mind that only one question will be displayed at a time. When the user requests a question, he can move to the next question.&lt;/p&gt;&lt;p&gt;Looking forward to your suggestions.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/GYnzBFi2Dgk" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/tutorials/plugins/integrating-multiple-choice-quizzes-in-wordpress-creating-the-backend/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <series:name><![CDATA[Integrating Multiple Choice Quizzes in WordPress]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/tutorials/plugins/integrating-multiple-choice-quizzes-in-wordpress-creating-the-backend/</feedburner:origLink></item> <item><title>The WordPress Coding Standards: Single Quotes and Double Quotes</title><link>http://feedproxy.google.com/~r/Wptuts/~3/O8hJ4Pccmj8/</link> <comments>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-single-quotes-and-double-quotes/#comments</comments> <pubDate>Tue, 11 Jun 2013 12:49:50 +0000</pubDate> <dc:creator>Tom McFarlin</dc:creator> <category><![CDATA[Cheat Sheets]]></category> <category><![CDATA[codex]]></category> <category><![CDATA[Coding Standards]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31688</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 3 of 4 in the series &lt;a
href="http://wp.tutsplus.com/series/the-wordpress-coding-standards/" class="series-1128" title="The WordPress Coding Standards"&gt;The WordPress Coding Standards&lt;/a&gt;&lt;/div&gt;&lt;p&gt;In this series, we&amp;#8217;re taking a look at the WordPress PHP Coding Standards in order further understand how quality WordPress code should be written.&lt;/p&gt;&lt;p&gt;Sure, all of this is documented in the &lt;a
title="The WordPress Coding Standards" href="http://make.wordpress.org/core/handbook/coding-standards/php/" target="_blank"&gt;WordPress Coding Standards&lt;/a&gt; and it&amp;#8217;s a site that every WordPress developer should have bookmarked and on hand when working on a theme, a plugin, or an application; however, if you&amp;#8217;re just getting into WordPress development, then it&amp;#8217;s important to understand the rationale as to &lt;em&gt;why&lt;/em&gt; the conventions are the way they are.&lt;/p&gt;&lt;p&gt;In this article, we&amp;#8217;re going to be taking a look at the use of single quotes and double quotes specifically when dealing with strings.&lt;/p&gt;&lt;p&gt;This may be the shortest, most straightforward article in the series, but it should cover some important nuances as it relates to working with single quotes, double quotes, and strings in WordPress.&lt;span
id="more-31688"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Strings in PHP and Strings in WordPress&lt;/h2&gt;&lt;p&gt;Before we actually discuss strings as they relate to WordPress, it&amp;#8217;s important to understand how the PHP language interprets strings when they are single quoted or double quoted.&lt;/p&gt;&lt;h3&gt;Single Quotes in PHP&lt;/h3&gt;&lt;p&gt;First, the simplest, most straightforward way to define a string in PHP is to simply wrap it with single quotes (that is, the &amp;#8216; character).&lt;/p&gt;&lt;p&gt;As with most programming languages, there &lt;em&gt;are&lt;/em&gt; ways to escape characters so that you&amp;#8217;re able to write out a string literal. For example, if you wanted to write: &amp;#8220;String&amp;#8217;s in PHP are easy,&amp;#8221; as a string, then you may do this:&lt;/p&gt;&lt;p&gt;&lt;code&gt;'String\'s in PHP are easy.'&lt;/code&gt;&lt;/p&gt;&lt;p&gt;See? The backslashes will instruct PHP to write out the single quote rather than terminating the actual string.&lt;/p&gt;&lt;p&gt;The second thing to note is that if you have a variable, it will &lt;em&gt;not&lt;/em&gt; be replaced when quoted in single quotes. For example, assume that you have a variable called &lt;code&gt;$name&lt;/code&gt; and it contains the value &amp;#8220;Envato&amp;#8221;.&lt;/p&gt;&lt;p&gt;More specifically, in code, this would look like this:&lt;/p&gt;&lt;p&gt;&lt;code&gt;$name = 'Envato'&lt;/code&gt;&lt;/p&gt;&lt;p&gt;If you were to embed the &lt;code&gt;$name&lt;/code&gt; variable into another string, &amp;#8216;Envato&amp;#8217; would not be replaced.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
$name = 'Envato';
echo 'I\'m writing for $name. It\'s a lot of fun.';
&lt;/pre&gt;&lt;p&gt;This would just echo: &lt;em&gt;I&amp;#8217;m writing for $name. It&amp;#8217;s a lot of fun.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Here is where double-quotes begin to come in handy.&lt;/p&gt;&lt;h3&gt;Double Quotes in PHP&lt;/h3&gt;&lt;p&gt;Straight from the PHP manual:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;If the string is enclosed in double-quotes (&amp;#8220;), PHP will interpret more escape sequences for special characters.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;You can read more about the escape characters and other information in &lt;a
title="PHP Manual" href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single" target="_blank"&gt;the PHP manual&lt;/a&gt;, but let&amp;#8217;s say we were to rework some of the strings that we&amp;#8217;ve defined above.&lt;/p&gt;&lt;p&gt;Our first example, that is the one with the escaped single quote, could now be defined like this:&lt;/p&gt;&lt;p&gt;&lt;code&gt;"String's in PHP are easy."&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Our second example could be taken a step further: Not only is the escape sequence no longer needed, but the variable will also be evaluated:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
$name = 'Envato';
echo &amp;quot;I'm writing for $name. It's a lot of fun.&amp;quot;;
&lt;/pre&gt;&lt;p&gt;The second line will literally echo: &lt;em&gt;I&amp;#8217;m writing for Envato. It&amp;#8217;s a lot of fun.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;So with this covered as an introduction, let&amp;#8217;s talk about the specifics of strings in WordPress. The truth is, if you have a strong grasp on all of the above, there&amp;#8217;s not much more to add other than a few one-off cases.&lt;/p&gt;&lt;h3&gt;Single Quotes in WordPress&lt;/h3&gt;&lt;p&gt;In keeping consistent with PHP coding conventions, the general rule of thumb to follow is to always use single quotes to define your strings.&lt;/p&gt;&lt;p&gt;Since much of the work done in WordPress also includes writing out markup &lt;em&gt;within&lt;/em&gt; a PHP string, it&amp;#8217;s best to place those strings in single quotes so that the attributes of the HTML element can be enclosed in double quotes.&lt;/p&gt;&lt;p&gt;For example:&lt;/p&gt;&lt;p&gt;&lt;code&gt;$html = '&amp;lt;a href="http://wordpress.org" target="_blank"&amp;gt;WordPress&amp;lt;/a&amp;gt;';&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Obviously, the above makes for a much cleaner reading experience than having to place several escape characters in to handle the double quotes, the slashes, and so on.&lt;/p&gt;&lt;p&gt;If you&amp;#8217;re dealing with markup that needs to span multiple lines or where you&amp;#8217;re creating several elements some of which are child elements of others, I always recommend indenting the code so that it reads like an HTML document would.&lt;/p&gt;&lt;p&gt;For example:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
$name = 'Tom McFarlin';

$html = '&amp;lt;div id=&amp;quot;container&amp;quot;&amp;gt;';
	$html .= '&amp;lt;span&amp;gt;' . $name . '&amp;lt;/span&amp;gt;';
$html .= &amp;lt;/div&amp;gt;&amp;lt;!-- /#container --&amp;gt;';
&lt;/pre&gt;&lt;p&gt;It&amp;#8217;s important to note that this isn&amp;#8217;t part of the WordPress Coding Standards &amp;#8211; this convention is one that I simply find useful when it comes to maintaining code &amp;#8211; however, recall that single quoted strings do &lt;em&gt;not&lt;/em&gt; evaluate the value of a variable.&lt;/p&gt;&lt;p&gt;As such, it is important to note that, in this case, we&amp;#8217;re using string concatenation to include the variable definition.&lt;/p&gt;&lt;h3&gt;Double Quotes in WordPress&lt;/h3&gt;&lt;p&gt;Just as with double quotes in PHP, it&amp;#8217;s important to note that there are times in which it&amp;#8217;s more preferable to use them especially when you need to evaluate a variable.&lt;/p&gt;&lt;p&gt;In using the example from the code above, we would adapt it as follows:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
$name = 'Tom McFarlin';

$html = &amp;quot;&amp;lt;div id='container'&amp;gt;&amp;quot;;
  $html .= &amp;quot;&amp;lt;span&amp;gt;$name&amp;lt;/span&amp;gt;&amp;quot;;
$html .= &amp;quot;&amp;lt;/div&amp;gt;&amp;lt;!-- /#container --&amp;gt;&amp;quot;;
&lt;/pre&gt;&lt;p&gt;Notice that we replaced all of the single quotes with double quotes and that we no longer have to perform any string concatenation with the &lt;code&gt;$name&lt;/code&gt; variable since it will be evaluated.&lt;/p&gt;&lt;p&gt;It&amp;#8217;s also important to note that although it&amp;#8217;s arguably &lt;em&gt;more common&lt;/em&gt; to see double quotes applied to HTML attributes, single quotes work just fine as well, especially in scenarios like this.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;But There &lt;em&gt;Are&lt;/em&gt; Exceptions&lt;/h2&gt;&lt;p&gt;When working with strings and various programming languages &amp;#8211; namely HTML &amp;#8211; it can get a little complicated in terms of how you&amp;#8217;re nesting your code.&lt;/p&gt;&lt;p&gt;For example, say that you&amp;#8217;re using PHP to write out JavaScript which is responsible for rendering some HTML. In this scenario, you&amp;#8217;re going to be writing a lot of quotes.&lt;/p&gt;&lt;p&gt;Though it&amp;#8217;s possible to simply alternate your quoting styles, this won&amp;#8217;t work 100% of the time.&lt;/p&gt;&lt;p&gt;Even the Coding Standards state:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;An exception to this is JavaScript, which sometimes requires double or single quotes.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Although there are times where you may not be able to do this, this reinforces the idea that we should strive to keep each programming language in its own file style and include them when and where necessary.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Are There Any Security Issues?&lt;/h2&gt;&lt;p&gt;Yes, but WordPress makes them extremely easy to manage, and though this technically falls under data validation, it&amp;#8217;s closely related to exactly what we&amp;#8217;re discussing in this article.&lt;/p&gt;&lt;p&gt;The primary problem is that there are times where we may be dynamically writing out HTML attributes to our markup that have values we can&amp;#8217;t anticipate. In cases like this, we need to be able to escape said data.&lt;/p&gt;&lt;p&gt;Fortunately, WordPress provides the &lt;code&gt;&lt;a
title="Escape Attribute" href="http://codex.wordpress.org/Function_Reference/esc_attr" target="_blank"&gt;esc_attr&lt;/a&gt;&lt;/code&gt; function. Simply put, this function will take an incoming string and encode the characters to make sure they are rendered to the browser properly.&lt;/p&gt;&lt;p&gt;Practically speaking, you would use it exactly like the Codex demonstrates:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
echo '&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;fname&amp;quot; value=&amp;quot;' . esc_attr( $_POST['fname'] ) . '&amp;quot;&amp;gt;';
&lt;/pre&gt;&lt;p&gt;For more information, be sure to review &lt;a
title="esc_attr" href="http://codex.wordpress.org/Function_Reference/esc_attr" target="_blank"&gt;the Codex article&lt;/a&gt;.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;So, as a general rule of thumb, you should stick with using single-quotes &lt;em&gt;unless&lt;/em&gt; you&amp;#8217;re going to be evaluating a string or you&amp;#8217;re going to be returning and echo&amp;#8217;ing HTML, JavaScript, or potentially even CSS back to the caller.&lt;/p&gt;&lt;p&gt;Just as the Coding Standards state:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;You should almost never have to escape quotes in a string, because you can just alternate your quoting style.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;So as you&amp;#8217;re working with strings in your future work, keep these principles in mind and it could go along way to making sure that your strings are as clean, and easy to understand as possible within the context of your WordPress-based projects.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/O8hJ4Pccmj8" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-single-quotes-and-double-quotes/feed/</wfw:commentRss> <slash:comments>2</slash:comments> <series:name><![CDATA[The WordPress Coding Standards]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-single-quotes-and-double-quotes/</feedburner:origLink></item> <item><title>Making WordPress Apps Ready for Facebook</title><link>http://feedproxy.google.com/~r/Wptuts/~3/pTSlr1Gxi4o/</link> <comments>http://wp.tutsplus.com/tutorials/creative-coding/making-wordpress-apps-ready-for-facebook/#comments</comments> <pubDate>Fri, 07 Jun 2013 12:43:55 +0000</pubDate> <dc:creator>Kailan Wyatt</dc:creator> <category><![CDATA[Creative Coding]]></category> <category><![CDATA[adsense]]></category> <category><![CDATA[Facebook]]></category> <category><![CDATA[Facebook Apps]]></category> <category><![CDATA[Shortcodes]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=30979</guid> <description>&lt;p&gt;It is becoming increasingly popular for people to integrate their WordPress sites into Facebook applications and fan pages. Although this tutorial is not about how to make an integration, it &lt;em&gt;will&lt;/em&gt; explain some smart code techniques that can be implemented for a Facebook-specific view.&lt;/p&gt;&lt;p&gt;Facebook and other websites have specific policies about what not to do on Facebook and this tutorial will show you just exactly how you can obey these policies.&lt;span
id="more-30979"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Why Use This Technique? Avoid Getting Banned!&lt;/h2&gt;&lt;p&gt;As lovely as it is to create a Facebook App using a WordPress site, many sites include advertising blocks from providers that Facebook does not allow.&lt;/p&gt;&lt;p&gt;Additionally, there is much talk on the net that Google Adsense does not allow the embedding of their ads on Facebook.&lt;/p&gt;&lt;p&gt;For the innocent web developer or blogger, it would be detrimental to get banned because of failure to adhere to either Google Adsense / Facebook policies; therefore, in order to avoid possible problems, the following technique can be implemented.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;Facebook has provided a list of providers that they support: &lt;a
href="http://developers.facebook.com/adproviders/" target="_blank"&gt;Advertising Providers on Facebook Platform&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;hr
/&gt;&lt;h2&gt;Writing the Function&lt;/h2&gt;&lt;p&gt;For the purpose of this tip, we will assume that you are building an adsense-ready WordPress theme. Within the file &lt;strong&gt;functions.php&lt;/strong&gt;, add the following function.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function is_facebook() {
	$url = $_SERVER['HTTP_REFERER'];
	$parse = parse_url( $url );
	$host = $parse['host'];
	if ( 'apps.facebook.com' == $host ) {
		return true;
	}
	else {
		return false;
	}
}
&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;The line &lt;code&gt;$url = $_SERVER['HTTP_REFERER'];&lt;/code&gt; sets a variable of the Facebook App to the address that the WordPress site is being iframed in. For example: &lt;code&gt;http://apps.facebook.com/my-app-name/&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;$parse = parse_url( $url );&lt;/code&gt; creates an array of all components used in the Facebook App address.&lt;/li&gt;&lt;li&gt;&lt;code&gt;$host = $parse['host'];&lt;/code&gt; This line gets the host being used, which in this case is &lt;code&gt;apps.facebook.com&lt;/code&gt;&lt;/li&gt;&lt;li&gt;The next lines check to see if the host is from a Facebook App and returns a Boolean.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Using the Function&lt;/h3&gt;&lt;p&gt;In places where you would like to display an Adsense block, you can write the following:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
if ( is_facebook() ) {
	// Display nothing or display AD from Facebook Ad Provider
}
else {
	// Display my Adsense Ads
}
&lt;/pre&gt;&lt;h3&gt;Other Uses of the Function&lt;/h3&gt;&lt;ol&gt;&lt;li&gt;Change logo / images on Facebook Apps&lt;/li&gt;&lt;li&gt;Create Facebook specific layouts&lt;/li&gt;&lt;li&gt;Use Facebook comments while viewing on Facebook&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;A neat trick with this function is to call different template parts. The snippet below, shows you how you can call a different header file &lt;strong&gt;header-facebook.php&lt;/strong&gt; by using the function.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
if ( is_facebook() ) {
	get_header( 'facebook' );
}
else {
	get_header();
}
&lt;/pre&gt;&lt;hr
/&gt;&lt;h2&gt;Creating a Shortcode&lt;/h2&gt;&lt;p&gt;Still assuming that you are working on a theme, open the &lt;strong&gt;functions.php&lt;/strong&gt; file and add the following:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
add_shortcode( 'is_fb', 'wp_if_fb' );
function wp_if_fb( $atts, $content = null ) {
	if ( is_facebook() ) {
		return $content;
	}
}

add_shortcode( 'not_fb', 'wp_not_fb' );
function wp_not_fb( $atts, $content = null ) {
	if ( ! is_facebook() ) {
		return $content;
	}
}
&lt;/pre&gt;&lt;p&gt;The first line creates a shortcode &lt;code&gt;is_fb&lt;/code&gt; with a call back function &lt;code&gt;wp_if_fb&lt;/code&gt;. Content passed in the shortcode will be displayed only if the WordPress site is wrapped in a Facebook iFrame.&lt;/p&gt;&lt;p&gt;The second &lt;code&gt;add_shortcode&lt;/code&gt; function is used to display items in a post not currently viewed via Facebook.&lt;/p&gt;&lt;h3&gt;How to Use the Shortcode&lt;/h3&gt;&lt;p&gt;Within any textarea that the shortcode filter is applied to, you can use the previously created shortcode there. Below is an example of how to use the snippet.&lt;/p&gt;&lt;pre class="brush: xml; title: ; notranslate"&gt;
[is_fb]This is my text that will show up on Facebook[/is_fb]

[not_fb]This test will be displayed outside of Facebook[/not_fb]
&lt;/pre&gt;&lt;p&gt;A more practical example is a welcome message in an about page.&lt;/p&gt;&lt;p&gt;For example:&lt;/p&gt;&lt;p&gt;&lt;code&gt;I would like to welcome you to my [not_fb]Website Portal[/not_fb][is_fb]Facebook Application[/is_fb]&lt;/code&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;To learn more about creating shortcodes, visit this link: &lt;a
href="http://codex.wordpress.org/Shortcode_API" target="_blank"&gt;Shortcode API&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Again, I can not stress enough about the importance of adhering to Google / Facebook&amp;#8217;s policies. For guest bloggers that are engaged in revenue sharing sites, it is important to know that their Google Adsense accounts are not at risk of being banned. Therefore, if measures were put in place by using our function, we can minimize the risk.&lt;/p&gt;&lt;p&gt;For Designers, the function we have created would be a perfect solution to creating a Facebook specific design in their themes. Such a design can make the WordPress site look like a well designed Facebook App by removing aspects that would otherwise make it seem more like a website. Like hiding header and footers to display page only, would be great for a simple promotional page tab.&lt;/p&gt;&lt;p&gt;Finally, the Developers can take advantage of the plugin and use it in their WordPress plugins. Developers can use the snippet for custom registration and login scripts, advertisement plugins and so much more.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/pTSlr1Gxi4o" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/tutorials/creative-coding/making-wordpress-apps-ready-for-facebook/feed/</wfw:commentRss> <slash:comments>1</slash:comments> <feedburner:origLink>http://wp.tutsplus.com/tutorials/creative-coding/making-wordpress-apps-ready-for-facebook/</feedburner:origLink></item> <item><title>The WordPress Coding Standards: Naming Conventions and Function Arguments</title><link>http://feedproxy.google.com/~r/Wptuts/~3/w1WMRLpK_kc/</link> <comments>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-naming-conventions-and-function-arguments/#comments</comments> <pubDate>Thu, 06 Jun 2013 13:27:58 +0000</pubDate> <dc:creator>Tom McFarlin</dc:creator> <category><![CDATA[Cheat Sheets]]></category> <category><![CDATA[codex]]></category> <category><![CDATA[Coding Standards]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31683</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 2 of 4 in the series &lt;a
href="http://wp.tutsplus.com/series/the-wordpress-coding-standards/" class="series-1128" title="The WordPress Coding Standards"&gt;The WordPress Coding Standards&lt;/a&gt;&lt;/div&gt;&lt;p&gt;In this series, we&amp;#8217;re taking a deep dive into the WordPress Coding Standards &amp;#8211; specifically, &lt;a
title="The WordPress PHP Coding Standards" href="http://make.wordpress.org/core/handbook/coding-standards/php/" target="_blank"&gt;the PHP coding standards&lt;/a&gt; &amp;#8211; in order to evangelize and understand how quality WordPress code should be written.&lt;/p&gt;&lt;p&gt;Despite the fact that this is documented within the WordPress Developer Handbook, I think there&amp;#8217;s something to be said for understanding the rationale behind &lt;em&gt;why&lt;/em&gt; some things are the way that they are.&lt;/p&gt;&lt;p&gt;Remember: Our ultimate goal is to make sure that we&amp;#8217;re writing code that conforms to the coding standards so that we, along with other developers, are able to more easily read, understand, and maintain code for themes, plugins, and applications built on top of WordPress.&lt;/p&gt;&lt;p&gt;In this post, we&amp;#8217;re going to be taking a look at how to handle naming conventions and function arguments.&lt;span
id="more-31683"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Naming Conventions&lt;/h2&gt;&lt;p&gt;Before spending any time elaborating on the points outlined in the coding standards, it&amp;#8217;s important to understand the role that naming conventions play in writing code regardless of what platform with which you&amp;#8217;re working.&lt;/p&gt;&lt;p&gt;Ultimately, naming conventions &amp;#8211; regardless of if they are for classes, functions, variables, attributes, or arguments &amp;#8211; should help to explicate the purpose that they serve.&lt;/p&gt;&lt;p&gt;By that, I mean that class names should typically be nouns, functions should typically be verbs, and variables, attributes, and arguments should explain the purpose that they serve within the context of the class or function in which they are to be defined. It&amp;#8217;s all about making the code as readable as possible.&lt;/p&gt;&lt;p&gt;Just as the Coding Standards state:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Don&amp;#8217;t abbreviate variable names un-necessarily; let the code be unambiguous and self-documenting.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This is a good rule of thumb &lt;em&gt;regardless&lt;/em&gt; of what part of the code it is on which you&amp;#8217;re working.&lt;/p&gt;&lt;h3&gt;Class Names&lt;/h3&gt;&lt;p&gt;When it comes to working with WordPress, you&amp;#8217;re not likely to encounter classes unless you&amp;#8217;re doing one of two things:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Writing a custom library to work alongside a theme or application&lt;/li&gt;&lt;li&gt;Writing an OOP-based plugin&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;If you&amp;#8217;re simply working on the theme, you&amp;#8217;re more likely to be working with a set of functions &amp;#8211; we&amp;#8217;ll be talking about those momentarily.&lt;/p&gt;&lt;p&gt;But for those who are working with plugins or their own libraries, it&amp;#8217;s important to remember that classes should typically be nouns &amp;#8211; they should represent the purpose that they encapsulate and they should ideally do one thing and do it well.&lt;/p&gt;&lt;p&gt;For example, if you have a class called &lt;code&gt;Local_File_Operations&lt;/code&gt; then it may be responsible for reading and writing files. It shouldn&amp;#8217;t be responsible for reading and writing files as well as, say, retrieving remote files.&lt;/p&gt;&lt;p&gt;According to the WordPress Coding Standards, classes should follow the following conventions:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Class names should use capitalized words separated by underscores.&lt;/li&gt;&lt;li&gt;Any acronyms should be all upper case.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Simple, right?&lt;/p&gt;&lt;p&gt;Practically speaking, this would look like the following:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;class Local_File_Operations {}&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;class Remote_File_Operations {}&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;class HTTP_Request {}&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;class SQL_Manager {}&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;To reiterate: classes should also be nouns and should describe the single purpose they serve.&lt;/p&gt;&lt;h3&gt;Function Names&lt;/h3&gt;&lt;p&gt;As mentioned earlier, if classes are nouns that ideally represent a single idea or single purpose, then their methods should be the actions that they are able to take. As such, they should be verbs &amp;#8211; they should indicate what action will be taken whenever they are called.&lt;/p&gt;&lt;p&gt;Furthermore, the arguments that they accept should also factor into the name of the function. For example, if a function is responsible for opening a file, then its parameter should be a file name. Since our goal should make it as easy as possible to read code, then it should read something like &amp;#8220;have the local file manager read the file having the following file name.&amp;#8221;&lt;/p&gt;&lt;p&gt;In code, this may look something like this:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
// The class definition
class Local_File_Manager {

	public function open_file( $filename ) {
		// Function implementation
	}

}

// How we'd use this code
$file_manager = new Local_File_Manager();
$file_manager-&amp;gt;open_file( 'foo.txt' );
&lt;/pre&gt;&lt;p&gt;Of course, this still doesn&amp;#8217;t cover &lt;em&gt;how&lt;/em&gt; functions should be written within the context of WordPress development. The Coding Standards state:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Use lowercase letters in variable, action, and function names (never &lt;code&gt;camelCase&lt;/code&gt;). Separate words via underscores. Don&amp;#8217;t abbreviate variable names un-necessarily; let the code be unambiguous and self-documenting.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;The first part of of the convention is easy enough to understand; however, I think developers have a propensity to take shortcuts when they are able. &amp;#8220;Ah,&amp;#8221; we think, &amp;#8220;&lt;code&gt;$str&lt;/code&gt; makes sense here, and &lt;code&gt;$number&lt;/code&gt; make sense here.&amp;#8221;&lt;/p&gt;&lt;p&gt;Of course, there are always worse &amp;#8211; some developers resort to using single characters for their variable names (which is generally only acceptable within loops.)&lt;/p&gt;&lt;p&gt;Just as the Coding Standards state: &lt;em&gt;Don&amp;#8217;t abbreviate variable names &lt;strong&gt;un-necessarily&lt;/strong&gt;. Let the code be unambiguous and self-documenting.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Now, the truth is, code can only be unambiguous to a point. After all, that&amp;#8217;s why it&amp;#8217;s called code, right? This is why &lt;a
title="A Case For Code Comments" href="http://wp.tutsplus.com/series/a-case-for-code-comments/"&gt;I think code comments should be used liberally&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Anyway, the bottom line is to lower case your method names, avoid all camel casing, separate by spacing, and be as specific as possible when naming your variables.&lt;/p&gt;&lt;h3&gt;Variable Names&lt;/h3&gt;&lt;p&gt;Variable names actually aren&amp;#8217;t much different from function names other than they represent a single value or a reference to a particular object. The naming conventions still follow what you&amp;#8217;d expect:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Lower case (versus camelCase)&lt;/li&gt;&lt;li&gt;Separate spaces with underscores&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;One other convention that some developers use is what&amp;#8217;s known as &lt;a
title="Hungarian Notation" href="http://en.wikipedia.org/wiki/Hungarian_notation" target="_blank"&gt;Hungarian Notation&lt;/a&gt; which is where the type of value the variable stores is prefixed in front of the variable.&lt;/p&gt;&lt;p&gt;For example:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Strings will often be represented as &lt;code&gt;$str_firstname&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Numbers will be written as &lt;code&gt;$i_tax&lt;/code&gt; or &lt;code&gt;$num_tax&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Arrays may be written as &lt;code&gt;$arr_range&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&amp;#8230;and so on&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Honestly, the coding standards say nothing about this. On one hand, I do think that this makes for cleaner code in the overall scope of code, but there are a lot of developers who dislike Hungarian Notation.&lt;/p&gt;&lt;p&gt;Since the coding conventions say nothing about them, I&amp;#8217;m hesitant to recommend them as I want to stay as close to standards as possible. As such, I have to recommend that it&amp;#8217;s best to follow the coding standards.&lt;/p&gt;&lt;h3&gt;File Names&lt;/h3&gt;&lt;p&gt;In keeping consistent with the theme of making our code as readable and self-documenting as possible, it makes sense that we pull this through our source code all the way to the files that we&amp;#8217;re going to make up our theme, plugin, or application.&lt;/p&gt;&lt;p&gt;According to the Coding Standards:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Files should be named descriptively using lowercase letters. Hyphens should separate words.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;In keeping consistent with our previous example, let&amp;#8217;s say that we&amp;#8217;re working with &lt;code&gt;Local_File_Operations&lt;/code&gt; then the file would be named &lt;code&gt;class-local-file-operations.php&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Easy enough.&lt;/p&gt;&lt;p&gt;Next, if you&amp;#8217;re working on a plugin called &lt;code&gt;Instagram_Foo&lt;/code&gt; then the file should be named &lt;code&gt;instagram-foo.php&lt;/code&gt;; however, it is worth noting that if you use some type of advanced methods for developing your plugins such as keeping the plugin class file in its own file and then loading it using it another file, then your file structure may be:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;class-instagram-foo.php&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;instagram-foo.php&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Where &lt;code&gt;instagram-foo.php&lt;/code&gt; is responsible for loading the &lt;code&gt;class-instagram-foo.php&lt;/code&gt;. Of course, this only makes sense if you&amp;#8217;re using OOP when writing your WordPress plugins.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Function Arguments&lt;/h2&gt;&lt;p&gt;When it comes to passing function arguments, it&amp;#8217;s important to remember that if function names describe the actions that are being taken by the class, then the argument should represent on what the function is actually operating.&lt;/p&gt;&lt;p&gt;From the Coding Standards:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Prefer string values to just &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; when calling functions.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Since boolean values can be unclear when passing values into a function, it makes it difficult to ascertain exactly what the function is doing.&lt;/p&gt;&lt;p&gt;For example, let&amp;#8217;s use the above example in a slightly different manner:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
// The class definition
class Local_File_Manager {

	public function manage_file( $filename, true ) {

		if ( true ) {
			// Open the file
		} else {
			// Delete the file
		}

	}

}

// How we'd use this code
$file_manager = new Local_File_Manager();
$file_manager-&amp;gt;manage_file( 'foo.txt', true );
&lt;/pre&gt;&lt;p&gt;Is more difficult to understand than, say, something like this:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
// The class definition
class Local_File_Manager {

	public function open_file( $filename ) {
		// open the file
	}

	public function delete_file( $filename ) {
		// delete the file
	}

}

// How we'd use this code
$file_manager = new Local_File_Manager();
$file_manager-&amp;gt;open_file( 'foo.txt' );
$file_manager-&amp;gt;delete_file( 'foo.txt' );
&lt;/pre&gt;&lt;p&gt;On top of that, remember that arguments being passed into functions are still variables in and of themselves so they are subject to the variable naming conventions that we&amp;#8217;ve detailed above.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;We&amp;#8217;ve taken an extended look at Naming Conventions and Function arguments in the Coding Standards. Hopefully this has helped to provide not only a guide for how to improve certain aspects of your WordPress code, but also to explain the rationale behind some of the practices.&lt;/p&gt;&lt;p&gt;In the next article, we&amp;#8217;re going to take a look at the significance of single quotes and double quotes within the context of working with strings in WordPress development.&lt;/p&gt;&lt;p&gt;There &lt;em&gt;is&lt;/em&gt; a difference into how they are interpreted by PHP and there are conditions in which you should use one over the other and we&amp;#8217;ll be reviewing that in the next article.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/w1WMRLpK_kc" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-naming-conventions-and-function-arguments/feed/</wfw:commentRss> <slash:comments>11</slash:comments> <series:name><![CDATA[The WordPress Coding Standards]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-naming-conventions-and-function-arguments/</feedburner:origLink></item> <item><title>Building a WordPress-Friendly API Wrapper: Foundations</title><link>http://feedproxy.google.com/~r/Wptuts/~3/TUy8sdhk5io/</link> <comments>http://wp.tutsplus.com/tutorials/creative-coding/building-a-wordpress-friendly-api-wrapper-getting-started/#comments</comments> <pubDate>Wed, 05 Jun 2013 13:13:09 +0000</pubDate> <dc:creator>Japh Thomson</dc:creator> <category><![CDATA[Creative Coding]]></category> <category><![CDATA[API]]></category> <category><![CDATA[envato api]]></category> <category><![CDATA[Widget]]></category> <category><![CDATA[wrapper]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31919</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 1 of 1 in the series &lt;a
href="http://wp.tutsplus.com/series/building-a-wordpress-friendly-api-wrapper/" class="series-1131" title="Building a WordPress-Friendly API Wrapper"&gt;Building a WordPress-Friendly API Wrapper&lt;/a&gt;&lt;/div&gt;&lt;p&gt;When building a web site or web application, it&amp;#8217;s a pretty common requirement that you might need to pull in data from third-party site or application. If that site or application has an API, this is an excellent way to obtain the data needed. So, let&amp;#8217;s take a look at how to do this with WordPress.&lt;span
id="more-31919"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;What Is an API?&lt;/h2&gt;&lt;p&gt;Before we get into the nitty gritty, let&amp;#8217;s make sure we&amp;#8217;re all up to speed with a couple of things, starting with what an API is.&lt;/p&gt;&lt;p&gt;For the purposes of this tutorial, when we say &amp;#8220;API&amp;#8221; (application programming interface) we specifically mean what Wikipedia classes as a &amp;#8220;&lt;a
href="http://en.wikipedia.org/wiki/Web_API"&gt;Web API&lt;/a&gt;&amp;#8220;:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;A server-side web API is a programmatic interface to a defined request-response message system, typically expressed in JSON or XML, which is exposed via the web — most commonly by means of an HTTP-based web server.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;To put it more simply: an API is a way for one application to ask another application for predefined information in (most often) JSON or XML format, over a standard web URL.&lt;/p&gt;&lt;p&gt;Got it? Ok, next…&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;What Is a &amp;#8220;Wrapper&amp;#8221;?&lt;/h2&gt;&lt;p&gt;Making a request to an API can involve a number of steps, and you may want to add in extra steps such as error checking, caching, validating, etc.&lt;/p&gt;&lt;p&gt;Wikipedia defines a &lt;a
href="http://en.wikipedia.org/wiki/Wrapper_function"&gt;Wrapper function&lt;/a&gt; as follows:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;A wrapper function is a function in a computer program whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation. Wrapper functions can be used for a number of purposes.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;That definition may make it sound a little like wrappers are just redundant code, but they&amp;#8217;re actually far from it.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Important Considerations&lt;/h2&gt;&lt;p&gt;Creating a wrapper for an API allows us to be friendly to everyone. Let me explain:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;APIs often have a &amp;#8220;rate limit&amp;#8221;, which is the number of times and frequency at which you can contact them. Contacting an API more than the rate limit specifies can get you blocked, and for information that doesn&amp;#8217;t change that regularly, it creates unnecessary load on the API. By creating a wrapper, we can implement our own caching so we contact the API less frequently.&lt;/li&gt;&lt;li&gt;Further to this, if we make a request to an API with information that&amp;#8217;s incorrectly formatted or incomplete, it&amp;#8217;s a wasted request. We should make sure the request we send is complete, and formatted correctly, as best we can before we send it.&lt;/li&gt;&lt;li&gt;Sometimes an API may send information back that&amp;#8217;s not quite formatted in the simplest way for us to use. Having a layer in between the developer and the API allows the chance to massage the data as needed before the developer gets it.&lt;/li&gt;&lt;li&gt;Developers also shouldn&amp;#8217;t have to &lt;a
href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;repeat themselves&lt;/a&gt; more than necessary. If we bundle all the steps involved in making an API call up into a wrapper, this simplifies development using the API in question.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Wrappers make dealing with APIs developer-friendly, and API-friendly.&lt;/p&gt;&lt;p&gt;So the particular things we&amp;#8217;re going to get our API to handle are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Making a call to the API when the developer requests information&lt;/li&gt;&lt;li&gt;Checking to ensure valid data is sent to the API&lt;/li&gt;&lt;li&gt;Checking if the API returned an error rather than useful data, and dealing with it accordingly&lt;/li&gt;&lt;li&gt;Caching responses from the API so that if the same request is made in a short space of time, we don&amp;#8217;t have to contact the API&lt;/li&gt;&lt;li&gt;Adjusting the returned data for simplified usage&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;We&amp;#8217;ll also be doing these things in a WordPress-friendly way, such as utilising transients for caching, and &lt;code&gt;wp_remote_request&lt;/code&gt; for sending the API requests.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Building the Foundation&lt;/h2&gt;&lt;p&gt;The API we&amp;#8217;re going to be using for this tutorial is the Envato Marketplace API. This API provides information on the items and users on the Envato Marketplaces, such as &lt;a
href="http://themeforest.net"&gt;ThemeForest&lt;/a&gt;, &lt;a
href="http://codecanyon.net"&gt;CodeCanyon&lt;/a&gt;, and others.&lt;/p&gt;&lt;p&gt;We&amp;#8217;ll be basing our development around Public Sets, seen in the &lt;a
href="http://marketplace.envato.com/api/documentation"&gt;Envato Marketplace API documentation&lt;/a&gt;. Also, note the TTL (time to live) column, which is essentially the &amp;#8220;rate limit&amp;#8221; for this API. As the data only changes with this frequency, there&amp;#8217;s no point requesting any given set more often than the TTL specified.&lt;/p&gt;&lt;p&gt;So let&amp;#8217;s begin building the base class for communicating with this API.&lt;/p&gt;&lt;h3&gt;1. File Structure&lt;/h3&gt;&lt;p&gt;Because this will eventually be inside an example plugin, which we&amp;#8217;ll get to later, let&amp;#8217;s create that file structure and build the API wrapper inside.&lt;/p&gt;&lt;p&gt;Our plugin will be showing the results of the &amp;#8220;random-new-files&amp;#8221; API set, so we&amp;#8217;ll call it &amp;#8220;Wptuts+ Random New&amp;#8221;. So inside your &lt;strong&gt;wp-content/plugins/&lt;/strong&gt; directory, go ahead and create a directory called &lt;strong&gt;wptuts-random-new&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Now inside that directory, make a new directory called &lt;strong&gt;envato-api-wrapper&lt;/strong&gt;, and a file inside that called &lt;strong&gt;class-envato-api-wrapper.php&lt;/strong&gt;. This whole directory will then be able to be copied into other projects that need to use this API in future too.&lt;/p&gt;&lt;h3&gt;2. Class and Basics&lt;/h3&gt;&lt;p&gt;Open the &lt;strong&gt;*class-envato-api-wrapper.php&lt;/strong&gt; file, and let&amp;#8217;s begin.&lt;/p&gt;&lt;p&gt;First, let&amp;#8217;s define our class:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
&amp;lt;?php
class Envato_Marketplace_API {

    // All the things

}
&lt;/pre&gt;&lt;p&gt;We&amp;#8217;re also going to need a simple way to test this wrapper as we build it, without having to load all of WordPress. So what I&amp;#8217;ve done, is just create an &lt;code&gt;index.php&lt;/code&gt; file in the &lt;strong&gt;envato-api-wrapper&lt;/strong&gt; directory, with the following content:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
&amp;lt;?php
// We want to see errors during debugging
error_reporting(E_ALL);
ini_set('display_errors', '1');

// We need to use some WordPress functions
require( '../../../../wp-load.php' );

// And we need our wrapper!
require( 'class-envato-api-wrapper.php' );

// Initialise our wrapper
$envato_api = new Envato_Marketplace_API();
&lt;/pre&gt;&lt;p&gt;Now you should be able to browse to &lt;code&gt;http://localhost/wp-content/plugins/wptuts-random-new/envato-api-wrapper/&lt;/code&gt; (replace &amp;#8220;localhost&amp;#8221; with your own development environment). You shouldn&amp;#8217;t see anything yet though, because wrapper doesn&amp;#8217;t do anything yet. If you see an error though, double-check the code.&lt;/p&gt;&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; don&amp;#8217;t forget to remove this file, or blank it out, before deploying to production!&lt;/em&gt;&lt;/p&gt;&lt;h3&gt;3. Where to Send Requests&lt;/h3&gt;&lt;p&gt;The first thing you&amp;#8217;re going to need before making a request to an API, is the base URL to use. This URL will then have various information added to it before sending the request.&lt;/p&gt;&lt;p&gt;In the case of the Envato Marketplace API, we&amp;#8217;ll use the following URL:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;

http://marketplace.envato.com/api/edge/set.json

&lt;/pre&gt;&lt;p&gt;In this URL there are two main variable bits of information: &lt;code&gt;edgde&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;The position of &lt;code&gt;edgde&lt;/code&gt; represents the version of the API we want to use, and in this specifically the most current version. It will be the same in all requests, however if something changes and we wanted to use an older version, we could swap it out for &lt;code&gt;v3&lt;/code&gt; instead for example.&lt;/p&gt;&lt;p&gt;The position of &lt;code&gt;set&lt;/code&gt; represents the actual data set we&amp;#8217;re requesting. The word &amp;#8220;set&amp;#8221; is a placeholder, and we&amp;#8217;ll replace it with the relevant details when making a specific request.&lt;/p&gt;&lt;p&gt;Let&amp;#8217;s add a property of &lt;code&gt;$api_url&lt;/code&gt; to our class with this value:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
&amp;lt;?php
class Envato_Marketplace_API {

    protected $api_url = 'http://marketplace.envato.com/api/edge/set.json';

}
&lt;/pre&gt;&lt;p&gt;Note that we&amp;#8217;ve indicated this property is &amp;#8216;&lt;code&gt;protected&lt;/code&gt;&amp;#8216;. We&amp;#8217;ve done this because we don&amp;#8217;t want the URL to be changed directly by other code outside of our wrapper.&lt;/p&gt;&lt;h3&gt;4. Making a Request&lt;/h3&gt;&lt;p&gt;Before we try making an specific request, we&amp;#8217;ll add a generic method to our class that will handle making any request. We&amp;#8217;ll call it &lt;code&gt;remote_request&lt;/code&gt;, and all it will need is a URL to send the request to.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
protected function remote_request( $url ) {

    // Firstly, if the URL is empty, don't send a request
    if ( empty( $url ) ) {
        return false;
    }

    // Make the request
    $request = wp_remote_request( $url );

    // Check that WordPress was able to send the request, otherwise error
    if ( is_wp_error( $request ) ) {
        echo $request-&amp;gt;get_error_message();
        return false;
    }

    // Decode the response so we can use it
    $data = json_decode( $request['body'] );

    if ( $request['response']['code'] == 200 ) {
        return $data;
    }
    else {
        return false;
    }

}
&lt;/pre&gt;&lt;p&gt;Note that again we&amp;#8217;ve indicated that this method is &amp;#8216;&lt;code&gt;protected&lt;/code&gt;&amp;#8216;, to prevent it being called directly from outside our class.&lt;/p&gt;&lt;p&gt;Now to be able to test that the request is working, we&amp;#8217;ll add our first API method. In this case, we&amp;#8217;ll go with the &lt;code&gt;random-new-files&lt;/code&gt; set, which is the one we&amp;#8217;ll use later in our example plugin.&lt;/p&gt;&lt;p&gt;Add the following method to our class:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
public function random_new_files( $marketplace ) {

    // Create a URL for this request by replacing the 'set' placeholder with the set and any required parameters. In this case, the slug of an Envato marketplace
    $url = preg_replace( '/set/i', 'random-new-files:' . $marketplace, $this-&amp;gt;api_url );

    // Send a request to the API
    $results = $this-&amp;gt;remote_request( $url );

    return $results;

}
&lt;/pre&gt;&lt;p&gt;To test this new method, and by extension the API request method, add the following line to our debugging &lt;strong&gt;index.php&lt;/strong&gt; file:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
echo '&amp;lt;pre&amp;gt;' . print_r( $envato_api-&amp;gt;random_new_files( 'themeforest' ), true ) . '&amp;lt;/pre&amp;gt;';
&lt;/pre&gt;&lt;p&gt;Now you should get output in your browser similar to this:&lt;/p&gt;&lt;pre class="brush: xml; title: ; notranslate"&gt;
stdClass Object
(
    [random-new-files] =&amp;gt; Array
        (
            [0] =&amp;gt; stdClass Object
                (
                    [id] =&amp;gt; 4860464
                    [item] =&amp;gt; Empire - Business, Portfolio HTML 5 Template
                    [url] =&amp;gt; http://themeforest.net/item/empire-business-portfolio-html-5-template/4860464
                    [user] =&amp;gt; designthemes
                    [thumbnail] =&amp;gt; http://1.s3.envato.com/files/57931279/thumb-html.png
                    [sales] =&amp;gt; 0
                    [rating] =&amp;gt; 0.0
                    [cost] =&amp;gt; 15.00
                )

                ...

            [9] =&amp;gt; stdClass Object
                (
                    [id] =&amp;gt; 4852371
                    [item] =&amp;gt; eplie | Mobile HTML/Css Portfolio Template
                    [url] =&amp;gt; http://themeforest.net/item/eplie-mobile-htmlcss-portfolio-template/4852371
                    [user] =&amp;gt; cosmincotor
                    [thumbnail] =&amp;gt; http://1.s3.envato.com/files/57831617/thumbnail.png
                    [sales] =&amp;gt; 3
                    [rating] =&amp;gt; 0.0
                    [cost] =&amp;gt; 8.00
                )

        )

)
&lt;/pre&gt;&lt;p&gt;Assuming you saw something like the above, that means it&amp;#8217;s working and you&amp;#8217;ve gotten data back from the API. Success!&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Next: Caching, Error Handling, and a Widget&lt;/h2&gt;&lt;p&gt;Now we&amp;#8217;ve gotten the basis of the wrapper done, things are under way. In the next part, we&amp;#8217;ll finish off the wrapper and build a widget that utilises it.&lt;/p&gt;&lt;p&gt;Also, keep in mind that while this wrapper is being built for the Envato Marketplace API, the same concept (and a lot of the same code so far) can be used to create a wrapper for any RESTful API.&lt;/p&gt;&lt;p&gt;Let us know in the comments what ideas you have for working with APIs and WordPress.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/TUy8sdhk5io" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/tutorials/creative-coding/building-a-wordpress-friendly-api-wrapper-getting-started/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <series:name><![CDATA[Building a WordPress-Friendly API Wrapper]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/tutorials/creative-coding/building-a-wordpress-friendly-api-wrapper-getting-started/</feedburner:origLink></item> <item><title>The WordPress Coding Standards: An Introduction</title><link>http://feedproxy.google.com/~r/Wptuts/~3/WkgUMBcXsIo/</link> <comments>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-an-introduction/#comments</comments> <pubDate>Tue, 04 Jun 2013 14:09:53 +0000</pubDate> <dc:creator>Tom McFarlin</dc:creator> <category><![CDATA[Cheat Sheets]]></category> <category><![CDATA[codex]]></category> <category><![CDATA[Coding Standards]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31678</guid> <description>&lt;div
class="seriesmeta"&gt;This entry is part 1 of 4 in the series &lt;a
href="http://wp.tutsplus.com/series/the-wordpress-coding-standards/" class="series-1128" title="The WordPress Coding Standards"&gt;The WordPress Coding Standards&lt;/a&gt;&lt;/div&gt;&lt;p&gt;When it comes to building WordPress-based products, we&amp;#8217;re somewhat cursed (or blessed, depending on how you see it), with a double-edged sword: Because WordPress is written in PHP, it&amp;#8217;s relatively easy to get WordPress &amp;#8211; or the project itself &amp;#8211; to do whatever it is we want to do all the while avoiding best practices.&lt;/p&gt;&lt;p&gt;But this raises the question: What&amp;#8217;s the point of an API or formal coding standards if we&amp;#8217;re simply going to ignore them?&lt;/p&gt;&lt;p&gt;We&amp;#8217;ve written quite a bit about the WordPress APIs in previous articles, and we&amp;#8217;ve touched on the WordPress Coding Standards, but we&amp;#8217;ve never really taken a deep dive into the coding standards, understanding each aspect of them, and why they matter.&lt;/p&gt;&lt;p&gt;So in this series, we&amp;#8217;re going to be doing just that.&lt;span
id="more-31678"&gt;&lt;/span&gt;&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;Why Bother With Coding Standards?&lt;/h2&gt;&lt;p&gt;Before taking a look at everything that we&amp;#8217;re going to be covering, I think it&amp;#8217;s important to understand why coding standards even matter.&lt;/p&gt;&lt;p&gt;WordPress is widely used by &lt;em&gt;a lot&lt;/em&gt; of people using &lt;em&gt;a lot&lt;/em&gt; of sites. There&amp;#8217;s a lot of code being employed out there that&amp;#8217;s all built on top of the core application. This means that there are developers and designers who are working to maintain those code bases.&lt;/p&gt;&lt;p&gt;The challenge comes when the person who didn&amp;#8217;t originally write the code is stuck with improving, managing, and fixing bugs for the project. Let&amp;#8217;s say that the first person didn&amp;#8217;t follow the coding standards, but the person who is now maintaining the code is used to following best practices.&lt;/p&gt;&lt;p&gt;They now have to deal with code that looks nothing like what WordPress code should look like.&lt;/p&gt;&lt;p&gt;And that&amp;#8217;s really the crux of the matter: The code that makes up our themes, our plugins, and in our applications should ideally look like a single developer wrote the code.&lt;/p&gt;&lt;p&gt;&lt;em&gt;That&amp;#8217;s&lt;/em&gt; what coding standards enforce.&lt;/p&gt;&lt;p&gt;Unfortunately, standards are either ignored or they aren&amp;#8217;t evangelized enough despite the fact they are well-documented. That&amp;#8217;s where this series of articles comes into play: We&amp;#8217;re going to be looking at each of the standards in-depth to evangelize, to understand, and to encourage one another to begin doing a better job.&lt;/p&gt;&lt;hr
/&gt;&lt;h2&gt;A Look at the Coding Standards&lt;/h2&gt;&lt;p&gt;This series is going to include seven article in addition to this introductory piece.&lt;/p&gt;&lt;p&gt;Here&amp;#8217;s where we are headed with this particular series:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;strong&gt;Naming Conventions and Function Arguments&lt;/strong&gt; &amp;#8211; We&amp;#8217;ll take a look at how we should name our variables, methods, and classes such that they are clear, easy to understand, and follow for ourselves and other developers.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The Use of Single Quotes and Double Quotes&lt;/strong&gt; &amp;#8211; Single quotes and double-quotes result in different behavior in PHP and there is a time and a place for when to use which in WordPress. In this article, we&amp;#8217;ll take a look at examples of each and the rationale behind what we&amp;#8217;re doing.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Indentation, Space Usage, and Trailing Spaces&lt;/strong&gt; &amp;#8211; White space places a big part in writing WordPress-based code. This article is going to explore the reasons why indentation and space matter as well as when to use tabs and when to use spaces.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Brace Style, Regular Expressions, and PHP Tags&lt;/strong&gt; &amp;#8211; Different C-style languages use different syntax. This article will examine the proper way to place braces in your WordPress-based code. We&amp;#8217;re also going to examine the proper way to include regular expressions, as well as some of the gotchas and best practices as it relates to using PHP tags.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The Ternary Operator and Yoda Conditions&lt;/strong&gt; &amp;#8211; Conditional statements are a corner stone of programming and they are used liberally throughout WordPress. PHP offers a shortcut for if/else statements, and WordPress opts to evaluate conditionals differently than what other languages often support. Here, we&amp;#8217;ll take a look at all of the above.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Database Queries and Formatting SQL Queries&lt;/strong&gt; &amp;#8211; There&amp;#8217;s a full API for running queries against the WordPress database, but there are also ways to run raw SQL. If you ever need to go that route, there&amp;#8217;s a right way to do it. This article will take a look at exactly that.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Bringing It All Together&lt;/strong&gt; &amp;#8211; Finally, we&amp;#8217;re going to summarize everything that we&amp;#8217;ve discussed throughout the series to make sure that we&amp;#8217;re all on the same page, and to urge us to move forward with doing a better job of adhering to the standards.&lt;/li&gt;&lt;/ol&gt;&lt;hr
/&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;When it comes to blogging, it&amp;#8217;s often assumed that the author is an expert in the topic at hand, but the truth is although I firmly believe that we should follow the coding standards and that I try to adhere to them to the best of my ability, I&amp;#8217;m learning things each week.&lt;/p&gt;&lt;p&gt;I&amp;#8217;ve violated the coding standards plenty of times, and I&amp;#8217;m sure some of you have, as well.&lt;/p&gt;&lt;p&gt;The point of this series is to walk through the standards together so that we may all come out as better developers once we&amp;#8217;re done.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/WkgUMBcXsIo" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-an-introduction/feed/</wfw:commentRss> <slash:comments>9</slash:comments> <series:name><![CDATA[The WordPress Coding Standards]]></series:name> <feedburner:origLink>http://wp.tutsplus.com/articles/cheat-sheets/the-wordpress-coding-standards-an-introduction/</feedburner:origLink></item> <item><title>Quick Tip: Add Shortlinks to Custom Post Types</title><link>http://feedproxy.google.com/~r/Wptuts/~3/K_c0fQgqyT4/</link> <comments>http://wp.tutsplus.com/articles/tips-articles/quick-tip-add-shortlinks-to-custom-post-types/#comments</comments> <pubDate>Mon, 03 Jun 2013 12:43:36 +0000</pubDate> <dc:creator>Stephen Harris</dc:creator> <category><![CDATA[Tips]]></category> <category><![CDATA[quick tip]]></category> <category><![CDATA[shortlinks]]></category> <guid isPermaLink="false">http://wp.tutsplus.com/?p=31892</guid> <description>&lt;p&gt;If you use any form of social media, and in particular Twitter, then you have almost certainly come across &amp;#8216;shortlinks&amp;#8217; – shortened URLs which act as a label, pointing to a particular page but disguising its lengthier URL. They&amp;#8217;ve been around for over a decade now, but their use really took off with URL shortening services which provided click-through statistics, and character limits on tweets.&lt;span
id="more-31892"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;WordPress has its own built in &amp;#8216;shortlink&amp;#8217; – which, by default, probably isn&amp;#8217;t very deserving of the name. These are the &lt;code&gt;www.yoursite.com?p=1&lt;/code&gt; links which point to a single post and you can grab them from the &amp;#8216;Get Shortlink&amp;#8217; button on your post&amp;#8217;s edit screen.&lt;/p&gt;&lt;p&gt;There&amp;#8217;s a good reason for this: WordPress did not want to impose any particular third-party service for URL-shortening, and beneath the default &lt;code&gt;www.yoursite.com?p=1&lt;/code&gt; shortlinks lies an API which allows you to replace it with a more substantially shortened URL from another service – or &lt;a
href="http://yourls.org/" title="YOURLS: Your Own URL Shortener" target="_blank"&gt;perhaps even your own&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;But WordPress&amp;#8217; shortlinks only appear on posts – not pages, or any other post type. In this quick tip I&amp;#8217;ll be showing you how to rectify this. (And in a similar way you can change the default shortlink entirely by one from a URL shortener service).&lt;/p&gt;&lt;p&gt;Cracking open the source code and locating the &lt;code&gt;wp_get_shortlink()&lt;/code&gt; function (see &lt;a
href="http://codex.wordpress.org/Function_Reference/wp_get_shortlink"&gt;Codex&lt;/a&gt;) we find the following:&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {

    // Allow plugins to short-circuit this function.
    $shortlink = apply_filters('pre_get_shortlink', false, $id, $context, $allow_slugs);
    if ( false !== $shortlink )
        return $shortlink;

        ...
&lt;/pre&gt;&lt;p&gt;The hook &lt;code&gt;pre_get_shortlink&lt;/code&gt;, therefore, allows us to by-pass WordPress&amp;#8217; default handling of shortlinks. To do that our plug-in needs only to hook onto that filter and return anything other than &amp;#8216;false&amp;#8217;.&lt;/p&gt;&lt;pre class="brush: php; title: ; notranslate"&gt;
/**
 * A function which adds a shortlinks button for 'portfolio' post type
 */
function wptuts_shortlinks_for_portfolio( $shortlink, $id, $context ) {

	// Context can be post/blog/meta ID or query
	$post_id = 0;

	if ( 'query' == $context &amp;amp;&amp;amp; is_singular( 'portfolio' ) ) {

		// If context is query use current queried object for ID
		$post_id = get_queried_object_id();

	}
	elseif ( 'post' == $context ) {

		// If context is post use the passed $id
		$post_id = $id;

	}

	// Only do something if of portfolio post type
	if ( 'portfolio' == get_post_type( $post_id ) ) {
		$shortlink = home_url( '?p=' . $post_id );
	}

	return $shortlink;
}
add_filter( 'pre_get_shortlink', 'wptuts_shortlinks_for_portfolio', 10, 3 );
&lt;/pre&gt;&lt;p&gt;Note that if you do not want to change the shortlink (for instance, it&amp;#8217;s the wrong post type) it&amp;#8217;s important to return &lt;code&gt;$shortlink&lt;/code&gt; (the filtered value that was passed to us by the hook) and not &amp;#8216;false&amp;#8217; &amp;#8211; since other plug-ins may have already changed &lt;code&gt;$shortlink&lt;/code&gt; &amp;#8211; and by returning false you would be overriding them.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/Wptuts/~4/K_c0fQgqyT4" height="1" width="1"/&gt;</description> <wfw:commentRss>http://wp.tutsplus.com/articles/tips-articles/quick-tip-add-shortlinks-to-custom-post-types/feed/</wfw:commentRss> <slash:comments>3</slash:comments> <feedburner:origLink>http://wp.tutsplus.com/articles/tips-articles/quick-tip-add-shortlinks-to-custom-post-types/</feedburner:origLink></item> </channel> </rss><!-- Dynamic Page Served (once) in 1.254 seconds -->
