<?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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Pro Blog Design</title>
	
	<link>http://www.problogdesign.com</link>
	<description>Advice for blog owners and designers on making a more attractive, usable and ultimately profitable blog.</description>
	<lastBuildDate>Mon, 14 May 2012 16:00:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ProBlogDesign" /><feedburner:info uri="problogdesign" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>ProBlogDesign</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/ProBlogDesign" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FProBlogDesign" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:browserFriendly>Thanks for looking here. It's free to subscribe to my feed and it means that each time I write a post, it will be sent straight to you. You'll be a WordPress and web design pro in no time! (Well, if you aren't already! ;) )</feedburner:browserFriendly><item>
		<title>7 Plugins to Save Time With the Dashboard</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/jaX4iRrQ1Fk/</link>
		<comments>http://www.problogdesign.com/wordpress/7-plugins-to-save-time-with-the-dashboard/#comments</comments>
		<pubDate>Mon, 14 May 2012 16:00:56 +0000</pubDate>
		<dc:creator>Claudia Somerfield</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[AdSense]]></category>
		<category><![CDATA[Analytics]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Dashboard]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8801</guid>
		<description><![CDATA[<img class="alignnone size-full wp-image-8815" title="dashboard-plugins" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/dashboard-plugins.jpg" alt="" width="629" height="180" />

As bloggers, we spend a lot of time working on a range of different tasks. This includes things like checking stats, monitoring ad revenue, and interacting on social media.

That leaves us with a lot of sites to log into. But by adding a few WordPress plugins, you can pull some of this data back into the WordPress dashboard, which saves you time by giving a quick snapshot overview of everything.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8815" title="dashboard-plugins" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/dashboard-plugins.jpg" alt="" width="629" height="180" /></p>
<p>As bloggers, we spend a lot of time working on a range of different tasks. This includes things like checking stats, monitoring ad revenue, and interacting on social media.</p>
<p>That leaves us with a lot of sites to log into. But by adding a few WordPress plugins, you can pull some of this data back into the WordPress dashboard, which saves you time by giving a quick snapshot overview of everything.</p>
<h3>Analytics 360</h3>
<p><img class="alignnone size-full wp-image-8813" title="analytics-360" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/analytics-360.jpg" alt="" width="629" height="180" /></p>
<p>Analytics 360 brings Google Analytics and Mailchimp (Newsletter service) data together, and displays them on a subpage under the WordPress dashboard. This helps in accessing analytics tools like visualizing site traffic and managing mailing lists easier even without leaving WordPress.</p>
<p>A Google Analytics account is required, but you do not need to be a Mailchimp user.</p>
<p><a href="http://wordpress.org/extend/plugins/analytics360/">Analytics 360 Download</a>.</p>
<h3>My Gmail</h3>
<p>My Gmail is another simple dashboard plugin which displays all the unread mails from your Gmail account without any need to load Gmail.</p>
<p>However, this plugin works only over HTTPS, so you will need to have purchased an SSL certificate.</p>
<p><a href="http://wordpress.org/extend/plugins/my-gmail/">My Gmail Download</a>.</p>
<h3>Full Comments on Dashboard</h3>
<p><img class="alignnone size-full wp-image-8808" title="full-comments" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/full-comments.jpg" alt="" width="629" height="180" /></p>
<p>The Recent Comments Box in WordPress facilitates managing and replying to comments easily, but it just shows the comment excerpt and the user has to load another page to view the full comment.</p>
<p>This limitation can be overcome with the Full Comments on Dashboard plugin. It extends the default widget to show the full comment, allowing you to reply properly.</p>
<p><a href="http://wordpress.org/extend/plugins/full-comments-on-dashboard/">Full Comments on Dashboard Download</a>.</p>
<h3>Google Adsense Summary</h3>
<p>This is another important plugin which lets the user to check Adsense earnings for a stipulated time. After the successful installation of the plugin, the user should enter his username and password in the settings page and the earnings from his/her account are displayed anytime.</p>
<p><a href="http://wordpress.org/extend/plugins/google-adsense-summary/">Google Adsense Summary Download</a>.</p>
<h3>Feedburner Stats</h3>
<p><img class="alignnone size-full wp-image-8809" title="feedburner" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/feedburner.jpg" alt="" width="629" height="180" /></p>
<p>Feed Stats is a simple statistical tool for viewing and managing FeedBurner statistics. Under the Dashboard, you will find a &#8220;Feed Stats&#8221; link with graphs based on your recent FeedBurner data.</p>
<p>The plugin displays your FeedBurner statistics for things like subscribers on your blog, which the feed readers are being used, and which of the posts are most popular with subscribers.</p>
<p><a href="http://wordpress.org/extend/plugins/feed-stats-plugin-for-wordpress-reworked/">Feedburner Stats Download</a>.</p>
<h3>Twitter Tools</h3>
<p>The Twitter Tools plugin makes it easy for the WordPress user to manage his Twitter posts. This plugin helps to Tweet directly from the dashboard itself without opening another browser tab.</p>
<p>Using this plugin, the user can automatically Tweet about new posts, create a collection of his Tweets and publish on the users blogs, Tweet from any page on the blog, etc.</p>
<p><a href="http://wordpress.org/extend/plugins/twitter-tools/">Twitter Tools Download</a>.</p>
<h3>Plugin Central</h3>
<p><img class="alignnone size-full wp-image-8811" title="plugin-central" src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/plugin-central.jpg" alt="" width="629" height="180" /></p>
<p>Finally, Plugin Central is a plugin for managing all other plugins for WordPress. This lets you easily install plugins by naming them, or linking to a zip folder, as well as adding multiple plugins at once, and updating all of your existing plugins directly from the dashboard.</p>
<p><a href="http://wordpress.org/extend/plugins/plugin-central/">Plugin Central Download</a>.</p>
<p>And that concludes our list. Not all of these plugins will be for you, but if you spend a lot of time on some of the services above, then give it a go and see if it saves you some time!</p>
<p>If you need more ideas, check out the <a href="http://wordpress.org/extend/plugins/tags/dashboard">dashboard tag</a> on the WP.org Plugin Directory.</p>
<p>If you have any other suggestions for great dashboard plugins, let me know in the comments!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/jaX4iRrQ1Fk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/7-plugins-to-save-time-with-the-dashboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/7-plugins-to-save-time-with-the-dashboard/</feedburner:origLink></item>
		<item>
		<title>CSS &amp; JS Auto-Refreshing with Live.js</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/i14SPV9jrM4/</link>
		<comments>http://www.problogdesign.com/tools/css-js-auto-refreshing-with-live-js/#comments</comments>
		<pubDate>Thu, 03 May 2012 16:00:40 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Live.js]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8789</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/livejs-wp.jpg" alt="Live.js and WordPress" title="Live.js and WordPress" width="629" height="180" class="alignnone size-full wp-image-8795" />

I want to share a fantastic little tool that I've only recently come across; <a href="http://livejs.com/">Live.js</a>, written by <a href="http://twitter.com/mrtnkl">Martin Kool</a>. 

This JavaScript file <strong>automatically checks for changes</strong> to your CSS and JavaScript files, and refreshes them.

That means you can have your text editor in one half of the screen, and a web browser in the other. When you save changes in the editor, the <strong>updates are reflected immediately in your browser</strong>. It really helps to speed up your development time.]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2012/05/livejs-wp.jpg" alt="Live.js and WordPress" title="Live.js and WordPress" width="629" height="180" class="alignnone size-full wp-image-8795" /></p>
<p>I want to share a fantastic little tool that I&#8217;ve only recently come across; <a href="http://livejs.com/">Live.js</a>, written by <a href="http://twitter.com/mrtnkl">Martin Kool</a>. </p>
<p>This JavaScript file <strong>automatically checks for changes</strong> to your CSS and JavaScript files, and refreshes them.</p>
<p>That means you can have your text editor in one half of the screen, and a web browser in the other. When you save changes in the editor, the <strong>updates are reflected immediately in your browser</strong>. It really helps to speed up your development time.</p>
<p>A lot of text editors offer HTML previews and such, but they only work for plain .html pages. The great thing about Live.js is that it works <strong>on any webpage</strong>, including a working WordPress theme.</p>
<h2>Live.js for WordPress Plugin</h2>
<p>I have put together a (very!) simple plugin for WordPress to make things that little bit easier. You can <a href="https://github.com/michael-martin/PBD-Live.js-for-WordPress/zipball/master">download it here</a> (<strong>Rename the folder</strong> to &#8220;pbd-livejs&#8221; once you unzip it), or view it on <a href="https://github.com/michael-martin/PBD-Live.js-for-WordPress">Github</a>.</p>
<p>It automatically <strong>adds Live.js</strong> to your theme, but only when you are <strong>logged in and working locally</strong>. You should never use this script on a live site, and the plugin ensures you won&#8217;t.</p>
<p>It also keeps the script out of your theme code, which is good because it&#8217;s purely for development. There is no need for it to be present in the files on your server.</p>
<p>One final note is that although Live.js also supports sensing HTML changes, I have disabled this. With it enabled, your webpage essentially &#8220;flashes&#8221; as it loads every second or two.</p>
<p>Let me know if you try it out, or if you&#8217;ve used Live.js before already!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/i14SPV9jrM4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/tools/css-js-auto-refreshing-with-live-js/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/tools/css-js-auto-refreshing-with-live-js/</feedburner:origLink></item>
		<item>
		<title>WordPress Development: Bypassing the Settings API</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/0yMW9ylDass/</link>
		<comments>http://www.problogdesign.com/wordpress/wordpress-development-bypassing-the-settings-api/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 16:00:58 +0000</pubDate>
		<dc:creator>Andy Walpole</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Settings]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8751</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/bypassing-settings-api.png" alt="Bypass the Settings API" title="Bypass the Settings API" width="629" height="180" class="alignnone size-full wp-image-8773" />

<em>The following is a guest post by <a href="http://andywalpole.me/">Andy Walpole</a>.</em><a href="https://gist.github.com/1996412"></a>

There are many parts of the Wordpress API which are fantastic but there are also other parts which, I would argue, are lacking.

The Settings API was introduced in version 2.7 to allow the semi-automation of form creation. All credible Content Management Systems and frameworks have their own set of functions or classes for the same purpose. Drupal has a <a href="http://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7">multitude of hooks which can be leveraged</a>, while CodeIgniter uses a combination of the <a href="http://codeigniter.com/user_guide/libraries/form_validation.html">Form Validation Class</a> and the Form Helper .

When creating a Wordpress plugin recently I wanted to create a dynamic form to insert data into a field in the option database table. I decided to create a class for this purpose with the intention of creating reusable code for future projects.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8773" title="Bypass the Settings API" src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/bypassing-settings-api.png" alt="Bypass the Settings API" width="629" height="180" /></p>
<p><em>The following is a guest post by <a href="http://andywalpole.me/">Andy Walpole</a>.</em></p>
<p>There are many parts of the WordPress API which are fantastic but there are also other parts which, I would argue, are lacking.</p>
<p>The Settings API was introduced in version 2.7 to allow the semi-automation of form creation. All credible Content Management Systems and frameworks have their own set of functions or classes for the same purpose. Drupal has a <a href="http://api.drupal.org/api/drupal/includes!form.inc/group/form_api/7">multitude of hooks which can be leveraged</a>, while CodeIgniter uses a combination of the <a href="http://codeigniter.com/user_guide/libraries/form_validation.html">Form Validation Class</a> and the Form Helper .</p>
<p>When creating a WordPress plugin recently I wanted to create a dynamic form to insert data into a field in the option database table. I decided to create a class for this purpose with the intention of creating reusable code for future projects. For ideas, I closely followed previous tutorials written by <a href="http://alisothegeek.com/2011/01/wordpress-settings-api-tutorial-1/">Alison Kleinschmidt</a> and <a href="http://wp.tutsplus.com/tutorials/using-the-settings-api-part-1-create-a-theme-options-page/">Sarah Neuber </a>.</p>
<p>I quickly ran into a brickwall. My main issue that I have with the Settings API is that it’s unintuitive and too basic. It is such an abstraction away from the meat and bones of PHP / HTML form creation and validation that it confuses the professional coder rather than assists them.</p>
<p>As a coding exercise I decided to take a different approach to WordPress form creation using OOP and one that had a more familiar user interface. The code I have written so far can be examined on a <a href="https://gist.github.com/1996412">Git Gist that I have created</a>. There are some excellent features of the Settings API, such as inserting new form fields into existing core admin pages; and this code doesn&#8217;t attempt to replicate that.</p>
<p>This purpose of this article is to provide some feedback on the successes and difficulties of my endeavourer.</p>
<p>There are some key aspects in understanding the option API access class. Firstly, I placed it into three files reflecting an MVC architecture. Admittedly, the script itself doesn&#8217;t tightly fit this segregation but it is a commonly used and understood way of organising code. In the view file there are key WordPress hooks such as add_action() and add_options_page(); in the controller class are the purpose built form field creation methods and in the model class are validation, sanitisation, security and database functions.</p>
<p>Another aspect is that I have used namespaces and late static bindings that arrived with PHP 5.3. It is a convention amongst WordPress developers to cater for servers that have versions of PHP lower than 5.3. I don&#8217;t follow that convention. If your hosting company is running versions of PHP which are three years behind or more then take you custom elsewhere because they are not providing to you a satisfactory service.</p>
<h2>Completed Code</h2>
<p>This is a lengthy tutorial, and walks you through exactly how the script works. Instead of manually copying and pasting all of the code chunks to assemble the file though, it will be simpler just to copy the <a href="https://gist.github.com/1996412">finished code</a> from here.</p>
<h2>1 &#8211; Instantiate the Class</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$aForm</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'option_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'a_url_here'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// has to be alphanumeric and underscores only</span>
    <span style="color: #0000ff;">'page_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'A page title here'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Main page title</span>
    <span style="color: #0000ff;">'page_url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'a-url-here'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// URL</span>
    <span style="color: #0000ff;">'dynamic_output'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Should the form be generated on more input</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">new</span> \OptionView\Form_View<span style="color: #009900;">&#40;</span><span style="color: #000088;">$aForm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above should be reasonably self-explanatory. In the $aform array are placed four different key values: the name of the option database field, the page title, the page URL and a boolean value &#8211; I&#8217;ll explain this in detail later but for now it&#8217;s set to false.</p>
<p>The create_html_cov() method is called by WordPress add_options_hook(). In here is the code that envelopes the form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;div class=&quot;wrap&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> screen_icon<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;h2&gt;<span style="color: #006699; font-weight: bold;">{$page_title}</span>&lt;/h2&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;div id=&quot;result&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// validation here</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update_option</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">failure_message</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if error</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if isset submit</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Anybody who codes PHP will be familiar with the above. After form submission any error messages are added to an array and then displayed. At the top is a div with a class of wrap. This is essential so that the admin CSS is used. Note as well that the page title originally made in the $aForm array is now been called from the static $form attribute. How? The values were used in the method below which in turn was called in the view constructor. The values within config_settings() are used throughout the script.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Form_Controller::config_settings()
 * Main array for important values throughout the class
 * @param string $option_name
 * @param string $page_title
 * @param string $page_url
 * @param boolean $dynamic_output
 * @return array
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> config_settings<span style="color: #009900;">&#40;</span><span style="color: #000088;">$option_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page_title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page_url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dynamic_output</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// put together the output array</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'option_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$option_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// name of option database field</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page_title'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page_title</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// name of page</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page_url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page_url</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// url of page</span>
    <span style="color: #000088;">$output</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dynamic_output'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dynamic_output</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>2 &#8211; Creating the Form Fields</h2>
<p>It is first necessary to create a form array:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#result'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'enctype'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Add a description here'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The description will go inbetween the legend form tags at the top of the form. The rest are basic attribute form values.</p>
<p>When invoking the create_form() method the form array has to be the first value:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create_form</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>From here more arrays have to be created for individual form fields. Note how closely the PHP fits the HTML form attributes – something that the user interface of the Settings API is distant from.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$textOne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;text&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// input type</span>
    <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;textOne&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// name attribute</span>
    <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;This is a text field&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// for use in input label</span>
    <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;200&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// max attribute</span>
    <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;YES&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// value attribute</span>
    <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #666666; font-style: italic;">// array only for the select input</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$textTwo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;text&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;textTwo&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;This is another text field:&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;250&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;YES&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The input can be text, textfield, radio, select or checkbox. The desc key is used in the form field label. Values for text or textfields should be “YES” only and by doing so instructs the class to create a sticky form field. Select is only used for drop-down fields, radio buttons and checkboxes (more of which later). Even if an option is not used there stills needs to be “false” or “null” stated.</p>
<p>Now the two arrays are placed in create_form():</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create_form</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textOne</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The form has now been created.</p>
<p><a href="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-one1.jpg"><img class="alignnone size-full wp-image-8753" title="screenshot-one" src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-one1.jpg" alt="" width="683" height="648" /></a></p>
<h2>3 &#8211; Validation and Sanitisation</h2>
<p>The following method is used for sanitizing data. If, for instance, it is desirable to remove backslashes or trim white space from the ends of strings, then it is possible to do so like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sanitize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'trim_post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sanitize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'stripslashes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s straight forward enough. Lets take a look at the code behind this in the model class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Form_Model::sanitize()
 * 
 * @param string $handle
 * @param array $form_output
 * @return array
 */</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> sanitize<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'sanitize_post'</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sanitize_post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'trim_post'</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'trim_post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'strip_tags_post'</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'strip_tags_post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'empty_value'</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'empty_value'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'stripslashes'</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'stripslashes'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
            <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The value you ended into the sanitize() method is not recognised: <span style="color: #006699; font-weight: bold;">$handle</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end switch</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Form_Model::trim_post()
 * 
 * @param string $att
 * @param string $single
 * @param array $form_output
 * @return array
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> trim_post<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #000088;">$att</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$single</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$single</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">array_walk_recursive</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'trim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$form_output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>static<span style="color: #339933;">::</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$option_name</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$thisKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$att</span>/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thisKey</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thisKey</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$form_output</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$option_name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$thisKey</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$form_output</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As demonstrated by the trim_post() method it is easy to widen the sanitisation features of the code. Just copy and paste the method, change the name and change trim to another function such as strip_tags() or htmlspecialchars(). Then add a new section to the switch statement in sanitize().</p>
<p>Notice that $this-&gt;sanitize($form, &#8216;trim_post&#8217;) trims all input data. It is possible to just sanitise one field by bypassing sanitize() and call its child method directly:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">strip_tags_post</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'feedName'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">trim_post</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'feedName'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'feedName'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Necessary parameters above are the form array, the name of the attribute and a boolean value of true.</p>
<p>Validation is similar to sanitization. Here are two examples of using validation methods:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Form_Model::validate_email()
 * 
 * @param string $att
 * @return boolean
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> validate_email<span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #339933;">,</span> <span style="color: #000088;">$att</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span>static<span style="color: #339933;">::</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$att</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_output</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$option_name</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$thisKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$att</span>/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thisKey</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> FILTER_VALIDATE_EMAIL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if</span>
&nbsp;
            <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end foreach</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Make sure that the inputs for validate_url() is an array and a string&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To extend the different types of validation it is, like sanitization, a straightforward process of copying the code, renaming it and changing the filter_var().</p>
<p>One other useful validation method is duplicate_entries():</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">duplicate_entries</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please make sure that all input values are unique&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will ensure that no one inputted value is identical.</p>
<h2>4 &#8211; Select Drop-Down Lists, Radio Buttons &amp; Checkboxes.</h2>
<p>The creating and validating of these form fields is slightly different.</p>
<p>Firstly, when you create radio buttons or checkboxes you must do so by specifying the total number of each in the select value of the field creation array:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$aRadioButton</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radio&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radioButtonName&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A radio button&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mac&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$aCheckBox</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radio&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aCheckBoxName&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First checkbox&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mushrooms&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above would specify that there are three checkboxes in the form and three radio buttons (obviously, it is necessary to create separate arrays for all individual fields).</p>
<p>As is standard in HTML, the radio buttons must have the same name attribute. In this script checkboxes must have individual name attributes. I&#8217;m working on a solution to creating checkboxes with the same name attributes! Notice as well that, unlike for text or textareas, the value is no longer “YES” but a value unique to all fields.</p>
<p>Previously, checking for empty submitted values was done so through the empty_value() method</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">// Check whether there are any empty form values:</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_value</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please don't leave any input values empty&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Radio buttons and checkboxes have their own methods:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_checkboxes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please check at least two checkboxes&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_radio_butts</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please make sure that you check one of the radio buttons&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In empty_checkboxes() the second digit is the minimum number of checkboxes that the user needs to click. If none is specified then the default is one.</p>
<p>When creating a drop-down list it is necessary to add any array of values to the select key:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$cities</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'Shanghai'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Karachi'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Mumbai'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Beijing'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Moscow'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Sao Paulo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Tianjin'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Guangzhou'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Delhi'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Seoul'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Shenzhen'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Jakarta'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Tokyo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Mexico City'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Istanbul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;select&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;selectName&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Select here select here&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$cities</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Putting together the above examples, the code for a form with multiple different types of input would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> create_html_cov<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// essential.</span>
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;div class=&quot;wrap&quot;&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> screen_icon<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;h2&gt;<span style="color: #006699; font-weight: bold;">{$page_title}</span>&lt;/h2&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;This is the admin section for Affiliate Hoover plugin&lt;/p&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;div id=&quot;result&quot;&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// ESSENTIAL! Do not leave this out. Needs to come first</span>
        <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">security_check</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Sanitization</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sanitize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'trim_post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sanitize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'stripslashes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Validation</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate_email</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'feedName'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please make sure that the email addresses are correct&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'urlName'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;URL is not right&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Check whether there are any empty form values for text or textarea fields:</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_value</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please don't leave any input values empty&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// don't allow empty checkboxes</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_checkboxes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please check at least two checkboxes&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// don't allow empty radio buttons</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">empty_radio_butts</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please make sure that you check one of the radio buttons&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Make sure that none of the form values are duplicates</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">duplicate_entries</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Please make sure that all input values are unique&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update_option</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">failure_message</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if error</span>
&nbsp;
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end if isset submitForm</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Create the form here:</span>
&nbsp;
    <span style="color: #000088;">$aTextField</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;text&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// input type</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;textOne&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// name attribute</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;This is a text field&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// for use in input label</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;200&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// max attribute</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;YES&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// value attribute</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #666666; font-style: italic;">// array only for the select input</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aTextArea</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;textarea&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aTextArea&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;This is a textarea:&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;YES&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aRadioButtonOne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radio&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radioButtonName&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A radio button one&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mac&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aRadioButtonTwo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radio&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radioButtonName&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A radio button two&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;linux&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aRadioButtonThree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radio&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;radioButtonName&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A radio button three&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;pc&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aCheckBoxOne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;checkbox&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aCheckBoxOne&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;A radio button one&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;mushrooms&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aCheckBoxTwo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;checkbox&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aCheckBoxTwo&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First checkbox&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;pizza&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$aCheckBoxThree</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">&quot;input&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;checkbox&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;aCheckBoxThree&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;desc&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;First checkbox&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;maxlength&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;chicken&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;select&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'method'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#result'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'enctype'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'multipart/form-data'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Add a new form underneath'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create_form</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aTextField</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aTextArea</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aRadioButtonOne</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aRadioButtonTwo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aRadioButtonThree</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$aCheckBoxOne</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aCheckBoxTwo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aCheckBoxThree</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;!-- end of wrap div --&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above will create a form that looks like this:</p>
<p><a href="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-two.jpg"><img class="alignnone size-full wp-image-8754" title="screenshot-two" src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-two.jpg" alt="screenshot two" width="684" height="637" /></a></p>
<h2>5 &#8211; Dynamic Forms</h2>
<p>Creating a form layout that enables the easy adding, deleting or editing of multiple form blocks with one submit button is almost impossible with PHP alone. The best solution would be AJAX but this is primarily intended as a server-side coding exercise.</p>
<p>To create a dynamic form with my Option API access class then the only value to change is the dynamic output booleanwhen instantiating the class.</p>
<p>It is now possible to create a form with different sections that can be independently deleted or edited.</p>
<p>An example being:</p>
<p><a href="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-three.jpg"><img class="alignnone size-full wp-image-8755" title="screenshot-three" src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/screenshot-three.jpg" alt="screenshot three" width="683" height="684" /></a></p>
<h2>Conclusion</h2>
<p>The code I linked to in the Git Gist is still in its development stage. There are a number of issues. Firstly, it is not possible to create checkboxes with the same name attribute – they have to be separate. Secondly, it is only possible to create one set of radio buttons per form section.</p>
<p>Attempting to create a dynamic form like I have done so in this Option API access class create an entire new level of code complexity. Because a server-side script like PHP has only limited use of the DOM, it is necessary to find solutions based on loops of the form arrays and a lot of maths. This is an aspect of the code that I will continue working on.</p>
<p>But, positive parts of the Option API access class are that it takes care of all tables HTML and CSS peculiar to the WordPress admin section; it is relatively easy to create and validate new forms with a multitude of fields, and all security aspects of the WordPress backend like wp_verify_nonce() are incorporated into the form submission process.</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/0yMW9ylDass" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/wordpress-development-bypassing-the-settings-api/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/wordpress-development-bypassing-the-settings-api/</feedburner:origLink></item>
		<item>
		<title>How to Install and Customize Varnish for WordPress</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/d3hIaLEOOV8/</link>
		<comments>http://www.problogdesign.com/wordpress/how-to-install-and-customize-varnish-for-wordpress/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 16:00:13 +0000</pubDate>
		<dc:creator>Austin Gunter</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Varnish]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8726</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/varnish.png" alt="Varnish and WordPress" title="Varnish and WordPress" width="629" height="180" class="alignnone size-full wp-image-8737" />

<em>The following is a guest post by Austin Gunter of <a href="http://wpengine.com/">WPEngine.com</a>.</em>

If you’re looking to boost performance for a content-heavy WordPress installation, adding a cache like <a href="https://www.varnish-cache.org/">Varnish</a> is a great way to boost your site’s performance.

NB - This is an advanced topic, and only relevant if <strong>you have full control over your server</strong> (e.g. you're on a VPS). It does not apply to regular webhosting.]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2012/04/varnish.png" alt="Varnish and WordPress" title="Varnish and WordPress" width="629" height="180" class="alignnone size-full wp-image-8737" /></p>
<p><em>The following is a guest post by Austin Gunter of <a href="http://wpengine.com/">WPEngine.com</a>.</em></p>
<p>If you’re looking to boost performance for a content-heavy WordPress installation, adding a cache like <a href="https://www.varnish-cache.org/">Varnish</a> is a great way to boost your site’s performance.</p>
<p>NB &#8211; This is an advanced topic, and only relevant if <strong>you have full control over your server</strong> (e.g. you&#8217;re on a VPS). It does not apply to regular webhosting.</p>
<h2>What is Varnish?</h2>
<p>Varnish Cache is a <strong>web app accelerator</strong>, or a caching HTTP <a href="http://en.wikipedia.org/wiki/Reverse_proxy">reverse proxy</a>. Install it in front of any server that speaks HTTP and configure it to cache the contents, and the Varnish community claims that it speeds up delivery by a factor of 300 &#8211; 1000x.<strong> </strong></p>
<p>In a nutshell, caching a webpage means storing a copy of that website’s content for future visitors to see. Varnish can cache pages of your WordPress site so that your server <strong>doesn’t need to call the database</strong> each time a visitor visits your site. This reduces server load because the stored copy means the webserver doesn’t have to go find the same images and content for each visitor.</p>
<p>Varnish caches page data in virtual memory, so your site will load much faster, providing a killer SEO boost. Google found that every additional <strong>0.5s load time meant 20% fewer site visitors</strong> (<a href="http://news.cnet.com/8301-10784_3-9954972-7.html">Source</a>). Reducing the page load time can dramatically increase your visitors and boost your search rankings at the same time.</p>
<p>Varnish themselves have put together a great video for explaining what it does, as simply as possible:</p>
<p><iframe src="http://www.youtube.com/embed/x7t2Sp174eI" frameborder="0" width="560" height="315"></iframe></p>
<h2>Installing Varnish</h2>
<p>Varnish is free software that you can install this afternoon. It runs on Linux, and primarily FreeBSD, but can work on other platforms as well. Once installed, you can customize <strong>how incoming requests will be handled</strong> with the Varnish Configuration Language (VCL).</p>
<p>This flexibility means that each Varnish install can and should be designed with a <strong>particular site in mind</strong>.</p>
<p>You’ll want to begin with a basic configuration of Varnish, <strong>and then begin testing small changes</strong> as you get the hang of how it works with your particular site, and how the functions work. There are several different subroutines that tell Varnish how to respond to requests going in and out, to errors, and so on.</p>
<p>I’ll start with a basic set up, and then cover basic VCL functions that you’ll be tweaking as you go.</p>
<h2>Step by Step</h2>
<p>Setting up Varnish is pretty simple. I’m going to assume you’re using Apache on a Debian-based system. It does work <strong>on other systems too</strong> though.</p>
<p>Start in the command line with this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> varnish</pre></div></div>

<p>First, you want to configure Apache to listen on port 8080 of the localhost interface. Varnish can then listen on port 80 (Where your visitors connect). In /etc/apache2/ports.conf, edit the following settings:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">NameVirtualHost 127.0.0.1:<span style="color: #000000;">8080</span>
Listen 127.0.0.1:<span style="color: #000000;">8080</span></pre></div></div>

<p>To get Varnish to start (it won’t by default), edit the following in /etc/default/varnish</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">START</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span>
<span style="color: #007800;">DAEMON_OPTS</span>=”-a EXTERNAL_IP_ADDRESS:<span style="color: #000000;">80</span> \
	<span style="color: #660033;">-T</span> localhost:<span style="color: #000000;">6082</span> \
	<span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>varnish<span style="color: #000000; font-weight: bold;">/</span>default.vcl \
	<span style="color: #660033;">-S</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>varnish<span style="color: #000000; font-weight: bold;">/</span>secret \
	<span style="color: #660033;">-s</span> <span style="color: #c20cb9; font-weight: bold;">file</span>,<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>varnish<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$INSTANCE</span><span style="color: #000000; font-weight: bold;">/</span>varnish_storage.bin,1G”</pre></div></div>

<p>Replace EXTERNAL_IP_ADDRESS with the IP of your external IP address. It can also be an internal address if your server is setup behind a load balancer, or something like NGINX. This setting controls what IP address and port you want Varnish to bind to and listen on.</p>
<p>Now edit /etc/varnish/default.vcl, which should already exist with lots of it commented out. Start by changing the default backend.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">backend default <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	.host = “127.0.0.1”;<span style="color: #000000; font-weight: bold;">&lt;/</span>p<span style="color: #000000; font-weight: bold;">&gt;</span>
	.port = “<span style="color: #000000;">8080</span>”;<span style="color: #000000; font-weight: bold;">&lt;/</span>p<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Now Varnish knows that Apache is listening on port 8080 of the localhost interface, and you can start using the functions. The majority of the work will be <strong>with vcl_recv and vcl_fetch</strong>, and if you don’t call an action in this subroutine and Varnish reaches the end, it will execute the built-in code from <em>default.vcl.</p>
<p>Note: you <em>never</em> want to cache wp_admin, wp_login, or similar pages.</p>
<p>Here’s how it works &#8211; the 4 basic subroutines in your Varnish config which you need to handle requests are:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sub vcl_recv</pre></div></div>

<p>This is called at the beginning of a request and tells varnish what to do with that particular request: whether or not to serve it, how to serve it, and which backend to use. </p>
<p>Varnish receives a request from your browser, <em>vcl_recv</em> and chooses to do one of 3 things with it: <em>vcl_hash, vcl_pass, and vcl_pipe</em> (More details on those in a minute!). You can change the request if you like, altering cookies or removing the request header.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sub vcl_fetch</pre></div></div>

<p>vcl_fetch is called after a document has been successfully retrieved from the backend. Use this to alter the response headers, trigger ESI processing, or try alternate backend servers if the request failed.</p>
<p>The request object, <em>req</em>, is still available, and there is also a backend response, <em>beresp</em>, which contains the HTTP headers from the backend.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sub vcl_hash</pre></div></div>

<p>You can call hash_data on the data you want to add to the hash. This subroutine may terminate with calling return() with one of the keywords, <em>hash</em> or <em>proceed</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">sub vcl_deliver</pre></div></div>

<p>Call this before a cached object is delivered to the client. This may terminate with <em>deliver, error code, or restart.</em> Deliver, delivers the object to the client. Error will return the specified error code to the client and abandon the request. Restart will restart the transaction and increase the restart counter. If the number of restarts exceed the <em>max_restarts</em>, varnish emits a guru meditation error.</p>
<h2>Actions</h2>
<p>There are a number of actions you can take inside each subroutine as you customize Varnish:</p>
<p style="padding-left: 30px;"><em>pass</em></p>
<p style="padding-left: 60px;">Pass the request and subsequent response to and from the backend server, not cached. pass can be called in both <em>vcl_recv</em> and <em>vcl_fetch.</em></p>
<p style="padding-left: 30px;"><em>lookup</em></p>
<p style="padding-left: 60px;">Called from vcl_recv to deliver content from cache even if the request indicates that the request should be passed. You can’t call lookup from <em>vcl_fetch.</em></p>
<p style="padding-left: 30px;"><em>pipe</em></p>
<p style="padding-left: 60px;"><em></em>From <em>vcl_recv</em>, pipe short-circuits client and backend connections and Varnish will just sits there passing the data back and forth, logging the data, so logs will be incomplete. Beware that with HTTP 1.1 a client can send several requests on the same connection and so you should instruct Varnish to add a “Connection: close” header before actually calling pipe.</p>
<p style="padding-left: 30px;"><em>deliver</em></p>
<p style="padding-left: 60px;"><em></em>Deliver the cached object to the client. Usually called in vcl_fetch.</p>
<p style="padding-left: 30px;"><em>esi</em></p>
<p style="padding-left: 60px;"><em></em>ESI-process the fetched document.</p>
<p>The Varnish community has a very <a href="https://www.varnish-cache.org/docs/2.1/tutorial/index.html">detailed tutorial on using VCL</a>, and the functions that you can perform on your site, which I won’t go into here. </p>
<h2>Example Configurations</h2>
<p>Hopefully this post has given you a good introduction to Varnish, but the best way to truly start playing with it is to see some sample configuration files.</p>
<p>The Varnish website has a great collection <a href="https://www.varnish-cache.org/trac/wiki/VCLExamples">sample configurations</a>, which can make a perfect starting point for building your own.</p>
<p>More specifically, Mattias Geniar has put up sample <a href="https://github.com/mattiasgeniar/varnish-3.0-configuration-templates/blob/master/conf.d/_wordpress-fetch.vcl">fetch</a> and <a href="https://github.com/mattiasgeniar/varnish-3.0-configuration-templates/blob/master/conf.d/_wordpress-receive.vcl">receive</a> configs for WordPress on Github.</p>
<p>Suffice to say that Varnish is very customizable and can do wonders for WordPress. A great example would be a Membership site. You can customize Varnish to <strong>cache content based on a user’s permissions</strong>, on their membership level, or even if they’re just visiting your site. </p>
<p>Facebook uses Varnish to great avail, and no doubt they have customized it to hell and back, but it goes to show you how efficient and flexible Varnish is.</p>
<h2>Plugins</h2>
<p><strong>Update (13 Apr 2012):</strong> Thanks to <a href="http://blog.tenya.me/">Alexander</a> and <a href="http://tinywp.in/">Pothi</a> for letting us know about an <a href="https://github.com/pkhamre/wp-varnish">updated Varnish plugin</a> for WP 3.0+ and one on WP.org also for <a href="http://wordpress.org/extend/plugins/varnish-http-purge/">purging your cache</a> (Also, check out both of these guy&#8217;s blogs for more Varnish and WP performance tips!)</p>
<p>The <a href="http://wordpress.org/extend/plugins/wordpress-varnish/">varnish plugin</a> will purge the cache when content is added or edited, and it works for multisite. The plugin does include a sample config file for your Varnish backend, however, keep in mind that when you’ve customized it for one particular site, those customizations may or may not play nicely with a different site.</p>
<p>Have you had any luck installing Varnish for your WordPress? What works and doesn&#8217;t? Feel free to share some of your ideas in the comments!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/d3hIaLEOOV8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/how-to-install-and-customize-varnish-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/how-to-install-and-customize-varnish-for-wordpress/</feedburner:origLink></item>
		<item>
		<title>6 Ways to Power Up WordPress Comments</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/TIWY4d0zWi8/</link>
		<comments>http://www.problogdesign.com/wordpress/6-ways-to-power-up-wordpress-comments/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 16:00:49 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Disqus]]></category>
		<category><![CDATA[Jetpack]]></category>
		<category><![CDATA[Livefyre]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8709</guid>
		<description><![CDATA[<img class="alignnone size-full wp-image-8721" title="Extend WordPress Comments" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/comments.jpg" alt="Extend WordPress Comments" width="629" height="180" />

It's a relatively simple process to add some flair to your blog comments these days. I want to look at 6 of the best free tools and give you a quick overview of the benefits of each.

Let's start with one you've likely heard of before...]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8721" title="Extend WordPress Comments" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/comments.jpg" alt="Extend WordPress Comments" width="629" height="180" /></p>
<p>It&#8217;s a relatively simple process to add some flair to your blog comments these days. I want to look at 6 of the best free tools and give you a quick overview of the benefits of each.</p>
<p>Let&#8217;s start with one you&#8217;ve likely heard of before&#8230;</p>
<h2>Disqus</h2>
<p><a href="http://disqus.com/"><img class="alignnone size-full wp-image-8711" title="Disqus" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/disqus.jpg" alt="Disqus" width="629" height="180" /></a></p>
<p>Disqus uses JavaScript to replace your WordPress comments with their own system. You lose the total design flexibility that vanilla WordPress comments offer, but you gain a large feature set, including:</p>
<ul>
<li><strong>Real-time Comments</strong> &#8211; New comments appear on the page without needing to refresh. Not a major benefit for smaller sites, but very good if your posts tend to get a flurry of comments in the first few hours of publishing.</li>
<li><strong>Social Media Integration</strong> &#8211; Users can comment via Facebook or Twitter, and share to those sites as well if they choose.</li>
<li><strong>Global Profiles</strong> &#8211; A user&#8217;s Disqus profile shows their comments across all Disqus sites. The idea is that a Disqus user will be more likely to comment on a Disqus blog because of this, and it makes for a fast way to find out more about a commenter.</li>
<li>Plenty of other features, like inline comment replies or &#8220;<strong>subscribe to updates</strong>.&#8221;</li>
</ul>
<p>Another good thing to bear in mind with Disqus is that they are very active. Disqus has been around for several years now, and it still innovates and updates regularly. They even have a mystery <a href="http://blog.disqus.com/post/19693554817/disqus-labs">Disqus 2012</a> project to be released soon.</p>
<p>And incase you&#8217;re wondering, your comments can all be imported to Disqus, and <strong>exported back to WordPress if you choose</strong> to leave Disqus. You won&#8217;t be hostage to their system.</p>
<p><a href="http://disqus.com/admin/wordpress/">WordPress Install Instructions for Disqus</a></p>
<h2>Livefyre</h2>
<p><a href="http://www.livefyre.com/"><img class="alignnone size-full wp-image-8712" title="Livefyre" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/livefyre.jpg" alt="Livefyre" width="629" height="180" /></a></p>
<p>Livefyre is very similar to Disqus, and as far as I can tell, they offer almost exactly the same <a href="http://www.livefyre.com/features/">feature set</a>. <strong>Real-time comments, social media, user profiles, subscription</strong> etc. are all included as standard.</p>
<p>Livefyre is a newer contender in this market though, but personally I think they have an edge over Disqus in 2 ways:</p>
<ul>
<li><strong>Simpler Installation</strong> &#8211; Disqus isn&#8217;t hard to install, but it does have JavaScript issues often enough (For anyone who has ever used it, hands up if your comment counts vanished?). I haven&#8217;t used Livefyre on as many sites yet, but it hasn&#8217;t given me any hassle.</li>
<li><strong>Sleeker Design</strong> &#8211; This is just a matter of taste, but I like the softer design of Livefyre&#8217;s comments more. It&#8217;s not a big difference, but every little helps, and even their website has a better design.</li>
</ul>
<p><a href="http://support.livefyre.com/customer/portal/articles/163187-how-do-i-install-livefyre-on-my-wordpress-blog-">WordPress Install Instructions for Livefyre</a></p>
<h2>IntenseDebate</h2>
<p><a href="http://www.intensedebate.com/"><img class="alignnone size-full wp-image-8713" title="Intense Debate" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/intense-debate.jpg" alt="Intense Debate" width="629" height="180" /></a></p>
<p>IntenseDebate is similar again to the 2 above (The last tool in this style, I promise!). The reason I mention it is because it has one very big draw for WordPress users; <strong>it&#8217;s owned by Automattic</strong>, the creators of WordPress.</p>
<p>The <a href="http://www.intensedebate.com/features">feature-set</a> is generally quite similar to the others (Though real-time commenting is noticeably absent), and their design is very similar as well.</p>
<p>Whilst there is nothing that I can say is &#8220;wrong&#8221; with IntenseDebate, it&#8217;s hard to recommend them overly either. Their site and script don&#8217;t feel as sleek as Livefyre, and their community no longer seems as vibrant as Disqus&#8217;.</p>
<p>Oh, and the bright green &#8220;180p&#8221; badges look awful&#8230; (Look down the sidebar of <a href="http://blog.intensedebate.com/">their blog</a> to see what I mean).</p>
<p><a href="http://intensedebate.com/install">Begin IntenseDebate Install Process</a></p>
<h2>Facebook Comments</h2>
<p><a href="https://developers.facebook.com/docs/reference/plugins/comments/"><img class="alignnone size-full wp-image-8715" title="Facebook Comments" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/facebook.jpg" alt="Facebook Comments" width="629" height="180" /></a></p>
<p>The Facebook Comments script is exactly what you would expect; it replaces your own comments with a Facebook wall. Users must be signed in to their Facebook accounts, and then what they see is <strong>identical to Facebook.com</strong>.</p>
<p>There are two big advantages to this approach:</p>
<ul>
<li><strong>Publicity</strong> &#8211; Users are commenting with their Facebook profiles, so with a little luck, their friends will see the comment and look at your article as well.</li>
<li><strong>Anti-spam</strong> &#8211; Every commenter must use their Facebook account, so spammers have a much harder time bulk-spamming blogs.</li>
</ul>
<p>The strongest feature is also the strongest drawback. Some people (like me) keep their accounts completely separate to their work or online profiles, and so, you will <strong>miss out on those people&#8217;s contributions</strong>.</p>
<p>There is a <a href="http://wordpress.org/extend/plugins/facebook-comments-for-wordpress/">plugin</a> for adding these comments to WordPress, but it seems to be broken with WordPress 3.3. For a developer though, the installation is similar to any other Facebook script. You can <a href="https://developers.facebook.com/docs/reference/plugins/comments/">get the code here</a>.</p>
<p>(If you have trouble with the install or can&#8217;t find a reliable guide online, <strong>let me know and I&#8217;ll put together a walkthrough</strong>).</p>
<h2>Jetpack</h2>
<p><a href="http://jetpack.me/"><img class="alignnone size-full wp-image-8718" title="Jetpack" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/jetpack.jpg" alt="Jetpack" width="629" height="180" /></a></p>
<p>Jetpack is essentially a set of WordPress plugins, which is a topic I&#8217;m about to cover in the final section. What sets Jetpack apart is its ease of use. You install one package, and <strong>cherry pick the features for you want</strong>.</p>
<p>In the case of commenting, that means 3 things:</p>
<ul>
<li><strong>Subscriptions</strong> &#8211; Users can be emailed when follow-up comments are left after their reply. This is a great way to keep conversations going.</li>
<li><strong>Gravatar Hovercards</strong> &#8211; When you mouse over a commenter&#8217;s name, this will pull up a little bubble with their image and a quick bio with a link to their full profile on <a href="http://en.gravatar.com/">Gravatar</a>.</li>
<li><strong>Sharing</strong> &#8211; Jetpack makes it easy to add social media sharing links anywhere on your page, including around the comments area.</li>
</ul>
<div>One tool is then enough to take your existing theme&#8217;s comments, and upgrade them with some of the most popular commenting functions.</div>
<p><a href="http://wordpress.org/extend/plugins/jetpack/installation/">Install Guide for WordPress Jetpack</a></p>
<h2>Other WordPress Plugins</h2>
<p><a href="http://wordpress.org/extend/plugins/tags/comments"><img class="alignnone size-full wp-image-8719" title="WordPress Comment Plugins" src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/wordpress-plugins.jpg" alt="WordPress Comment Plugins" width="629" height="180" /></a></p>
<p>There are hundreds out there, but I want to focus on just 4 of them:</p>
<ul>
<li><a href="http://wordpress.org/extend/plugins/wp-ajax-edit-comments/">AJAX Edit Comments </a>- This allows users to edit their comments for up to 15 minutes after submitting them. It is a good way to enable users to fix typos, and avoid &#8220;double-posting&#8221; if they want to add another line. It is particularly popular on coding blogs, where awesome commenters might contribute code snippets of their own but find they get messed up when displayed.</li>
<li><a href="http://wordpress.org/extend/plugins/commentluv/">CommentLuv</a> &#8211; CommentLuv gets the latest blog post from a commenter&#8217;s RSS feed, and automatically adds a link to it after their comment. This is a great way to thank visitors for taking the time to post a comment.</li>
<li><a href="http://www.gdstarrating.com/">GD Star Rating</a> &#8211; Not the simplest of plugins to configure, but GD Star Rating has a wealth of options that can enable star ratings (Or thumbs) on WordPress comments (And posts).</li>
<li><a href="http://wordpress.org/extend/plugins/wp-recaptcha/">WordPress reCAPTCHA</a> - Spam can be a nightmare on WordPress blogs, but this plugin makes it simple to add Google&#8217;s captcha (&#8220;Type the letters&#8230;&#8221;) tool to your blog.</li>
</ul>
<h2>Which Do You Use?</h2>
<p>I&#8217;ve always used regular WordPress comments with some plugins here on Pro Blog Design, because I like the <strong>flexibility of creating your own comments design</strong>. The topic of this blog is another reason; it&#8217;s good for us to show something unique.</p>
<p>More and more though, I&#8217;m working with clients who are choosing the scripts above (Particularly Disqus and Livefyre). These scripts are very impressive, and can even be a good way to <strong>speed up the development time</strong> of a new site.</p>
<p>I think the Facebook widget is only a great bet if you specifically try to encourage users to interact with you via Facebook (e.g. your site takes signups via Facebook Connect, or you need as much social sharing of your app as possible etc.).</p>
<p>I&#8217;d love to hear what approach you&#8217;ve taken on your site, <strong>and why you went down that road!</strong></p>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/TIWY4d0zWi8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/6-ways-to-power-up-wordpress-comments/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/6-ways-to-power-up-wordpress-comments/</feedburner:origLink></item>
		<item>
		<title>Add a Backup to Embedded Tweets in WordPress</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/aHh3sEpFnaQ/</link>
		<comments>http://www.problogdesign.com/wordpress/add-a-backup-to-embedded-tweets-in-wordpress/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 18:18:49 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Transients]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8687</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/twitter-backup.jpg" alt="Tweets in WordPress with Backups" title="Tweets in WordPress with Backups" width="629" height="180" class="alignnone size-full wp-image-8699" />

Around this time last year, I showed you how to <a href="http://www.problogdesign.com/wordpress/how-to-use-the-twitter-api-in-wordpress/">embed tweets in WordPress</a> using the Twitter API.

Today, I want to improve on that script by adding a backup to it. <strong>Twitter is often down or unresponsive</strong>, and when it is, the previous script fails and displays nothing. 

With a backup, we store an extra copy of each set of tweets that we find. When Twitter is down, we can then use this backup <strong>to display tweets as normal</strong>.]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2012/03/twitter-backup.jpg" alt="Tweets in WordPress with Backups" title="Tweets in WordPress with Backups" width="629" height="180" class="alignnone size-full wp-image-8699" /></p>
<p>Around this time last year, I showed you how to <a href="http://www.problogdesign.com/wordpress/how-to-use-the-twitter-api-in-wordpress/">embed tweets in WordPress</a> using the Twitter API.</p>
<p>Today, I want to improve on that script by adding a backup to it. <strong>Twitter is often down or unresponsive</strong>, and when it is, the previous script fails and displays nothing. </p>
<p>With a backup, we store an extra copy of each set of tweets that we find. When Twitter is down, we can then use this backup <strong>to display tweets as normal</strong>.</p>
<h2>WordPress &#8220;Transients&#8221; vs &#8220;Options&#8221;</h2>
<p>We will use both of these, so it&#8217;s best to explain quickly what they are. An &#8220;option&#8221; is a value that is <strong>stored permanently</strong> in your database. There is an option for your site title, the slogan etc.</p>
<p>A transient is very similar to an option, but it also has <strong>an expiry time</strong>. After this time, the transient is deleted. For more information, read <a href="http://www.problogdesign.com/wordpress/use-the-transients-api-to-list-the-latest-commenter/">using the Transients API</a>.</p>
<p>The transient is perfect for <strong>caching tweets</strong>. We can store the results from Twitter, and when the transient expires, fetch new ones. </p>
<p>The option however is perfect for the backup, because we <strong>never want it to expire</strong> (We will just write over it each time we update it).</p>
<h2>The Code</h2>
<p>The majority of this code is from the previous article. For that reason, I will only explain the new additions. To understand how we process each tweet and display it, check out <a href="http://www.problogdesign.com/wordpress/how-to-use-the-twitter-api-in-wordpress/">the original post</a>.</p>
<p>And if you&#8217;d rather just skip on, you can grab the fully <a href="http://www.problogdesign.com/uploads/other/embed-tweets.phps" rel="nofollow">completed code here</a>. </p>
<p>Find where in your theme you would <strong>like the tweets to appear</strong> (e.g. sidebar.php), and paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
 * JSON list of tweets using:
 *         http://dev.twitter.com/doc/get/statuses/user_timeline
 * Cached using WP transient API.
 *        http://www.problogdesign.com/wordpress/add-a-backup-to-embedded-tweets-in-wordpress/
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Configuration.</span>
<span style="color: #000088;">$numTweets</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'problogdesign'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cacheTime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Time in minutes between updates.</span>
<span style="color: #000088;">$exclude_replies</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Leave out @replies?</span>
&nbsp;
<span style="color: #000088;">$transName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'list-tweets'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Name of value in database.</span>
<span style="color: #000088;">$backupName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$transName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-backup'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Name of backup value in database.</span></pre></td></tr></table></div>

<p>These are your <strong>configuration options</strong>. How many tweets do you want to display? From which user? And how often should we fetch new updates? (It&#8217;s fine to go to 1 minute even, but it means one of your users will have a slow page load every minute, rather than one every 5 minutes).</p>
<p>The only change from before is that I&#8217;ve added the option to exclude @replies from your results, and we now have 2 names for values in the database. Let&#8217;s carry on.</p>
<p>The next section of code is the bulk of the script. Essentially, it <strong>fetches the data from Twitter&#8217;s API</strong>, picks out the parts we want, formats them, and saves them as an array called $tweets. The <a href="http://www.problogdesign.com/wordpress/how-to-use-the-twitter-api-in-wordpress/">original post</a> explains it all one step at a time.</p>
<p>You can also read about this specific <a href="https://dev.twitter.com/docs/api/1/get/statuses/user_timeline">Twitter API call</a> here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Do we already have saved tweet data? If not, lets get it.</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> get_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$transName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>    
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the tweets from Twitter.</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> wp_remote_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://api.twitter.com/1/statuses/user_timeline.json?screen_name=<span style="color: #006699; font-weight: bold;">$name</span>&amp;count=<span style="color: #006699; font-weight: bold;">$numTweets</span>&amp;exclude_replies=<span style="color: #006699; font-weight: bold;">$exclude_replies</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// If we didn't find tweets, use the previously stored values.</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>is_wp_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'response'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
        <span style="color: #666666; font-style: italic;">// Get tweets into an array.</span>
        <span style="color: #000088;">$tweets_json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Now update the array to store just what we need.</span>
        <span style="color: #666666; font-style: italic;">// (Done here instead of PHP doing this for every page load)</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets_json</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
            <span style="color: #666666; font-style: italic;">// Core info.</span>
            <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$permalink</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://twitter.com/#!/'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'/status/'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id_str'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">/* Alternative image sizes method: http://dev.twitter.com/doc/get/users/profile_image/:screen_name */</span>
            <span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'profile_image_url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Message. Convert links to real links.</span>
            <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/http:(\S)+/'</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;a href=&quot;${0}&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;${0}&lt;/a&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Need to get time in Unix format.</span>
            <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'created_at'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date_parse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$uTime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'hour'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'minute'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'second'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'month'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'day'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'year'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Now make the new array.</span>
            <span style="color: #000088;">$tweets</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                            <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'permalink'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$permalink</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'time'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$uTime</span>
                            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Save our new transient, and update the backup.</span>
        set_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$transName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweets</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$cacheTime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        update_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$backupName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweets</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The change to notice comes on line 8. Here, we check if an error occurred or Twitter failed to give us back our tweets. We check the latter by <strong>looking at the HTTP status code returned</strong>. You can read more about how Twitter uses <a href="https://dev.twitter.com/docs/error-codes-responses">status codes here</a>, but the short of it is that anything other than 200 means something went wrong.</p>
<p>We only process the page returned if it passes these two checks.</p>
<p>The other change is on the very last line, where we save the transient as normal, but <strong>also save the permanent &#8220;option&#8221;</strong>. This updates our backup with the most recent set of results.</p>
<p>Moving on, all we need to do if the fetch fails is <strong>populate the $tweets array</strong> with the value from our permanent backup.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// i.e. Fetching new tweets failed.</span>
    	<span style="color: #000088;">$tweets</span> <span style="color: #339933;">=</span> get_option<span style="color: #009900;">&#40;</span><span style="color: #000088;">$backupName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// False if there has never been data saved.</span>
	<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we can <strong>display the tweets</strong> as before. To check that we do have tweets to display, we check if $tweets is false (If a backup has never been made, get_option() will have returned false to it).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">// Now display the tweets, if we can.
if($tweets) : ?&gt;
    &lt;ul id=&quot;tweets&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweets</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;li&gt;
            &lt;img src=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; width=&quot;48&quot; height=&quot;48&quot; alt=&quot;&quot; /&gt;                
            &lt;div class=&quot;tweet-inner&quot;&gt;
                &lt;p&gt;
                     <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">': '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
                     &lt;span class=&quot;tweet-time&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> human_time_diff<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> current_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> ago&lt;/span&gt;
                &lt;/p&gt;
            &lt;/div&gt;&lt;!-- /tweet-inner --&gt;
        &lt;/li&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/ul&gt;
	&lt;p&gt;&lt;a href=&quot;http://twitter.com/#!/<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;[ Follow us on Twitter ]&lt;/a&gt;&lt;/p&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;p&gt;No tweets found.&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>At the end, I&#8217;ve added a simple <strong>error message</strong>, rather than displaying nothing. This will only appear if fetching tweets fails, and a backup has never been made.</p>
<p>And that&#8217;s it done. If you used the previous script, this can be slotted in over it and all you need to do is update <strong>your details in the configuration section</strong> at the top. Now your tweet list should be that bit more reliable.</p>
<p>The full code can be found <a href="http://www.problogdesign.com/uploads/other/embed-tweets.phps" rel="nofollow">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/aHh3sEpFnaQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/add-a-backup-to-embedded-tweets-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/add-a-backup-to-embedded-tweets-in-wordpress/</feedburner:origLink></item>
		<item>
		<title>Lessons Learned Running a Small Business in 2011</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/29DgISscjJQ/</link>
		<comments>http://www.problogdesign.com/business/lessons-learned-running-a-small-business-in-2011/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 20:23:22 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Freelance]]></category>
		<category><![CDATA[Pro Blog Design]]></category>
		<category><![CDATA[Productivity]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8641</guid>
		<description><![CDATA[<img class="alignnone size-full wp-image-8661" title="Small Business Working" src="http://cdn.problogdesign.com/wp-content/uploads/2012/01/starbucks-laptop.jpg" alt="Small Business Working" width="629" height="180" />

As 2012 begins, I want to look back over the past year to see <strong>what can be learnt from it</strong>. Learning from your mistakes is less glamorous than a list of grand New Year's Resolutions (Though I do like those too), but often more effective at ensuring the next year is even better than the last.

With that in mind, what follows are some of the lessons I have picked out from my own 2011. I would love to hear if these resonate with you.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8661" title="Small Business Working" src="http://cdn.problogdesign.com/wp-content/uploads/2012/01/starbucks-laptop.jpg" alt="Small Business Working" width="629" height="180" /></p>
<p>As 2012 begins, I want to look back over the past year to see <strong>what can be learnt from it</strong>. Learning from your mistakes is less glamorous than a list of grand New Year&#8217;s Resolutions (Though I do like those too), but often more effective at ensuring the next year is even better than the last.</p>
<p>With that in mind, what follows are some of the lessons I have picked out from my own 2011. I would love to hear if these resonate with you.</p>
<p>When looking back at your own year, I&#8217;d encourage you not to look for when things went &#8220;wrong&#8221; as such, but simply to think of <strong>how things may have gone differently</strong>. That&#8217;s what I&#8217;ve done. If I were to go back, I may well do some of these things the same, but considering the alternatives is a great way to learn.</p>
<h3>Plan For the Follow-Through</h3>
<p>When working on a large project, launch day is the natural target. <strong>All of your work is dedicated to releasing</strong> the product.</p>
<p>That is obviously crucial, but equally important is planning in advance to <strong>build on the momentum from your big release</strong>. That could mean anything from analyzing its success, planning the next release, or simply capitalizing on the good PR.</p>
<p>I learned this lesson from the relaunch of this site in May. We had been working on it on and off for almost a year, so it had been a huge project, but in the push to launch, we built up a backlog of other work.</p>
<p>I worked on this and wrote a number of articles following the launch, but not enough. The backlog took priority because <strong>as far as schedules were concerned</strong>, the relaunch project was complete. The follow-through should have been a clearly defined of the project.</p>
<h3>Short-Term is More Tempting than Long-Term</h3>
<p>How many times have you heard a <strong>freelancer say they can&#8217;t find the time</strong> to work on their site or a personal project? Even though they really enjoy it?</p>
<p>All freelancers hit this issue, and I was no different. I think the reason for this is extremely simple; <strong>short term benefits have a clearer and more obvious benefit than long term ones</strong>. In my case, <a href="http://www.pliablepress.com">PliablePress</a> has been hurt by this. Improving the framework brings me incremental benefits over time, but taking on a large client project pays off right away.</p>
<p>The trick here I believe is a balancing act. If you spend all of your time on short-term goals, you&#8217;ll never get past where you are right now. But similarly, if you only work on long-term plans, you may not survive that long. <strong>Don&#8217;t let one overcrowd the other.</strong></p>
<h3>Focus Beats Multitasking</h3>
<p>This is a common enough productivity tip that you have likely heard before. <strong>Only work on one thing at a time</strong>. It makes sense, and it will come as no surprise to you that closing down Twitter and Skype etc. will help you work better.</p>
<p>The bigger lesson is to organize yourself each day so you know which projects you will be working on. You don&#8217;t need a step-by-step breakdown of the tasks (Though some people swear by that), a simple <strong>list of the top x projects</strong> that need your attention today will do.</p>
<p>This is something I see in my work again and again. The days in which I have a clear list of projects to work on, and I tackle them one by one, are my most productive days. Sometimes you can even <strong>spend a whole day on just one project</strong>, which is fantastic. Focus only on the next task, not all of the tasks.</p>
<p><em>Sidenote</em> &#8211; When making your list, try to avoid adding any murky tasks that don&#8217;t have clear completion objectives, e.g. research can go on indefinitely so before you start, make sure you&#8217;re aware of <strong>how you will know when you&#8217;re done</strong>.</p>
<h3>New Does Not Necessarily Equal Worthwhile</h3>
<p>In the tech world, we all love the speed at which things change. The <strong>impulse is to try as many new products</strong> and services as we can, because who knows what the next game changer will be?</p>
<p>And therein lies the problem. It could be anything, and there are new things coming at you every day. You can&#8217;t try them all, and the attempt is a waste of your time. I think a better approach is to let the community try them out for you, and <strong>join in when a winner seems to be emerging</strong>.</p>
<p><img class="alignright size-full wp-image-8669" title="Quora" src="http://cdn.problogdesign.com/wp-content/uploads/2012/01/quora.jpg" alt="Quora is a great Q&amp;A style site." width="210" height="94" />A good example of this is social networks. There are a million different variations out there and new ones all the time. Most won&#8217;t catch on, and the time put into them is lost to you. I joined Quora after consistently reading good responses on it for months, but I&#8217;m not on Pinterest, Path or any of the other current &#8220;hot&#8221; networks yet. I&#8217;ll <strong>check back again in a few months</strong>.</p>
<p>The <strong>exception is tools that really add value</strong> to your business (Webapps you use, development software etc.). Again, I wouldn&#8217;t try every single one as it comes out, but every few months, take a look around. It&#8217;s fun too, playing with new toys is hard to count as work!</p>
<h3>Join The Communities You Value</h3>
<p>The web is full of little communities, and you could take part in them from morning to night. On a productivity note, <strong>you might try to cut down on this</strong> when you have deadlines to meet.</p>
<p>It&#8217;s possible to go too far with this though. Once you&#8217;ve read a lot on a particular site, and especially after you&#8217;ve started enjoying the comment discussions, you can <strong>get a lot more out of taking part as well</strong>.</p>
<p><img class="alignright size-full wp-image-8673" title="Hacker News &amp; Stack Overflow" src="http://cdn.problogdesign.com/wp-content/uploads/2012/01/so-hn2.jpg" alt="Hacker News &amp; Stack Overflow" width="210" height="84" />I used to post thousands of comments in forums when I was younger, and then again on blogs as I started Pro Blog Design. That&#8217;s been somewhat lessened in 2011, so I&#8217;m looking forward to starting again. In particular, I&#8217;ve massively enjoyed reading <a href="http://news.ycombinator.com/">Hacker News</a> and <a href="http://stackoverflow.com/">Stack Overflow</a>, so it&#8217;s <strong>about time I joined up</strong>.</p>
<h3>Planning is Both Impossible and Essential</h3>
<p>A slight paradox, but it&#8217;s true. It&#8217;s <strong>impossible to plan for everything</strong> that may happen, even over just a short period of time. But with no plan, things inevitably fall into chaos.</p>
<p>I like to think I&#8217;m quite organized, and I always have a good idea of <strong>how the next 3-4 weeks will flow</strong>. I don&#8217;t think any of my &#8220;ideas&#8221; have ever gone 100% to plan though.</p>
<p><strong>Things change</strong>. Something will crash, a client will take longer with their feedback, an awesome new project will come up etc.</p>
<p>You need to plan enough to help things run smoothly, but be <strong>flexible enough to cope with</strong> the inevitable curve balls that come at you. I&#8217;m still not sure of the exact formula for this, but it seems that the more experience you have of it, the better your plans and estimates become.</p>
<h3>Fight to Work On What You Love</h3>
<p>When you run a small business, you&#8217;re responsible for everything. I have the pleasure of working with a number of other people throughout the year here, but no matter what, the <strong>final responsibility is still mine</strong>.</p>
<p>In practice, what this means is that the tasks you can&#8217;t easily find someone else to take care of, <strong>you do yourself</strong>.</p>
<p>That&#8217;s fine in the short term, and often even beneficial (Knowing you have to submit your accounts yourself keeps your finances very organized throughout the year!), but it <strong>isn&#8217;t a long term solution</strong>.</p>
<p>You didn&#8217;t start a business to work on things you don&#8217;t enjoy. There will always be some jobs you just have to do, but <strong>when the ones you don&#8217;t enjoy are the majority</strong>, you need to fix it.</p>
<p>I&#8217;m quite happy with where I am in regards to this now; the trick for me was <strong>angling towards certain types of projects</strong>. Great clients with interesting projects means that more or less everything is fun!</p>
<h3>The Truest Test of a Business is When You Leave It</h3>
<p><img class="alignright size-full wp-image-8677" title="Not that I didn't still enjoy the break!" src="http://cdn.problogdesign.com/wp-content/uploads/2012/01/washington.jpg" alt="Not that I didn't still enjoy the break!" width="210" height="210" />Again, this is something you&#8217;ve likely read before, and it&#8217;s spot on. In September, I was away for around 3 weeks, and it likely affected every part of the business.</p>
<p>I planned in advance as much as I could, but <strong>every change has a knock-on effect</strong>. By prioritizing the projects that could most likely be finished before I left, I was leaving others to be done largely after I returned. I was also lining up new projects to be worked on while I was away, but I was still needed at various points to give my thoughts.</p>
<p>In other words, the month before and the month after the trip were crazy. There is no easy fix to this, but <strong>removing the dependency on you</strong> is definitely a big area to work on if you want your business to grow.</p>
<h3>Conclusion</h3>
<p>Those were the biggest new &#8220;lessons&#8221; I would take from my 2011. I&#8217;ve deliberately chosen from <strong>when things could have gone better</strong>, not from when they went great. I&#8217;d be happy to share those as well if you like, but learning from mistakes is often the more revealing analysis so they weren&#8217;t included here.</p>
<p>In return, I&#8217;d love to hear about your year. What have you learnt from it and <strong>what will you do differently this year?</strong></p>
<p>Before I finish, I want to apologize for how quiet the past months have been. They&#8217;ve been some of the busiest months I&#8217;ve ever had, but I&#8217;ve missed blogging greatly. January will be a quiet month as well, sorry, but after the 29th, I <strong>will</strong> be back with a vengeance. I&#8217;ve learnt a lot, so there&#8217;s a lot I want to share!</p>
<p>If you reached this far in the post, <strong>thank you</strong> for still being a reader after all this time.</p>
<p>Happy New Year everyone!</p>
<p><small>Main photo by <a href="http://www.flickr.com/photos/tsk/2480950146/">razvan.caliman</a>.</small></p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/29DgISscjJQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/business/lessons-learned-running-a-small-business-in-2011/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/business/lessons-learned-running-a-small-business-in-2011/</feedburner:origLink></item>
		<item>
		<title>How to Create WordPress Posts in Themes/Plugins</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/g3QuxMtK8KE/</link>
		<comments>http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 16:00:52 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Posts]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8543</guid>
		<description><![CDATA[<a href="http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/attachment/insert_post2/" rel="attachment wp-att-8558"><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/08/insert_post2.jpg" alt="User-Submitted Posts in WordPress" title="User-Submitted Posts in WordPress" width="629" height="180" class="alignnone size-full wp-image-8558" /></a>

Recently, one of our clients needed to let visitors create WordPress posts (Or custom post types of course) <strong>from the website itself</strong>.

In this post, I'm going to walk through some of the WordPress functions I used to achieve this. We'll go over creating the post itself, adding <strong>categories/tags</strong>, and adding <strong>metadata</strong>.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/attachment/insert_post2/" rel="attachment wp-att-8558"><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/08/insert_post2.jpg" alt="User-Submitted Posts in WordPress" title="User-Submitted Posts in WordPress" width="629" height="180" class="alignnone size-full wp-image-8558" /></a></p>
<p>Recently, one of our clients needed to let visitors create WordPress posts (Or custom post types of course) <strong>from the website itself</strong>.</p>
<p>In this post, I&#8217;m going to walk through some of the WordPress functions I used to achieve this. We&#8217;ll go over creating the post itself, adding <strong>categories/tags</strong>, and adding <strong>metadata</strong>.</p>
<p>(We&#8217;ll skip the stage where you build a form, validate what users enter etc. and <strong>just focus on the WordPress functions</strong> instead)</p>
<h2>Create the Post</h2>
<p>The method to create the core post is simple. Essentially, you <strong>build a $post object</strong>, and then insert it with <a href="http://codex.wordpress.org/Function_Reference/wp_insert_post">wp_insert_post()</a>.</p>
<p>You can use our <a href="http://www.problogdesign.com/wordpress/wordpress-post-term-and-comment-object-cheat-sheet/">$post reference</a> to speed this up. Let&#8217;s take a look at some examples:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That would create a new post, with the title &#8220;Our New Post&#8221;, and the filler text we&#8217;ve entered.</p>
<p>If wp_insert_post() is successful, it returns the <strong>ID of the new post it has created</strong> (Which we assign to $post_id here). If it fails, it will return 0.</p>
<p>By default, the <strong>status is set to &#8220;Draft&#8221;</strong>, so let&#8217;s tweak it to publish the post instantly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can set just about <strong>any value</strong> from the $post variable, e.g.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_author'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2011-09-01 15:10:30'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Our post is <strong>now a page</strong>, written by user #2, and <strong>scheduled</strong> to be published on the 1st of September.</p>
<h2>Categories, Tags and Custom Taxonomies</h2>
<p>Let&#8217;s move on to categories and tags. You can either set these <strong>as you make the rest of the post</strong>, or add them separately later. Let&#8217;s start by seeing how it is done in wp_insert_post(). </p>
<p>For categories, you pass in an <strong>array containing the IDs of the categories</strong> you want to add, e.g. for categories 9 and 29:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_category'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">29</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Tags are a little easier. Instead of passing in the IDs, you can use the <strong>names of the tags themselves</strong>, e.g.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'tags_input'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Filler Tag'</span><span style="color: #009900;">&#41;</span>	
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can even set the <strong>terms of a custom taxonomy</strong>. This uses the &#8220;tax_input&#8221; parameter and expect you to give it an array where each key is a custom taxonomy, and each value is an array of tags in that taxonomy.</p>
<p><em><strong>Update (8 Aug 2011)</strong>: <a href="http://www.rarst.net/">Rarst</a> has left a comment saying that &#8220;tax_input&#8221; will only work if initiated by a logged in user with the permission to edit that taxonomy. If this affects you, read on below for a different solution that will work anywhere!</em></p>
<p>For example, let&#8217;s say we have a <strong>taxonomy called &#8220;custom_tax&#8221;</strong>, and the two tags we want to add to the post are &#8220;Dolor&#8221; and &#8220;Ipsum&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$new_post</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'post_title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our New Post'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_content'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Our filler text for the post.'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'tax_input'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'custom_tax'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dolor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Ipsum'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span>	
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> wp_insert_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I mentioned that you could <strong>also add your tags later on</strong>. To do that, use the <a href="http://codex.wordpress.org/Function_Reference/wp_set_object_terms">wp_set_object_terms()</a> function (There are similar functions to this one, specifically for tags etc. but this covers it all for you).</p>
<p>The format is: wp_set_object_terms( post ID , array of tag IDs , taxonomy , append true/false ).</p>
<p>The last value is important. If you set true, then your new tags will be <strong>added to any existing ones</strong>. If you set false, then any existing tags will be <strong>replaced with your new ones</strong>.</p>
<p>Here are some examples of adding categories, tags, and a custom taxonomy this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">wp_set_object_terms<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">27</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">28</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">29</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
wp_set_object_terms<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Example'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Lorem Ipsum'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post_tag'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
wp_set_object_terms<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Lorem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_tax'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Metadata</h2>
<p>If you&#8217;ve ever added custom meta data to a post before, you know what to do already. </p>
<p>Let&#8217;s say we want to add a <strong>custom field with the name &#8220;summary&#8221;</strong> and the value &#8220;Quick Summary!&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_post_meta<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'summary'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Quick summary!'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s quite self-explanatory. The &#8216;true&#8217; at the end refers to <strong>whether this field is unique or not</strong>. If we set this to false, and a summary already exists, then another custom field also titled &#8216;summary&#8217; would be added to the post.</p>
<p>By setting it to true, this won&#8217;t happen. WordPress <strong>checks for the &#8216;summary&#8217; field first</strong>, and if it exists, it does nothing (You would then need to use <a href="http://codex.wordpress.org/Function_Reference/update_post_meta">update_post_meta()</a> instead).</p>
<p>One special note to make is that if you want to <strong>set a page template</strong>, you do this with the meta functions as well. WordPress stores the name of the template file to use in a custom field titled &#8216;_wp_page_template&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">update_post_meta<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_wp_page_template'</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'onecolumn-page.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Between those functions, you should be able to set up any post data you need. Feel free to post in the comments if you have any questions, or I&#8217;d love to hear how you&#8217;ve <strong>allowed users to add content to a WordPress site</strong>!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/g3QuxMtK8KE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/feed/</wfw:commentRss>
		<slash:comments>98</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/how-to-create-wordpress-posts-in-themesplugins/</feedburner:origLink></item>
		<item>
		<title>How to Make a Floating Share Box</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/lT-nGEaaesQ/</link>
		<comments>http://www.problogdesign.com/wordpress/how-to-make-a-floating-share-box/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 16:00:34 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Google +1]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Social]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8492</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/07/floating-share.jpg" alt="Floating Share Buttons" title="Floating Share Buttons" width="629" height="180" class="alignnone size-full wp-image-8493" />

This is one of the most common questions I get asked; how do you make a <strong>floating box with share links</strong>?

Today, we're going to see just how simple it can be. We will:

<ul>
	<li>Set up the current trinity of search (<strong>Facebook, Twitter, +1</strong>).</li>
	<li>See how to make sure they share <strong>the right URL</strong>.</li>
	<li>Align the box to the <strong>bottom left</strong> of the user's browser.</li>
	<li><strong>Hide</strong> the box if the user's browser is too small (So it never overlaps our content).</li>
</ul>

You can see the <a href="http://www.problogdesign.com/demo/floating-share/">demo site here</a> (Try resizing your browser smaller).]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/07/floating-share.jpg" alt="Floating Share Buttons" title="Floating Share Buttons" width="629" height="180" class="alignnone size-full wp-image-8493" /></p>
<p>This is one of the most common questions I get asked; how do you make a <strong>floating box with share links</strong>?</p>
<p>Today, we&#8217;re going to see just how simple it can be. We will:</p>
<ul>
<li>Set up the current trinity of search (<strong>Facebook, Twitter, +1</strong>).</li>
<li>See how to make sure they share <strong>the right URL</strong>.</li>
<li>Align the box to the <strong>bottom left</strong> of the user&#8217;s browser.</li>
<li><strong>Hide</strong> the box if the user&#8217;s browser is too small (So it never overlaps our content).</li>
</ul>
<p>You can see the <a href="http://www.problogdesign.com/demo/floating-share/">demo site here</a> (Try resizing your browser smaller).</p>
<h2>1 &#8211; The URL and Text to Share</h2>
<p>The first step involves a little WordPress code. In <em>footer.php</em> in your theme, scroll down to the </body> tag, and just before it, paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// URL to share</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> is_singular<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> is_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> is_tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$cat</span> <span style="color: #339933;">=</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> get_category_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cat</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$tag</span> <span style="color: #339933;">=</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> get_tag_link<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> single_cat_title<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' on '</span> <span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>	
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The point of this code is to work out the <strong>right URL and title text</strong> to share, and save them in 2 PHP variables ($url and $text).</p>
<p>The way it works is to run through a &#8220;what page are we on?&#8221; check:</p>
<ul>
<li><strong>Single Post/Page</strong> &#8211; Then use the post&#8217;s URL and title.</li>
<li><strong>Category/Tag</strong> &#8211; Then use the category/tag&#8217;s URL, and &#8220;Category on Site Name&#8221; as the text.</li>
<li><strong>Default</strong> &#8211; Share the homepage URL, site title and description on any other page.</li>
</ul>
<p>The reason we go to the trouble of making sure we have the right URL and text here is to ensure that the right details are shared, e.g. we don&#8217;t need any utm_source= etc. tracking tags after the address.</p>
<h2>Get the Share Codes</h2>
<p>The next step is to get the <strong>share code for each button</strong>, and put it in HTML that we can style.</p>
<p>You can either follow through the steps manually as I show you, or skip to the next block of code after this one, and copy it (But read the paragraph before it too).</p>
<p><em>Manual way:</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;social-float&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-twitter&quot;</span>&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- Twitter Code Goes Here --&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-facebook&quot;</span>&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- Facebook Code Goes Here --&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-plusone&quot;</span>&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- Google +1 Code Goes Here --&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- /social-float --&gt;</span></pre></td></tr></table></div>

<p>You can see HTML comments for <strong>where each share code will go</strong>. Now, let&#8217;s get those codes (And tweak them as needed)</p>
<ul>
<li><strong>Twitter</strong>
<ul>
<li><a href="http://twitter.com/about/resources/tweetbutton">Click here</a>.</li>
<li>Choose &#8220;Vertical Count&#8221;, and enter your Twitter handle below.</li>
<li>Copy and paste the code.</li>
<li>Find the part that looks like: <em>data-count=&#8221;vertical&#8221; data-via=&#8221;problogdesign&#8221;</em>. Take a space after that and paste: <em>data-url=&#8221;&lt;?php echo $url; ?&gt;&#8221; data-text=&#8221;&lt;?php echo $text; ?&gt;&#8221;</em></li>
</ul>
</li>
<li><strong>Facebook</strong>
<ul>
<li><a href="https://developers.facebook.com/docs/reference/plugins/like/">Click here</a>.</li>
<li>Enter <em>http://google.com</em> for &#8220;Address to Share&#8221;.</li>
<li>Deselect the &#8220;Send Button&#8221; option, choose &#8220;box_count&#8221; for layout, and set the width to 50.</li>
<li>Hit &#8220;Get Code&#8221; and copy and paste the iframe code (Or XFBML if you already use the FB JavaScript SDK. If you aren&#8217;t sure, just take the iframe code).</li>
<li>In the iframe code, search for <em>http%3A%2F%2Fgoogle.com</em>, and replace it with: <em>&lt;?php echo urlencode($url); ?&gt;</em></li>
<li>Search for <em>80</em> in the code, and replace it with <em>62</em> (There are 2 places to change it, both to do with the height).</li>
</ul>
</li>
<li><strong>Google +1</strong>
<ul>
<li><a href="http://www.google.com/webmasters/+1/button/">Click here</a>.</li>
<li>Select the &#8220;Tall&#8221; button, and then copy and paste the code.</li>
<li>Find where it says <em>size=&#8221;tall&#8221;</em> in the code, then take a space and paste: <em>href=&#8221;&lt;?php echo $url; ?&gt;&#8221;</em></li>
</ul>
</ul>
<p>And that&#8217;s you done! Your code should look <strong>similar to what&#8217;s below</strong>.</p>
<p><em>Automatic Way:</em></p>
<p>Copy and paste the following, but on line 3, make sure you change the <em>data-via=&#8221;problogdesign</em>&#8221; part to use <strong>your Twitter username instead</strong>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;social-float&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-twitter&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://twitter.com/share&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;twitter-share-button&quot;</span> data-count<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;vertical&quot;</span> data-via<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;problogdesign&quot;</span> data-url<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $url; ?&gt;</span></span>&quot; data-text=&quot;<span style="color: #009900;">&lt;?php echo $text; ?&gt;</span>&quot;&gt;Tweet<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://platform.twitter.com/widgets.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-facebook&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.facebook.com/plugins/like.php?app_id=186708408052490&amp;amp;href=&lt;?php echo urlencode($url); ?&gt;</span></span><span style="color: #ddbb00;">&amp;amp;</span>send=false<span style="color: #ddbb00;">&amp;amp;</span>layout=box_count<span style="color: #ddbb00;">&amp;amp;</span>width=50<span style="color: #ddbb00;">&amp;amp;</span>show_faces=false<span style="color: #ddbb00;">&amp;amp;</span>action=like<span style="color: #ddbb00;">&amp;amp;</span>colorscheme=light<span style="color: #ddbb00;">&amp;amp;</span>font<span style="color: #ddbb00;">&amp;amp;</span>height=62&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;border:none; overflow:hidden; width:50px; height:62px;&quot; allowTransparency=&quot;true&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sf-plusone&quot;</span>&gt;</span>
		<span style="color: #808080; font-style: italic;">&lt;!-- Place this tag in your head or just before your close body tag --&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;https://apis.google.com/js/plusone.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- Place this tag where you want the +1 button to render --&gt;</span>
		<span style="color: #009900;">&lt;g:plusone <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tall&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $url; ?&gt;</span></span>&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>g:plusone&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!-- /social-float --&gt;</span></pre></td></tr></table></div>

<h2>CSS Time</h2>
<p>The majority of our effect is done with just a simple piece of CSS. By <strong>using fixed positioning</strong>, we can anchor our box to the corner of the screen (It won&#8217;t work in IE6, but that&#8217;s okay).</p>
<p>The following will put the box <strong>10px from the bottom and left edges</strong> of their browser (Don&#8217;t copy this just yet).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#social-</span><span style="color: #000000; font-weight: bold;">float</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>If you try that, it works, but of course, we want to style it too. One of the annoyances of these share buttons is that each vendor has decided to <strong>vary their button&#8217;s size by 1 or 2 pixels</strong>. </p>
<p>The following CSS makes a good <strong>template to work from</strong>. I&#8217;ve measured the sizes so each button is 10px from the next, with a 10px padding around the whole thing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Floating Social Buttons
 */</span>
&nbsp;
<span style="color: #cc00cc;">#social-</span><span style="color: #000000; font-weight: bold;">float</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">55px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">180</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">180</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">180</span><span style="color: #00AA00;">,</span> .7<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
	-webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
	-moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
	border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sf-twitter</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">62px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sf-facebook</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.sf-plusone</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>To tweak the style, change the background-color and border parts of <em>#social-float</em> (Right now, I&#8217;ve just used some CSS3 to quickly make a rounded, semi-transparent border).</p>
<p><strong>Important</strong>: Don&#8217;t worry that it&#8217;s <strong>not showing up</strong> for you. The <em>display: none;</em> in there is hiding it. Read on!</p>
<h2>Hide on Smaller Browsers</h2>
<p>We don&#8217;t want to show the box on smaller browsers because it will obstruct the post content. To solve that, let&#8217;s use <strong>jQuery to check the browser size</strong>, and show the box only if it is wide enough.</p>
<p>First up, make sure you&#8217;ve <strong>loaded jQuery</strong> by adding the following to your <em>functions.php</em> file, or to <em>header.php</em> (Before the wp_head(); tag).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_enqueue_script<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now, go back to <em>footer.php</em> and after our button HTML, paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Show social voter only if the browser is wide enough.</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">1030</span> <span style="color: #009900;">&#41;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#social-float'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Update when user resizes browser.</span>
		$<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1030</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#social-float'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#social-float'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Lines 4 and 5 run when the page is first loaded. They check the width, and show the box <strong>if it is wide enough</strong>.</p>
<p>Lines 8 is <strong>an event handler</strong> that runs when the user <a href="http://api.jquery.com/resize/">resizes their browser</a>. When they do, we run the exact same check as above, and either hide/show the box as appropriate.</p>
<p>I&#8217;ve set the width to 1030px, which is 980px (A common page width), plus 150px (The width of our box) times 2 (The page is centered, so 150px on either side). Feel free to change this however you like, but make sure you <strong>update it in both locations</strong>.</p>
<p>Now save your code and check it out on site. With <strong>everything in place now</strong>, feel free to tweak the style and you&#8217;re good to go.</p>
<p>If you hit any issues, let me know in the comments! Good luck!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/lT-nGEaaesQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/how-to-make-a-floating-share-box/feed/</wfw:commentRss>
		<slash:comments>181</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/how-to-make-a-floating-share-box/</feedburner:origLink></item>
		<item>
		<title>Validate Forms in WordPress with jQuery</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/Piv30odw2Ys/</link>
		<comments>http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 16:00:35 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8446</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/validate.jpg" alt="Validate WordPress Forms with jQuery" title="Validate WordPress Forms with jQuery" width="629" height="180" class="alignnone size-full wp-image-8458" />

This article will teach you how to add validation to your <strong>WordPress comment forms</strong>, using jQuery.

With validation, users can submit their comment and instantly see if they've hit an issue. There is no need to submit the page and get WordPress' default "There is a problem..." page. Instead, errors are <strong>displayed inline instantly</strong>.

In our example, we will build a plugin which you can <strong>drop into any theme</strong> and will work instantly.

Just like last week, you can see the finished product in action <a href="http://www.problogdesign.com/demo/validate-comments/2011/06/21/hello-world/">on the demo site</a>. And you can download the <a href="http://www.problogdesign.com/uploads/pbd-validate-comments.zip">completed files here</a>.]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/validate.jpg" alt="Validate WordPress Forms with jQuery" title="Validate WordPress Forms with jQuery" width="629" height="180" class="alignnone size-full wp-image-8458" /></p>
<p>This article will teach you how to add validation to your <strong>WordPress comment forms</strong>, using jQuery.</p>
<p>With validation, users can submit their comment and instantly see if they&#8217;ve hit an issue. There is no need to submit the page and get WordPress&#8217; default &#8220;There is a problem&#8230;&#8221; page. Instead, errors are <strong>displayed inline instantly</strong>.</p>
<p>In our example, we will build a plugin which you can <strong>drop into any theme</strong> and will work instantly.</p>
<p>Just like last week, you can see the finished product in action <a href="http://www.problogdesign.com/demo/validate-comments/2011/06/21/hello-world/">on the demo site</a>. And you can download the <a href="http://www.problogdesign.com/uploads/pbd-validate-comments.zip">completed files here</a>.</p>
<h2>Organize Your Files</h2>
<p>Download the <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">bassistance.de</a> form validation plugin for jQuery.</p>
<p>This plugin contains the file <em>jquery.validate.min.js</em>, which will be taking care of all our validation worries for us. All we have to do is <strong>tell it how and when to work</strong>.</p>
<p>I&#8217;ve <strong>called the plugin &#8220;pbd-validate-comments&#8221;</strong>, though you&#8217;re welcome to name it however you like. You should have a .php file with the same name, and then a &#8220;css&#8221; and &#8220;js&#8221; folder. Create a <em>style.css</em> file in the <em>css</em> folder, and place the <em>jquery.validate.min.js</em> file from above in the <em>js</em> folder.</p>
<p>The end result is like this:</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/files1.jpg" alt="Files" title="Files" width="281" height="95" class="alignnone size-full wp-image-8461" /></p>
<p>With your files in place, open up the <em>pbd-validate-comments.php</em> file and start with the <strong>essential plugin info</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Plugin Name: PBD Validate Comments
 * Plugin URI: http://www.problogdesign.com/
 * Description: Validate comments instantly with jQuery. Uses &lt;a href=&quot;http://bassistance.de/jquery-plugins/jquery-plugin-validation/&quot;&gt;jQuery Form Validation&lt;/a&gt; plugin by Jörn Zaefferer.
 * Version: 0.1
 * Author: Pro Blog Design
 * Author URI: http://www.problogdesign.com/
 * License: GPLv2
 */</span></pre></td></tr></table></div>

<h2>Load Your Scripts</h2>
<p>Now, let&#8217;s begin. The process today is quite simple, and will have 3 steps.</p>
<ol>
<li>Load all of our <strong>scripts</strong>.</li>
<li><strong>Initialize</strong> the validation on our form, and tell it the rules to use.</li>
<li><strong>Style</strong> the error messages.</li>
</ol>
<p>Let&#8217;s start with <strong>loading the scripts</strong>. Still in <em>pbd-validate-comments.php</em>, paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Add jQuery Validation script on posts.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_vc_scripts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		wp_enqueue_script<span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'jquery-validate'</span><span style="color: #339933;">,</span>
			plugin_dir_url<span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/js/jquery.validate.min.js'</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'1.8.1'</span><span style="color: #339933;">,</span>
			<span style="color: #009900; font-weight: bold;">true</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		wp_enqueue_style<span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'jquery-validate'</span><span style="color: #339933;">,</span>
			plugin_dir_url<span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/css/style.css'</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'1.0'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_redirect'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_vc_scripts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The important thing here is the <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">wp_enqueue_script()</a> section. This is what tells WordPress to load in our validation script. Similarly, <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">wp_enqueue_style()</a> is used to load our stylesheet.</p>
<p>We have then wrapped that up in an <a href="http://codex.wordpress.org/Conditional_Tags">is_single() condition</a>, so that our script is <strong>only loaded on post pages</strong>. And in order for that to work, we have put the whole thing in a hook (Attached to &#8216;template_redirect&#8217;).</p>
<p>The end result is that our script is now loaded on every post page, but not on the homepage/categories etc.</p>
<h2>Activate the Script</h2>
<p>Congrats, you&#8217;ve loaded in the scripts now. The next step is to <strong>initialize the validator</strong> and give it our rules.</p>
<p>One way to give the rules <strong>is to use CSS classes,</strong> e.g. class=&#8221;required&#8221; would mean the field is required.</p>
<p>However, with new themes using the <a href="http://codex.wordpress.org/Function_Reference/comment_form">comment_form()</a> function, that gets more complicated. So instead, we will <strong>pass all of the rules via JavaScript</strong>.</p>
<p>Carry on in the PHP file, and paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Initiate the script.
 * Calls the validation options on the comment form.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_vc_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
		jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#commentform'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>Here, we open a new function (Which we will hook into place on the page in a minute), and start writing some JavaScript.</p>
<p>The &#8216;<em>#commentform</em>&#8216; needs to be a <strong>jQuery selector for your form</strong>. In most cases, you can leave this as it is because 99% of WordPress themes will be using that ID on their comment forms. </p>
<p>We are now going to pass in an array of options. The first will be <strong>an array of rules</strong>. You can see the <a href="http://docs.jquery.com/Plugins/Validation#List_of_built-in_Validation_methods">full list of validation rules</a> available here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
	author<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		minlength<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	email<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		email<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	url<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		url<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	comment<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
		minlength<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>We have simply taken rules from the available list, and applied them to the various fields (author, email etc. are the <strong>&#8220;name&#8221; values for the comment form fields</strong>, and will be in every WordPress theme).</p>
<p>These rules translate to:</p>
<ul>
<li><strong>Author</strong> Field &#8211; Must be filled in, with a minimum of 2 characters.</li>
<li><strong>Email</strong> Field &#8211; Must be filled in, with a valid email address.</li>
<li><strong>URL</strong> Field &#8211; Not required, but if something is entered, it must be a valid URL.</li>
<li><strong>Comment</strong> Field &#8211; Required, and must be at least 20 characters long.</li>
</ul>
<p>How simple was that?</p>
<p>We could leave it there, but let&#8217;s pass in one more option; a <strong>custom error message</strong> for each field.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">messages<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
	author<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter a valid name.&quot;</span><span style="color: #339933;">,</span>
	email<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #339933;">,</span>
	url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please use a valid website address.&quot;</span><span style="color: #339933;">,</span>
	comment<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Message must be at least 20 characters.&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Again, this is quite self-explanatory. The messages entered will appear when there is an error in the corresponding field. </p>
<p>Now, just close up the JavaScript functions and the script tag, and then <strong>hook the PHP function</strong> into place.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">			});
		});
	&lt;/script&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_footer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_vc_init'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">999</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And that&#8217;s the end of our work, so we&#8217;ve closed up the PHP file!</p>
<h2>Last of All, Some Styles</h2>
<p>If you save and activate the plugin now, it all works perfectly well. However, a little <strong>CSS for the error messages</strong> won&#8217;t hurt.</p>
<p>Open the <em>style.css</em> file, and paste the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">label<span style="color: #6666ff;">.error</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ffd2d2</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s it! Save your plugin, activate it in the dashboard and go try posting some phony comments. You should see <strong>instant errors pointing out any issues</strong>.</p>
<p>The jQuery validation script is hugely powerful and suitable for just about any type of form. Once you know how to use it in WordPress as we have done here, you&#8217;ll find <strong>you can use it anywhere</strong>!</p>
<p>And don&#8217;t forget, if you <strong>don&#8217;t want to put the files together</strong> yourself, you can grab the <a href="http://www.problogdesign.com/uploads/pbd-validate-comments.zip">completed download here</a>.</p>
<p>Please let me know if you enjoyed this post! If you&#8217;d <strong>like to see more jQuery guides</strong> on the site, leave a comment!</p>
<p><em>The idea behind this post was a collaboration between myself, Michael Martin, and Seth Bryant, thanks Seth!</em></p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/Piv30odw2Ys" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/feed/</wfw:commentRss>
		<slash:comments>78</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 4.788 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-14 17:01:38 --><!-- Compression = gzip -->

