<?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>Sun, 01 Jan 2012 20:23:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</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>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>35</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>82</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>152</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>53</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/</feedburner:origLink></item>
		<item>
		<title>Load Next WordPress Posts With AJAX</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/eKy5buoHqWw/</link>
		<comments>http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 16:00:21 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8414</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/ajax-load-posts.jpg" alt="AJAX Load Posts" title="AJAX Load Posts" width="629" height="180" class="alignnone size-full wp-image-8420" />

Today, we're going to replace the standard "Older Posts" links on your blog. Our plugin will create a button to <strong>instantly load the next page of posts</strong>, without reloading the page (Similar to what Twitter used to do at the bottom of profiles)

Click here to load <a href="http://www.problogdesign.com/demo/ajax-load-posts/">the demo site</a> and see <strong>the final project in action</strong>.

You can also download <strong>the completed files as a plugin</strong> <a href="http://www.problogdesign.com/uploads/pbd-ajax-load-posts.zip">here</a> (Just upload and activate it).]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/ajax-load-posts.jpg" alt="AJAX Load Posts" title="AJAX Load Posts" width="629" height="180" class="alignnone size-full wp-image-8420" /></p>
<p>Today, we&#8217;re going to replace the standard &#8220;Older Posts&#8221; links on your blog. Our plugin will create a button to <strong>instantly load the next page of posts</strong>, without reloading the page (Similar to what Twitter used to do at the bottom of profiles)</p>
<p>Click here to load <a href="http://www.problogdesign.com/demo/ajax-load-posts/">the demo site</a> and see <strong>the final project in action</strong>.</p>
<p>You can also download <strong>the completed files as a plugin</strong> <a href="http://www.problogdesign.com/uploads/pbd-ajax-load-posts.zip">here</a> (Just upload and activate it).</p>
<p>Now, let&#8217;s get started. Our plugin is going to have several features:</p>
<ul>
<li><strong>Multiple clicks</strong> &#8211; First clicks will load page 2&#8242;s posts, second will load page 3 etc.)</li>
<li><strong>Check for posts first</strong> &#8211; If there are no more posts that can be loaded, we&#8217;ll tell the user.</li>
<li><strong>Degrade gracefully</strong> &#8211; If a visitor doesn&#8217;t use JavaScript, we won&#8217;t change the site at all.</li>
</ul>
<h2>Plugin Structure</h1>
<p>There will be 3 files in this plugin (One PHP, one CSS, one JS). For good practice, we&#8217;ll keep the CSS and JavaScript in <strong>their own folders</strong>.</p>
<p>And I&#8217;ve called the plugin &#8220;pbd-ajax-load-posts&#8221;.</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/files.jpg" alt="File Structure" title="File Structure" width="263" height="114" class="alignnone size-full wp-image-8417" /></p>
<p>Now let&#8217;s start by putting the necessities at the top of our <em>pbd-ajax-load-posts.php</em> file:</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 AJAX Load Posts
 * Plugin URI: http://www.problogdesign.com/
 * Description: Load the next page of posts with AJAX.
 * Version: 0.1
 * Author: Pro Blog Design
 * Author URI: http://www.problogdesign.com/
 * License: GPLv2
 */</span></pre></td></tr></table></div>

<p>All good so far? Cool, time to get started for real.</p>
<h2>Loading Files and Passing Values</h2>
<p>The first thing we need to do is ensure that our JavaScript and CSS files are <strong>loaded on the right pages</strong>. Stay in the <em>pbd-ajax-load-posts.php</em> file and paste this:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #009933; font-style: italic;">/**
  * Initialization. Add our script if needed on this page.
  */</span>
 <span style="color: #000000; font-weight: bold;">function</span> pbd_alp_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;">global</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">// Add code to index pages.</span>
 	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</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: #666666; font-style: italic;">// Queue JS and CSS</span>
 		wp_enqueue_script<span style="color: #009900;">&#40;</span>
 			<span style="color: #0000ff;">'pbd-alp-load-posts'</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/load-posts.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.0'</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;">'pbd-alp-style'</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: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
 			<span style="color: #0000ff;">'1.0'</span><span style="color: #339933;">,</span>
 			<span style="color: #0000ff;">'all'</span>
 		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code above begins by creating a new function, <em>pbd_alp_init()</em> (alp = AJAX Load Posts), which we will then hook into place later on.</p>
<p>You&#8217;ll notice that we also call the<strong> global <em>$wp_query</em> variable</strong>, which we&#8217;ll use in the next step.</p>
<p>The important parts begin on line 8. The first statement is a <a href="http://codex.wordpress.org/Conditional_Tags">conditional statement</a>. It means that on any page that isn&#8217;t an individual post or Page, we are going to run this code.</p>
<p>This is a broad brush to make sure our code runs on the homepage, tag pages, search pages etc. You <strong>can adapt it to be more specific</strong>, e.g. if you don&#8217;t want the code included on your homepage.</p>
<p>We then use <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">wp_enqueue_script()</a> and <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_style">wp_enqueue_style()</a> to tell WordPress about our two files (As well as the fact that we will be using jQuery).</p>
<p>Now, we need to pass some values to our script, namely:</p>
<ul>
<li>The <strong>page number</strong> we&#8217;re on right now (Going to page 1 99% of the time, but let&#8217;s be sure).</li>
<li>The <strong>total number</strong> of pages (So we know when we&#8217;ve hit the limit).</li>
<li>The link to the <strong>next page</strong> (e.g. site.com/tag/example/page/2/)</li>
</ul>
<p>We will use the <a href="http://codex.wordpress.org/Function_Reference/wp_localize_script">wp_localize_script()</a> function to calculate each of these values in PHP, and then <strong>print them into the webpage</strong> so that our script can access them later (Hat tip to <a href="http://www.amazon.com/gp/product/0470916222/ref=as_li_ss_tl?ie=UTF8&#038;tag=problodes-20&#038;linkCode=as2&#038;camp=217153&#038;creative=399349&#038;creativeASIN=0470916222">Professional WordPress Plugin Development</a> for introducing me to this great function!)</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// What page are we on? And what is the pages limit?</span>
<span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">max_num_pages</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add some parameters for the JS.</span>
wp_localize_script<span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'pbd-alp-load-posts'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'pbd_alp'</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'startPage'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$paged</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'maxPages'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$max</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'nextLink'</span> <span style="color: #339933;">=&gt;</span> next_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$max</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;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We begin by working out the first 2 values. <em>$max</em> is the <strong>maximum number of pages</strong> the current query can return (e.g. if each page shows 5 posts and there are 12 posts in the current category, then max will be 3).</p>
<p>The <em>$paged</em> variable will store the <strong>page we are currently on</strong> (The whole point of our plugin is to ensure people never load a second page, but it doesn&#8217;t hurt to make sure).</p>
<p>If you skip down to line 12, you&#8217;ll see where I&#8217;ve worked out the 3rd value (The link to the next page). next_posts() is a built-in WordPress function that <strong>will return the URL we need</strong>.</p>
<p>The <em>wp_localize_script()</em> function is great because it makes it easy to <strong>pass values from PHP to JavaScript</strong>. The first value, &#8216;pbd-alp-load-posts&#8217; must match the first value in the <em>wp_enqueue_script()</em> call.</p>
<p>The second value, &#8216;pbd_alp&#8217;, is the <strong>name we will use</strong> in our JavaScript later on.</p>
<p>Finally, we send over <strong>an array of the data</strong>. If you view the HTML source of your webpage later, you&#8217;ll see something like this right before your JavaScript file is loaded:</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="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
<span style="color: #009966; font-style: italic;">/* &lt;![CDATA[ */</span>
<span style="color: #003366; font-weight: bold;">var</span> pbd_alp <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	startPage<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">,</span>
	maxPages<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;6&quot;</span><span style="color: #339933;">,</span>
	nextLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.problogdesign.com/demo/ajax-load-posts/page/2/&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009966; font-style: italic;">/* ]]&gt; */</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Now, we just need to close up our if statement, our <em>pbd_alp_init()</em> function, and then <strong>hook it all into place</strong>.</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: #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_alp_init'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We use the <em>template_redirect</em> hook because with the init hook, the <strong><em>$wp_query</em> variable won&#8217;t be set yet</strong>.</p>
<h2>jQuery &#8211; The Heart of our Plugin</h2>
<p>We have now loaded our scripts and passed the values we need. It&#8217;s time to get to the real meat of our tutorial.</p>
<p>Open the <em>load-posts.js</em> file. The first thing we do is <strong>access the 3 variables</strong> we passed in with our PHP function.</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="javascript" style="font-family:monospace;">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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// The number of the next page to load (/page/x/).</span>
	<span style="color: #003366; font-weight: bold;">var</span> pageNum <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>pbd_alp.<span style="color: #660066;">startPage</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// The maximum number of pages the current query can return.</span>
	<span style="color: #003366; font-weight: bold;">var</span> max <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>pbd_alp.<span style="color: #660066;">maxPages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// The link of the next page of posts.</span>
	<span style="color: #003366; font-weight: bold;">var</span> nextLink <span style="color: #339933;">=</span> pbd_alp.<span style="color: #660066;">nextLink</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The way to access our values is to <strong>use the format</strong>: <em>pbd_alp.valueName</em> (pbd_alp was the second value we entered in wp_localize_script(), remember?).</p>
<p>The important thing to remember is that our numbers have been sent over as strings, so we use JavaScript&#8217;s <a href="http://www.w3schools.com/jsref/jsref_parseint.asp">parseInt() function</a> to convert them back to numbers.</p>
<p>With <em>pageNum</em>, we add one to the number because it is going to store the <strong>number of the <em>next</em> page</strong> to load (Not the current page).</p>
<p>Most themes already have navigation to move between pages, in the form of Older Posts/Newer Posts links. We want to replace that with our AJAX button, so our first step will be to remove those navigation links, and <strong>insert our button 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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Replace the traditional navigation with our own,
 * but only if there is at least one page of new posts to load.
 */</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>pageNum <span style="color: #339933;">&lt;=</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Insert the &quot;More Posts&quot; link.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content'</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;pbd-alp-placeholder-'</span><span style="color: #339933;">+</span> pageNum <span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p id=&quot;pbd-alp-load-posts&quot;&gt;&lt;a href=&quot;#&quot;&gt;Load More Posts&lt;/a&gt;&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Remove the traditional navigation.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.navigation'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We start out with a conditional check. Remember that <em>pageNum</em> is the number of the next page, so if it is greater than <em>max</em>, there are no more pages to load. <strong>In that case, we don&#8217;t want to add the button</strong>.</p>
<p>If there is new content to load though, then we look for the <em>#content</em> div, and add two things to the end of it. The first is an empty div, which we will <strong>later use to insert our posts into</strong>.</p>
<p>The second is <strong>the button itself</strong> (A regular HTML link), wrapped up in a paragraph.</p>
<p>Finally, we look for the <em>.navigation</em> div and <strong>remove it</strong>. If your theme uses a different class for the navigation buttons, you will need to change that (<em>.navigation</em> is the default that the 2010 theme uses). The same thing applies to the <em>#content</em> div too!</p>
<p>The result of the code above is that <strong>our button is now in place</strong>, though it won&#8217;t do anything yet.</p>
<p>And because we did it all with JavaScript, we know <strong>our plugin degrades gracefully</strong> (Because if the JavaScript isn&#8217;t loaded, no changes will be made to the page).</p>
<p>Now, let&#8217;s take care of what happens when the user actually <strong>clicks the button</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="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Load new posts when the link is clicked.
 */</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pbd-alp-load-posts a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Are there more posts to load?</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>pageNum <span style="color: #339933;">&lt;=</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Show that we're working.</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Loading posts...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first line of code is a jQuery event handler, which runs <strong>when a user clicks the button</strong>.</p>
<p>On line 7, we run the same check as before. This is important because our script <strong>would load in a 404 error message</strong> if there were no more posts. Definitely not what we want!</p>
<p>Line 10 updates the text on our button to read &#8220;Loading posts&#8230;&#8221; This is good practice because users will get an <strong>instant reaction when they click</strong> the button.</p>
<p>The next step is to <strong>make the AJAX call</strong>. Quite a lot happens here, so copy and paste the following into your script and I&#8217;ll walk through it all afterwards.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.pbd-alp-placeholder-'</span><span style="color: #339933;">+</span> pageNum<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span>nextLink <span style="color: #339933;">+</span> <span style="color: #3366CC;">' .post'</span><span style="color: #339933;">,</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;">// Update page number and nextLink.</span>
		pageNum<span style="color: #339933;">++;</span>
		nextLink <span style="color: #339933;">=</span> nextLink.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/page\/[0-9]?/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'/page/'</span><span style="color: #339933;">+</span> pageNum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Add a new placeholder, for when user clicks again.</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pbd-alp-load-posts'</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">before</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class=&quot;pbd-alp-placeholder-'</span><span style="color: #339933;">+</span> pageNum <span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// Update the button message.</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>pageNum <span style="color: #339933;">&lt;=</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pbd-alp-load-posts a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Load More Posts'</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;">'#pbd-alp-load-posts a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No more posts to load.'</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></pre></td></tr></table></div>

<p>The first line is the most important. We use a jQuery selector to select our placeholder div. The reason we have added the <em>pageNum</em> number to the end of the class name is so that if the user clicks the button again, we&#8217;ll <strong>add the new posts to a new placeholder</strong> (Not overwrite the old one).</p>
<p>The <a href="http://api.jquery.com/load/">.load() function</a> loads the URL we pass (Remember that <em>nextLink</em> is the URL for the next page), and because we have added .<em>post</em> to the end, it will <strong>only copy over the <em>.post</em> divs</strong> that it finds (Not the whole page!)</p>
<p>On line 2, we start a new function which <strong>will run when the AJAX call completes</strong>. The first thing it does is update our values for the next time the button is clicked.</p>
<p><em>pageNum</em> is increased by one (To point to the new next page), and <em>nextLink</em> is updated using a regular expression. This searches the URL for /page/2/ (Or any number), and <strong>replaces the number portion</strong> with the new next page number.</p>
<p>On line 8, we add <strong>a new placeholder div</strong>. This will be used the next time the button is clicked.</p>
<p>Finally, on line 12, we <strong>update the text on the button again</strong>. If there are more posts that can be loaded, we revert back to the original text. If there aren&#8217;t, then we&#8217;ll update with a message saying so.</p>
<p>Now, we just need to round things off:</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="javascript" style="font-family:monospace;">		<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;">'#pbd-alp-load-posts a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>	
&nbsp;
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This code begins by closing off the first if-statement (Are there more pages to load?). If there aren&#8217;t, it adds a &#8216;.&#8217; to the button&#8217;s message. This is simply to give some sort of <strong>visual response to the user</strong> when the button is clicked (Look at the screenshot below to see what happens).</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/06/no-more.jpg" alt="No More Posts" title="No More Posts" width="629" height="76" class="alignnone size-full wp-image-8432" /></p>
<p>And last of all, we use <em>return false;</em> to <strong>prevent the link</strong> from the button itself loading.</p>
<h2>Style It</h2>
<p>Your button is now fully working! The only thing remaining is to style it. You can do this <strong>however you like with CSS</strong>. I&#8217;ve used CSS3 to round off the corners, and add a gradient and drop shadow.</p>
<p>You can look these up more online (Or post in the comments if you&#8217;d like to <strong>see some CSS3 tuts here</strong>), but a big thanks is due to <a href="http://css-tricks.com/css3-gradients/">CSS Tricks</a> for making the cross-browser gradient code easy.</p>
<p>Add this to your <em>css/style.css</em> file:</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
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#pbd-alp-load-posts</span> a<span style="color: #3333ff;">:link</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#pbd-alp-load-posts</span> a<span style="color: #3333ff;">:visited </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;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#444</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/** Rounded Corners **/</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: #808080; font-style: italic;">/** Drop shadow **/</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span>
	-webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span>
	box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/** Gradients : http://css-tricks.com/css3-gradients/ */</span>
	<span style="color: #808080; font-style: italic;">/* fallback */</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Firefox 3.6+ */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -moz-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #933;">100%</span> <span style="color: #933;">100%</span> 90deg<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#e4e3e3</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Safari 4-5, Chrome 1-9 */</span>
	<span style="color: #808080; font-style: italic;">/* -webkit-gradient(&lt;type&gt;, &lt;point&gt; [, &lt;radius&gt;]?, &lt;point&gt; [, &lt;radius&gt;]? [, &lt;stop&gt;]*) */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -webkit-gradient<span style="color: #00AA00;">&#40;</span>linear<span style="color: #00AA00;">,</span> <span style="color: #933;">0%</span> <span style="color: #933;">0%</span><span style="color: #00AA00;">,</span> <span style="color: #933;">0%</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">,</span> from<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span> to<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#e4e3e3</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Safari 5.1+, Chrome 10+ */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -webkit-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#e4e3e3</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Opera 11.10+ */</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -o-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#e4e3e3</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#pbd-alp-load-posts</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#pbd-alp-load-posts</span> a<span style="color: #3333ff;">:active </span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/** Drop shadow **/</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#bbb</span><span style="color: #00AA00;">;</span>
	-webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#bbb</span><span style="color: #00AA00;">;</span>
	box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #cc00cc;">#bbb</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/** Gradients : http://css-tricks.com/css3-gradients/ */</span>
	<span style="color: #808080; font-style: italic;">/* fallback */</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#f5f5f5</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Firefox 3.6+ */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -moz-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #933;">100%</span> <span style="color: #933;">100%</span> 90deg<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#eaeaea</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#f5f5f5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Safari 4-5, Chrome 1-9 */</span>
	<span style="color: #808080; font-style: italic;">/* -webkit-gradient(&lt;type&gt;, &lt;point&gt; [, &lt;radius&gt;]?, &lt;point&gt; [, &lt;radius&gt;]? [, &lt;stop&gt;]*) */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -webkit-gradient<span style="color: #00AA00;">&#40;</span>linear<span style="color: #00AA00;">,</span> <span style="color: #933;">0%</span> <span style="color: #933;">0%</span><span style="color: #00AA00;">,</span> <span style="color: #933;">0%</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">,</span> from<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f5f5f5</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span> to<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#eaeaea</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Safari 5.1+, Chrome 10+ */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -webkit-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f1f1f1</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#eaeaea</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* Opera 11.10+ */</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -o-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #cc00cc;">#f5f5f5</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#eaeaea</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s you done! Just save your work and <strong>activate the plugin</strong>.</p>
<p>If you <strong>have any issues</strong>, check that your theme uses the <em>#content</em> and <em>.navigation</em> divs in the way that we assumed. Most themes will, but not all. If yours is different, update those values in the JavaScript file.</p>
<p>Let me know <strong>what you thought</strong> of the effect! And don&#8217;t forget you can grab the <a href="http://www.problogdesign.com/uploads/pbd-ajax-load-posts.zip">completed plugin here</a>.</p>
<p>And if you have <strong>any feedback on the tutorial</strong>, I&#8217;d love to hear it! Too long? Or more explanation needed? (The demo site exists now purely because some of you asked for it in previous tutorials, so thank you!)</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/eKy5buoHqWw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/feed/</wfw:commentRss>
		<slash:comments>129</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/</feedburner:origLink></item>
		<item>
		<title>User Friendly Short Codes with TinyMCE</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/Pfst05HV5mk/</link>
		<comments>http://www.problogdesign.com/wordpress/user-friendly-short-codes-with-tinymce/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 16:00:43 +0000</pubDate>
		<dc:creator>Pippin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[buttons]]></category>
		<category><![CDATA[Editor]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[shortcodes]]></category>
		<category><![CDATA[tinymce]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8315</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/post-image.png" alt="" title="post-image" width="629" height="180" class="alignnone size-full wp-image-8342" />

WordPress short codes are everywhere: in  practically every theme and many, many plugins. They are extremely useful and provide huge amounts of extra functionality. With all of their greatness, however, there is one major problem with them:<strong> they are not very user friendly</strong>, particularly for users who are not familiar with programming, especially when a short code accepts more than one parameter. So, in order to help the non-programmer users, we need a new solution.

The ideal solution is one that provides an interface for the user to input all of their short code options <strong>without having to enter any "code"</strong>. This is a pretty common technique used by a lot of developers, but until now, there has never been a good tutorial on it, so we are going to fix that.]]></description>
			<content:encoded><![CDATA[<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/post-image.png" alt="" title="post-image" width="629" height="180" class="alignnone size-full wp-image-8342" /></p>
<p>WordPress short codes are everywhere: in  practically every theme and many, many plugins. They are extremely useful and provide huge amounts of extra functionality. With all of their greatness, however, there is one major problem with them:<strong> they are not very user friendly</strong>, particularly for users who are not familiar with programming, especially when a short code accepts more than one parameter. So, in order to help the non-programmer users, we need a new solution.</p>
<p>The ideal solution is one that provides an interface for the user to input all of their short code options <strong>without having to enter any &#8220;code&#8221;</strong>. This is a pretty common technique used by a lot of developers, but until now, there has never been a good tutorial on it, so we are going to fix that.</p>
<p>In this tutorial I&#8217;m going to show you how to <strong>add short code buttons to the tinyMCE editor</strong> that, when clicked, bring up a modal window with all of the options for your short code.</p>
<p>Through the steps in this tutorial, we will be <strong>writing a complete WordPress plugin</strong> that will provide a small collection of CSS 3 buttons available for use through short codes. All of the options for the buttons, size, color, style, url, text, etc, will be available through the modal window.</p>
<p>If you look at the image at the top of this post, you can see exactly <strong>how that modal window will look</strong> (Much easier than remembering all of the short code&#8217;s option names yourself!)</p>
<h3>Prerequisites</h3>
<p>In order to follow this tutorial, one of the first things you will need to know is <strong>how to create short codes</strong>. If you are not yet familiar with the process, please follow my tutorial <a href="http://www.problogdesign.com/wordpress/working-with-wordpress-shortcodes/">Working with WordPress Short Codes</a>.</p>
<h3>Getting Started</h3>
<p>The first thing we need to do is setup our plugin structure. Please use the following file structure as a reference.</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/file-structure.png" alt="" title="file structure" width="305" height="189" class="alignnone size-full wp-image-8317" /></p>
<p>To make things easier, I would recommend that you go ahead and create each of the files according to the image. I will reference the filename we&#8217;re using in each step.</p>
<h3>Beginning the Plugin</h3>
<p>Just like every other WordPress plugin, we need to setup our plugin header in order to tell WordPress about our plugin. Place the following code into <em>friendly-shortcode-buttons.php</em>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
Plugin Name: Friendly Short Code Buttons
Plugin URI: http://pippinsplugins.com/user-friendly-short-codes-plugin-example/
Description: Adds user-friendly short code buttons to your  WordPress site. This plugin is more of an example than anything, but does provide a few nice looking buttons
Version: 1.0
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
*/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// plugin root folder</span>
<span style="color: #000088;">$fscb_base_dir</span> <span style="color: #339933;">=</span> WP_PLUGIN_URL <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">basename</span><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;">&quot;&quot;</span> <span style="color: #339933;">,</span>plugin_basename<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Our plugin will now be available to WordPress. Note that I have also placed a variable called <em>$fscb_base_dir</em>. This is a variable that contains the file path to the root folder of our plugin. We will use this later to load CSS and JS scripts.</p>
<h3>Setting Up the Short Code</h3>
<p>Now that we have our plugin registered with WordPress, we need to setup our short code and its options. This is a very simple function that registers our short code with WordPress and sets up the HTML structure of our buttons. Place this code in <em>friendly-shortcode-buttons.php</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
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// setup the shortcode for use</span>
<span style="color: #000000; font-weight: bold;">function</span> friendly_button_shortcode<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$atts</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</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>
      <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span> shortcode_atts<span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'blue'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'medium'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'style'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'round'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'align'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'none'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$atts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&lt;div class=&quot;friendly_button friendly_button_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$size</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' friendly_button_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$color</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' friendly_button_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$style</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' friendly_button_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$align</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$content</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'button'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'friendly_button_shortcode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The return statement of this function may look a little complex, but all I have done is make it so that a DIV with a class name for each option is displayed. So there is a class for the size, the color, the style, etc. This will allow us to style each kind of button with CSS later on.</p>
<h3>The Button CSS</h3>
<p>Our button short code is now available in WordPress, so let&#8217;s go ahead and add all of the necessary CSS. We will use <em>wp_enqueue_style()</em> to load our CSS into the site header. Again, place this in <em>friendly-shortcode-buttons.php</em>:</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: #666666; font-style: italic;">// load button css</span>
<span style="color: #000000; font-weight: bold;">function</span> friendly_buttons_css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// the path to our root plugin folder</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$fscb_base_dir</span><span style="color: #339933;">;</span>
	wp_enqueue_style<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'friendly-buttons'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fscb_base_dir</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'includes/css/friendly_buttons.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_print_styles'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'friendly_buttons_css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Remember the GLOBAL variable we defined earlier with the path to the root plugin folder? We&#8217;re using that here to obtain the correct path to our CSS file, which we will create now. As indicated in the file structure diagram above, the CSS file is called <em>friendly_buttons.css</em> as is located inside of <em>includes/css/</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
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.friendly_button</span> a<span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span>arial<span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">8px</span> <span style="color: #933;">24px</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</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> 
	-moz-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">6px</span><span style="color: #00AA00;">;</span> 
	-webkit-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">6px</span><span style="color: #00AA00;">;</span> 
	border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">6px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button</span> a<span style="color: #3333ff;">:active</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;">relative</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/******************************************************
* sizes
******************************************************/</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_large</span> a<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">16px</span> !important<span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_small</span> a<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">11px</span> !important<span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">6px</span> <span style="color: #933;">16px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_medium</span> a<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span> <span style="color: #933;">22px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/******************************************************
* sizes
******************************************************/</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_left</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_left</span> a<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_right</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_right</span> a<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_none</span> <span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span> zoom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/******************************************************
* styles
******************************************************/</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_round</span> a <span style="color: #00AA00;">&#123;</span>
	-moz-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> -webkit-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">15px</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_less_round</span> a <span style="color: #00AA00;">&#123;</span>
	-moz-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> -webkit-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">10px</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_square</span> a <span style="color: #00AA00;">&#123;</span>
	-moz-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">1px</span><span style="color: #00AA00;">;</span> -webkit-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">1px</span><span style="color: #00AA00;">;</span> border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">1px</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/******************************************************
* colors
******************************************************/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*black button*/</span>
<span style="color: #6666ff;">.friendly_button_gray</span> a<span style="color: #00AA00;">&#123;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> 
	-webkit-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> 
	box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ededed</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#dfdfdf</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ededed</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#dfdfdf</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#ededed'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#dfdfdf'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#ededed</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#dcdcdc</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#777</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_gray</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#dfdfdf</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ededed</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#dfdfdf</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ededed</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#dfdfdf'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#ededed'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#dfdfdf</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*black button*/</span>
<span style="color: #6666ff;">.friendly_button_blue</span> a<span style="color: #00AA00;">&#123;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#bdddff</span><span style="color: #00AA00;">;</span> 
	-webkit-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#bdddff</span><span style="color: #00AA00;">;</span> 
	box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#bdddff</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#7db8ff</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#2c80e8</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#7db8ff</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#2c80e8</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#7db8ff'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#2c80e8'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#7db8ff</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#047fd1</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_blue</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#2c80e8</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#7db8ff</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#2c80e8</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#7db8ff</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#2c80e8'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#7db8ff'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#2c80e8</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*black button*/</span>
<span style="color: #6666ff;">.friendly_button_red</span> a<span style="color: #00AA00;">&#123;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#ffa6a6</span><span style="color: #00AA00;">;</span> 
	-webkit-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#ffa6a6</span><span style="color: #00AA00;">;</span> 
	box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#ffa6a6</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#f55858</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ba1c1c</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#f55858</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ba1c1c</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#f55858'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#ba1c1c'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#f55858</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#8a0a0a</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_red</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ba1c1c</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#f55858</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#ba1c1c</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#f55858</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#ba1c1c'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#f55858'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#ba1c1c</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*black button*/</span>
<span style="color: #6666ff;">.friendly_button_green</span> a<span style="color: #00AA00;">&#123;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#39de10</span><span style="color: #00AA00;">;</span> 
	-webkit-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#39de10</span><span style="color: #00AA00;">;</span> 
	box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#39de10</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#13c20a</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#05ad19</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#13c20a</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#05ad19</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#13c20a'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#05ad19'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#13c20a</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#169124</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.friendly_button_green</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#05ad19</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#13c20a</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#05ad19</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#13c20a</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#05ad19'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#13c20a'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#05ad19</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*black button*/</span>
<span style="color: #6666ff;">.friendly_button_black</span> a<span style="color: #00AA00;">&#123;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#8a8a8a</span><span style="color: #00AA00;">;</span> 
	-webkit-box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#8a8a8a</span><span style="color: #00AA00;">;</span> 
	box-shadow<span style="color: #00AA00;">:</span><span style="color: #993333;">inset</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #cc00cc;">#8a8a8a</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#525252</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#525252</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#000</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#525252'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#000'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#525252</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.friendly_button_black</span> a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-webkit-</span>gradient<span style="color: #00AA00;">&#40;</span> linear<span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.05</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>color-stop<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#525252</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:-moz-linear-</span>gradient<span style="color: #00AA00;">&#40;</span> <span style="color: #993333;">center</span> <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#000</span> <span style="color: #933;">5%</span><span style="color: #00AA00;">,</span><span style="color: #cc00cc;">#525252</span> <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	filter<span style="color: #3333ff;">:progid</span><span style="color: #3333ff;">:DXImageTransform</span><span style="color: #6666ff;">.Microsoft</span>.gradient<span style="color: #00AA00;">&#40;</span>startColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#000'</span><span style="color: #00AA00;">,</span>endColorstr<span style="color: #00AA00;">=</span><span style="color: #ff0000;">'#525252'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Note, I am not going to explain which parts of the CSS do what. That is not the purpose of this tutorial. Suffice it to say that there are separate CSS styles for each color, size and type of button, as well as a couple other items, such as alignment. The code is well laid out and commented, so it should not be difficult to follow.</p>
<p>Excellent. Now that we have our button short code created and our CSS loaded, we will now be able to create buttons that look like this:</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/screenshot1.png" alt="" title="screenshot" width="629" height="257" class="alignnone size-full wp-image-8325" /></p>
<h3>Setting Up the TinyMCE Button</h3>
<p>It is now time to begin the really fun part of this tutorial: adding a button to our tinyMCE editor that allows us to enter the short code into our post content. We are first going to register a &#8220;plugin&#8221; with tinyMCE (this is our button), and then we will connect that button to a modal popup window. So to begin, we need to place three functions into <em>friendly-shortcode-buttons.php</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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// registers the buttons for use</span>
<span style="color: #000000; font-weight: bold;">function</span> register_friendly_buttons<span style="color: #009900;">&#40;</span><span style="color: #000088;">$buttons</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// inserts a separator between existing buttons and our new one</span>
	<span style="color: #666666; font-style: italic;">// &quot;friendly_button&quot; is the ID of our button</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$buttons</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;|&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;friendly_button&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$buttons</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// filters the tinyMCE buttons and adds our custom buttons</span>
<span style="color: #000000; font-weight: bold;">function</span> friendly_shortcode_buttons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Don't bother doing this stuff if the current user lacks permissions</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> current_user_can<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_posts'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span> current_user_can<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_pages'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Add only in Rich Editor mode</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_user_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rich_editing'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'true'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// filter the tinyMCE buttons and add our own</span>
		add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mce_external_plugins&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;add_friendly_tinymce_plugin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mce_buttons'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'register_friendly_buttons'</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: #666666; font-style: italic;">// init process for button control</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'friendly_shortcode_buttons'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// add the button to the tinyMCE bar</span>
<span style="color: #000000; font-weight: bold;">function</span> add_friendly_tinymce_plugin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$plugin_array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$fscb_base_dir</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$plugin_array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'friendly_button'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fscb_base_dir</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'friendly-shortcode-buttons.js'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$plugin_array</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The first of these functions registers our button with tinyMCE. It places a separator at the end of the current buttons, then our custom button following that.</p>
<p>The second function uses an <em>add_filter</em> hook to make our custom button actually display. It places the button created in the first function in the tinyMCE buttons.</p>
<p>And the third function connects our button to a javascript file. This is the file that controls the behavior of our button.</p>
<p><strong>Important!</strong> In both the first and third function I have used <em>friendly_button</em> as an ID for the button. It&#8217;s very important that this never changes. I will use <em>friendly_button</em> a couple more times in this tutorial and it must be the same throughout.</p>
<h3>Our TinyMCE Button&#8217;s Javascript</h3>
<p>It is now time to create the javascript file that will control the behavior of the tinyMCE button that we added above.</p>
<p>Place this code into <em>friendly-shortcode-buttons.js</em>, which, remember the diagram, resides in the root plugin folder.</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="javascript" style="font-family:monospace;"><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>
	tinymce.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tinymce.plugins.buttonPlugin'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		init <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ed<span style="color: #339933;">,</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// Register commands</span>
			ed.<span style="color: #660066;">addCommand</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mcebutton'</span><span style="color: #339933;">,</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>
				ed.<span style="color: #660066;">windowManager</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
					file <span style="color: #339933;">:</span> url <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/button_popup.php'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// file that contains HTML for our modal window</span>
					width <span style="color: #339933;">:</span> <span style="color: #CC0000;">220</span> <span style="color: #339933;">+</span> parseInt<span style="color: #009900;">&#40;</span>ed.<span style="color: #660066;">getLang</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.delta_width'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// size of our window</span>
					height <span style="color: #339933;">:</span> <span style="color: #CC0000;">240</span> <span style="color: #339933;">+</span> parseInt<span style="color: #009900;">&#40;</span>ed.<span style="color: #660066;">getLang</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.delta_height'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// size of our window</span>
					inline <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
					plugin_url <span style="color: #339933;">:</span> url
				<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>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Register buttons</span>
			ed.<span style="color: #660066;">addButton</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'friendly_button'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>title <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Insert Button'</span><span style="color: #339933;">,</span> cmd <span style="color: #339933;">:</span> <span style="color: #3366CC;">'mcebutton'</span><span style="color: #339933;">,</span> image<span style="color: #339933;">:</span> url <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/includes/images/icon.gif'</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: #339933;">,</span>
&nbsp;
		getInfo <span style="color: #339933;">:</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;">return</span> <span style="color: #009900;">&#123;</span>
				longname <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Insert Button'</span><span style="color: #339933;">,</span>
				author <span style="color: #339933;">:</span> <span style="color: #3366CC;">'Pippin Williamson'</span><span style="color: #339933;">,</span>
				authorurl <span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://pippinsplugins.com'</span><span style="color: #339933;">,</span>
				infourl <span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://pippinsplugins.com'</span><span style="color: #339933;">,</span>
				version <span style="color: #339933;">:</span> tinymce.<span style="color: #660066;">majorVersion</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">+</span> tinymce.<span style="color: #660066;">minorVersion</span>
			<span style="color: #009900;">&#125;</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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Register plugin</span>
	<span style="color: #006600; font-style: italic;">// first parameter is the button ID and must match ID elsewhere</span>
	<span style="color: #006600; font-style: italic;">// second parameter must match the first parameter of the tinymce.create() function above</span>
	tinymce.<span style="color: #660066;">PluginManager</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'friendly_button'</span><span style="color: #339933;">,</span> tinymce.<span style="color: #660066;">plugins</span>.<span style="color: #660066;">buttonPlugin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This code does a couple of things: first, it registers a command that runs when our button is clicked. This command opens a modal window that contains the contents of the <em>button_popup.php</em> file. We have also set the dimensions of our window to be 220&#215;240 px. The second thing this code does is apply an image to our button, for aesthetics. And thirdly, this code sets up the meta data of our tinyMCE plugin button. Not overly important, but nice to have.</p>
<p>Recall above how I mentioned the ID of the button being <em>friendly_button</em>? Line 17 and 34 both use that same ID. It MUST not change, otherwise your button will not work.</p>
<p>Okay, we now have our button and it should look like this (though your image may be different):</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/buttons.png" alt="" title="buttons" width="496" height="96" class="alignnone size-full wp-image-8330" /></p>
<p>If you click the button now, a window will pop up, but it will display an &#8220;Object not found!&#8221; error. So we need to create the window contents now.</p>
<h3>The Modal Popup</h3>
<p>The contents of the modal window are nothing more than a PHP (or HTML) file. The file will have all of the necessary components of any other HTML page: HTML, HEAD, TITLE, and BODY tags. So let&#8217;s begin our file. The first part is the HEAD section. Place the following code into <em>button_popup.php</em>, which should be located in the root of your plugin folder.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">//</span> this file contains the contents of the popup window</span>
<span style="color: #009900;">?&gt;</span>
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Insert Button<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&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://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.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: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript&quot;</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;../../wp-includes/js/tinymce/tiny_mce_popup.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: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../wp-includes/js/tinymce/themes/advanced/skins/wp_theme/dialog.css&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;includes/css/friendly_buttons_tinymce.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>Aside from the normal HTML file stuff, such as declaring a DOCTYPE, I have also placed SCRIPT, STYLE, and LINK tags to load the necessary scripts. We need to load jQuery, the tinyMCE Pop Up file, a CSS file for tinyMCE, and lastly our custom CSS file, which will control the styling for the window contents.</p>
<p>Note that I have <em>not</em> used wp_enqueue_script() and wp_enqueue_style() to load our scripts here. This is because those will not work in this case. With a bit of tweaking, you could probably make them work, but for this tutorial we will not worry about that. Also note that the first three files are all located inside of the WordPress install and are not part of this plugin. We&#8217;re just making use of them.</p>
<p>Alright, we have two more parts to our button_popup.php file. One is the javascript that will take care of inserting the short code and its selected attributes into the post editor, and the second is the HTML form code for our short code attributes. Let&#8217;s do the javascript first.</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
45
46
47
48
49
50
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><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>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> ButtonDialog <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	local_ed <span style="color: #339933;">:</span> <span style="color: #0000ff;">'ed'</span><span style="color: #339933;">,</span>
	init <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ed<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ButtonDialog<span style="color: #339933;">.</span>local_ed <span style="color: #339933;">=</span> ed<span style="color: #339933;">;</span>
		tinyMCEPopup<span style="color: #339933;">.</span>resizeToInnerSize<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: #339933;">,</span>
	insert <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">function</span> insertButton<span style="color: #009900;">&#40;</span>ed<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Try and remove existing style / blockquote</span>
		tinyMCEPopup<span style="color: #339933;">.</span>execCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mceRemoveNode'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// set up variables to contain our input values</span>
		<span style="color: #000000; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog input#button-url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog input#button-text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> size <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog select#button-size'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">var</span> color <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog select#button-color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		 
		<span style="color: #000000; font-weight: bold;">var</span> style <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog select#button-style'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		 
		<span style="color: #000000; font-weight: bold;">var</span> align <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#button-dialog select#button-align'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>val<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		 
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> output <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// setup the output of our shortcode</span>
		output <span style="color: #339933;">=</span> <span style="color: #0000ff;">'[button '</span><span style="color: #339933;">;</span>
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'size='</span> <span style="color: #339933;">+</span> size <span style="color: #339933;">+</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'style='</span> <span style="color: #339933;">+</span> style <span style="color: #339933;">+</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'color='</span> <span style="color: #339933;">+</span> color <span style="color: #339933;">+</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">'align='</span> <span style="color: #339933;">+</span> align<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// only insert if the url field is not blank</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span>
				output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">' url='</span> <span style="color: #339933;">+</span> url<span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// check to see if the TEXT field is blank</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">']'</span><span style="color: #339933;">+</span> text <span style="color: #339933;">+</span> <span style="color: #0000ff;">'[/button]'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// if it is blank, use the selected text, if present</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			output <span style="color: #339933;">+=</span> <span style="color: #0000ff;">']'</span><span style="color: #339933;">+</span>ButtonDialog<span style="color: #339933;">.</span>local_ed<span style="color: #339933;">.</span>selection<span style="color: #339933;">.</span>getContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">'[/button]'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		tinyMCEPopup<span style="color: #339933;">.</span>execCommand<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mceReplaceContent'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Return</span>
		tinyMCEPopup<span style="color: #339933;">.</span>close<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: #339933;">;</span>
tinyMCEPopup<span style="color: #339933;">.</span>onInit<span style="color: #339933;">.</span>add<span style="color: #009900;">&#40;</span>ButtonDialog<span style="color: #339933;">.</span>init<span style="color: #339933;">,</span> ButtonDialog<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>The important things to know about this code, so that you can modify it for your own uses, are the <em>var</em> declarations and the <em>output</em> string. </p>
<p>Starting at line 15, we have a variable declared for each attribute of our short code: url, text, size, color, style, align. Each of these variables contain the value (using jQuery&#8217;s .val() function) of their respective form field, which we will be adding shortly.</p>
<p>Then, starting at line 22, we setup the <em>output</em> string. This variable will contain the final short code to be inserted into the editor. We start by adding <em>[button </em> to it. This is the beginning of our short code. Then we add each attribute so that it ends up like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>button size<span style="color: #339933;">=</span>SIZE style<span style="color: #339933;">=</span>STYLE color<span style="color: #339933;">=</span>COLOR align<span style="color: #339933;">=</span>ALIGN</pre></div></div>

<p>Next, we perform two IF checks. One to check if the URL field is empty, and if it&#8217;s not, we add the <em>url=URL</em> attribute. The second IF statement checks to see if the TEXT field is empty, and if it&#8217;s not, we close the short code, place the TEXT value inside of it, and then place the closing short code tag after that, so that the result looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>button size<span style="color: #339933;">=</span>SIZE style<span style="color: #339933;">=</span>STYLE color<span style="color: #339933;">=</span>COLOR align<span style="color: #339933;">=</span>ALIGN url<span style="color: #339933;">=</span>URL<span style="color: #009900;">&#93;</span>TEXT<span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span>button<span style="color: #009900;">&#93;</span></pre></div></div>

<p>If the TEXT field is empty, then we insert whatever text was selected (if any) when the user clicked the insert button, which would result in and output like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>button size<span style="color: #339933;">=</span>SIZE style<span style="color: #339933;">=</span>STYLE color<span style="color: #339933;">=</span>COLOR align<span style="color: #339933;">=</span>ALIGN url<span style="color: #339933;">=</span>URL<span style="color: #009900;">&#93;</span>SELECTED TEXT<span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span>button<span style="color: #009900;">&#93;</span></pre></div></div>

<p>That&#8217;s it for the jQuery, now we need to create the HTML form for our short code attributes.</p>
<h3>The Short Code Attributes Form</h3>
<p>This next piece of code will contain all of the form fields for our short code attributes. One of the important things to note is that the field IDs match those of the variables we declared for each attribute in our jQuery above. Also note that we are included in the closing HEAD, BODY, and HTML tags in this next snippet.</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
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-dialog&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;get&quot;</span> accept<span style="color: #339933;">-</span>charset<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-url&quot;</span><span style="color: #339933;">&gt;</span>Button URL<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-url&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-url&quot;</span> <span style="color: #339933;">/&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-text&quot;</span><span style="color: #339933;">&gt;</span>Button Text<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-text&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-text&quot;</span> <span style="color: #339933;">/&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-size&quot;</span><span style="color: #339933;">&gt;</span>Size<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-size&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-size&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;small&quot;</span><span style="color: #339933;">&gt;</span>Small<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;medium&quot;</span> selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;selected&quot;</span><span style="color: #339933;">&gt;</span>Medium<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;large&quot;</span><span style="color: #339933;">&gt;</span>Large<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-style&quot;</span><span style="color: #339933;">&gt;</span>Style<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-style&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-style&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;less_round&quot;</span><span style="color: #339933;">&gt;</span>Less Round<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;round&quot;</span> selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;selected&quot;</span><span style="color: #339933;">&gt;</span>Round<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;square&quot;</span><span style="color: #339933;">&gt;</span>Square<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-color&quot;</span><span style="color: #339933;">&gt;</span>Color<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-color&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-color&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;gray&quot;</span> selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;selected&quot;</span><span style="color: #339933;">&gt;</span>Gray<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">=&gt;</span>Blue<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #339933;">&gt;</span>Red<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;green&quot;</span><span style="color: #339933;">&gt;</span>Green<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;black&quot;</span><span style="color: #339933;">&gt;</span>Black<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-align&quot;</span><span style="color: #339933;">&gt;</span>Alignment<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-align&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button-align&quot;</span> size<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;gray&quot;</span> selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;selected&quot;</span><span style="color: #339933;">&gt;</span>None<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;left&quot;</span><span style="color: #339933;">=&gt;</span>Left<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
					<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;right&quot;</span><span style="color: #339933;">&gt;</span>Right<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>	
				<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript:ButtonDialog.insert(ButtonDialog.local_ed)&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;insert&quot;</span> style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;display: block; line-height: 24px;&quot;</span><span style="color: #339933;">&gt;</span>Insert<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>There&#8217;s really nothing complex about this code, it&#8217;s just a plain HTML form with either an INPUT or SELECT field for each of our short code options. First, notice that I have set default options by defining the VALUE attribute for input fields and SELECTED attribute for select fields. This helps to ensure that blank attributes are not inserted.</p>
<p>The next important thing about this code is at the bottom, where I have placed an anchor link with a bit of javascript for the HREF. This is vital to the modal window&#8217;s function. Without this HREF, the short code will not be inserted into the content.</p>
<h3>The Modal Window CSS</h3>
<p>Recall that a few steps back we loaded a CSS file called <em>friendly_buttons_tinymce.css</em>. This file controls the appearance of the HTML form fields we just created. But without any CSS in that file, it&#8217;s not doing very much ;) So here is the CSS:</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="css" style="font-family:monospace;"><span style="color: #cc00cc;">#button-dialog</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#button-dialog</span> div<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#button-dialog</span> label <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;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">8px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#button-dialog</span> select<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#button-dialog</span> input <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;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#button-dialog</span> select <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">112px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#button-dialog</span> <span style="color: #cc00cc;">#insert</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;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</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> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>This file is placed in the <em>includes/css</em> folder, along with the CSS file for the actual buttons.</p>
<p>That&#8217;s it! We&#8217;re done! Now, when you click the button in the TinyMCE button bar, you should be presented with this:</p>
<p><img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/window.png" alt="" title="window" width="629" height="402" class="alignnone size-full wp-image-8340" /></p>
<p>And, assuming you have done everything correctly, your short code should appear in your post editor as soon as you click the &#8220;Insert&#8221; button.</p>
<h3>Going Further</h3>
<p>So what&#8217;s next? It is entirely up to you. With the code provided in this tutorial, you have everything you need to add awesome, easy to use short codes to your WordPress theme or plugin. I believe I have done pretty well explaining the code, and making it easy to modify, but if you have any trouble, of course ask in the comments!</p>
<h3>Download the Complete Plugin</h3>
<p>If doing all the manual coding yourself isn&#8217;t quite your thing, you&#8217;re in luck! I have put the code from this tutorial into a complete plugin that you can download for free at my website: <a href="http://pippinsplugins.com/user-friendly-short-codes-plugin-example/">Pippin&#8217;s Plugins.com</a></p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/Pfst05HV5mk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/user-friendly-short-codes-with-tinymce/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/user-friendly-short-codes-with-tinymce/</feedburner:origLink></item>
		<item>
		<title>All New Design!</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/DObqNSa5zj8/</link>
		<comments>http://www.problogdesign.com/design/all-new-design/#comments</comments>
		<pubDate>Mon, 23 May 2011 15:36:42 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Pro Blog Design]]></category>
		<category><![CDATA[Redesign]]></category>
		<category><![CDATA[Typography]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8168</guid>
		<description><![CDATA[<img class="alignnone size-full wp-image-8171" title="Redesign" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/redesign.jpg" alt="Redesign" width="629" height="180" />

Here's something we've been working on for a while now; <strong>an all new design</strong>! If you're reading this in an RSS reader, please click through and <a href="http://www.problogdesign.com/">check it out</a>!

I'd love to hear your thoughts! Nicer than the old version?]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8171" title="Redesign" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/redesign.jpg" alt="Redesign" width="629" height="180" /></p>
<p>Here&#8217;s something we&#8217;ve been working on for a while now; <strong>an all new design</strong>! If you&#8217;re reading this in an RSS reader, please click through and <a href="http://www.problogdesign.com/">check it out</a>!</p>
<p>I&#8217;d love to hear your thoughts! Nicer than the old version?</p>
<h2>Goals for the Redesign</h2>
<p>We had some very clear <strong>goals</strong> in mind with this redesign:</p>
<ol>
<li>Greater emphasis on our <strong>design services</strong>.</li>
<li>Better integration of <strong>PliablePress links</strong>.</li>
<li>Improved &#8220;<strong>sectioning</strong>&#8221; (i.e. Services area vs. Blog area).</li>
</ol>
<p>Our design services are the bread and butter of the site, but until now, they&#8217;ve always just been one page that gets a link on different corners of the blog. We wanted to <strong>show that the two are equal</strong> in importance, rather than the services being just a subset of the blog.</p>
<p>The new homepage is intended to be a big part of that, with a lot of real estate given to the Services and PliablePress, not just the blog. And it gave us the chance to give a proper <strong>overview of what the site does</strong> to first time visitors with our headline:</p>
<p><img class="alignnone size-full wp-image-8169" title="home-headline" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/home-headline.jpg" alt="" width="629" height="77" /></p>
<p>In terms of the actual design style; we obviously wanted to make the site look nicer, but that&#8217;s a little too general to be a real goal. So our goal was to do this in a way that <strong>brought more color into the site</strong> and gave a very unique and memorable look.</p>
<p>This choice of style is purely because we&#8217;re a design team, so our site needs to showcase what we can do <strong>in a creative way</strong>.</p>
<h2>Typography</h2>
<p>Improving some of our typography was one of the <strong>main reasons for the redesign</strong> (To be more specific, getting rid of the fonts I&#8217;d used on our post titles was <em>the</em> main reason I was dying to redesign). In the new site, I think we&#8217;ve done that well.</p>
<p>Our fonts are now <strong>powered by Typekit</strong>, so we had a great selection of stunning fonts at our disposal. We&#8217;re using the lovely <a href="http://typekit.com/fonts/ff-meta-web-pro">FF Meta Web Pro</a> for a number of titles around the site, and <a href="http://typekit.com/fonts/sorts-mill-goudy">Sorts Mill Goudy</a> for various buttons (And our post titles are in good old-faithful Arial).</p>
<p>Aside from font selection, another thing I&#8217;m happy to have improved is our<strong> bullet points</strong>. We use them a lot in articles, but they vary from only a few words per bullet, to a few lines. That made spacing the bullets out quite awkward.</p>
<ul>
<li>But now, at last</li>
<li>we have a nicer design</li>
<li>that can handle either.</li>
</ul>
<p>Yay!</p>
<h2>Advertising</h2>
<p>The last thing to mention is the 125 by 125 ads in the blog sidebar. A little over a year ago I took all ads off this blog (Largely to make room for PliablePress banners), but now that we&#8217;ve built the PliablePress links more effectively into the design, it leaves plenty of room in the sidebar for <strong>some tasteful square ads again</strong>.</p>
<p>So with that in mind, if you&#8217;d like to advertise here, you can do on our <a href="http://buysellads.com/buy/detail/1611/zone/1260116">BuySellAds page</a>. The current cost is <strong>$99 for 30 days</strong> (The ads aren&#8217;t on the homepage, so it will work out around 120,000 pageviews from 80,000 uniques). And as a little bonus, I&#8217;ll <strong>tweet/Facebook</strong> the first 4 advertisers who join us (Around 10,000 combined followers).</p>
<h2>How Did We Do?</h2>
<p>Naturally, everything I&#8217;ve said above is just what I <em>hoped</em> the redesign will do. The real test is how it actually does. I&#8217;d love to hear your thoughts on it. <strong>Anything we did well? Or poorly?</strong> Better or worse than our previous?</p>
<p>And if you spot any bugs, please do let me know!</p>
<p><em>PS &#8211; We&#8217;re still putting on the last layer of polish, so you may notice some area not quite complete yet (e.g. the portfolio listing just one site right now! ;) )</em></p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/DObqNSa5zj8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/design/all-new-design/feed/</wfw:commentRss>
		<slash:comments>91</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/design/all-new-design/</feedburner:origLink></item>
		<item>
		<title>8 Tips to Secure a WordPress Site</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/mbfEsZdOkyo/</link>
		<comments>http://www.problogdesign.com/wordpress/tips-to-secure-a-wordpress-site/#comments</comments>
		<pubDate>Tue, 10 May 2011 13:52:36 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Competition]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=8025</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/appsumo.jpg" alt="Appsumo &#38; Sucuri.net Deal" title="Appsumo &#38; Sucuri.net Deal" width="560" height="145" class="alignnone size-full wp-image-8032" />

Security is one of those topics that comes up time and time again. You hear the advice, you <strong>know you should do it</strong>, but somehow, you never get around to it. 

The tips in this post are designed to help make your WordPress installation safer and less likely <strong>to be hacked or spammed</strong>. If you're short on time, just start at the top and work down. I've (roughly) ordered them so that the most important tweaks come first.

We also have a great giveaway at the end of this post, from <a href="http://sucuri.net/">Sucuri.net</a> and <a href="http://www.appsumo.com/">Appsumo</a>. Check it out for the chance to win <strong>free site security monitoring for a year</strong>!]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-8032" title="Appsumo &amp; Sucuri.net Deal" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/appsumo.jpg" alt="Appsumo &amp; Sucuri.net Deal" width="560" height="145" /></p>
<p>Security is one of those topics that comes up time and time again. You hear the advice, you <strong>know you should do it</strong>, but somehow, you never get around to it.</p>
<p>The tips in this post are designed to help make your WordPress installation safer and less likely <strong>to be hacked or spammed</strong>. If you&#8217;re short on time, just start at the top and work down. I&#8217;ve (roughly) ordered them so that the most important tweaks come first.</p>
<p>We also have a great giveaway at the end of this post, from <a href="http://sucuri.net/">Sucuri.net</a> and <a href="http://www.appsumo.com/">Appsumo</a>. Check it out for the chance to win <strong>free site security monitoring for a year</strong>!</p>
<h2>1 &#8211; Set up Backups</h2>
<p>This is quite simply a requirement. If you don&#8217;t have backups, you&#8217;re putting all your eggs in one basket and if something goes wrong, you lose everything. You&#8217;d have to be mad to risk this&#8230;</p>
<p>There are 3 rules for a good backup:</p>
<ol>
<li>It <strong>can&#8217;t be saved on your server</strong> (Because then if your server dies, so do your backups).</li>
<li>It needs to be <strong>automatic</strong> (Because you aren&#8217;t going to remember to do this yourself every day.</li>
<li>You need to save <strong>a number of them</strong> (e.g. if you only save the past 5 backups, and then go on holiday for a week, your backups could all be useless by the time you get home).</li>
</ol>
<p>Thankfully, they&#8217;re easy to do. Here are 3 possible ways:</p>
<ul>
<li><a href="http://www.problogdesign.com/wordpress/the-perfect-hands-free-database-backup/">Automatic Database Backups to Email</a></li>
<li><a href="http://www.problogdesign.com/how-to/automatic-amazon-s3-backups-on-ubuntu-debian/">Automatic Amazon s3 Backups</a> (For VPS users)</li>
<li><a href="http://vaultpress.com/">VaultPress</a> &#8211; Paid service from Automattic to make handling backups simple.</li>
</ul>
<h2>2 &#8211; Always Update WordPress</h2>
<p>A fairly common thought used to be that &#8220;upgrading WordPress might break my plugins.&#8221; And it was true; upgrading WordPress <strong>does carry that risk</strong>.</p>
<p><strong>Not upgrading is worse</strong>; it guarantees that you&#8217;re at risk. Almost every WordPress release that comes out has security fixes as part of it. If you don&#8217;t update, then by definition, you&#8217;re leaving those holes open.</p>
<p>At the very least, you can always do minor updates, e.g. 3.0.1 to 3.0.2. They&#8217;re usually solely for security and bugs, <strong>so always update right away when one comes out</strong>.</p>
<p>As an aside; it&#8217;s possible to <strong>hide the version</strong> of WordPress you&#8217;re using by adding this to your <em>functions.php</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">remove_action<span style="color: #009900;">&#40;</span>‘wp_head’<span style="color: #339933;">,</span> ‘wp_generator’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s no substitute for not upgrading though (And if they can load your login page, quite often the design is a dead giveaway anyway).</p>
<h2>3 &#8211; Choose a Better Password</h2>
<p>I know you hear this all the time, but crappy passwords are still the <strong>#1 reason for sites being hacked</strong>. How do you know if your password sucks?</p>
<p>Easy. Does it make any sense whatsoever as a word/phrase/name? If you can pronounce it at all, it&#8217;s probably bad. Your password should be a completely <strong>random, meaningless set of letters and numbers</strong>.</p>
<p>And the meaningless part is important there. Turning <strong>&#8220;michael&#8221; into &#8220;m1ch@el&#8221; doesn&#8217;t make it secure</strong>, it just means the hacker will get it on their second guess instead of their first.</p>
<h2>4 &#8211; Check Your Site Regularly</h2>
<p>You&#8217;d be surprised how often your site can be compromised <strong>without you even knowing</strong>. For instance, a lot of unwanted scripts will hide links all over your pages that only search engines can see.</p>
<p>It&#8217;s crucial that you <strong>catch on fast</strong> because once Google penalizes you, getting your rankings back is a slow, painful process.</p>
<p><img class="alignright size-full wp-image-8038" title="Security" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/sucuri.jpg" alt="Security" width="200" height="85" />One way to check is to use tools like <a href="https://www.google.com/webmasters/tools/home?hl=en">Google Webmaster Tools</a> to see what <strong>links Google is finding</strong> on your pages.</p>
<p>Another option is to use a tool like <a href="http://sucuri.net/">Sucuri.net</a> (Which we&#8217;re giving away at the end of this post!), which will <strong>automatically scan your site regularly</strong> and alert you to any issues.</p>
<h2>5 &#8211; Prevent Directory Indexing</h2>
<p>Directory indexing means that if someone loads a folder on your site (e.g. wp-content/plugins/), they will get a list of all the files in it. This is bad because then a hacker <strong>can see everything on your server</strong>.</p>
<p>The fix is easy, open up your <em>.htaccess</em> file in a text editor and add the following to it:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">#Prevent directory indexing</span>
<span style="color: #00007f;">Options</span> -<span style="color: #0000ff;">Indexes</span></pre></div></div>

<h2>6 &#8211; Don&#8217;t use FTP</h2>
<p>FTP is the most common way to add files to your server, but it isn&#8217;t secure. Someone <strong>can intercept your transmissions</strong> and even your access details. If your host allows you to access your server with SSH, then you can use SFTP instead.</p>
<p>Behind the scenes the two work completely differently, but <strong>using them is exactly the same</strong>. Odds are you can even continue to use your current FTP program with SFTP instead.</p>
<p>There is no reason not to make this swap, so <strong>contact your host</strong> and ask them if you can use SFTP (Most should allow it).</p>
<h2>7 &#8211; Move and Update Your WP-Config.php File</h2>
<p>Your <em>wp-config.php</em> file doesn&#8217;t have to be in your site&#8217;s root folder (Where it is by default). You can move it up a folder (So it is <strong>no longer in your public web folders at all</strong>).</p>
<p>And it&#8217;s as easy as it sounds. Just move the file up one folder, and <strong>WordPress will know to find it there</strong>.</p>
<p>Also, if you&#8217;ve <strong>had WordPress for a while now</strong>, you may not have all the security keys that you should in your wp-config.php file. These keys help encrypt your passwords and other details. All you need to do is go to <a href="https://api.wordpress.org/secret-key/1.1/salt/">https://api.wordpress.org/secret-key/1.1/salt/</a>, copy the code it generates, and then paste it into the relevant section of your wp-config.php file.</p>
<h2>8 &#8211; Take Care Downloading Plugins/Themes</h2>
<p>There are a lot of places to find these items, but not all of them are trustworthy. Many sites get caught out because they installed a script <strong>with malicious code hidden in it</strong>.</p>
<p>Before downloading a theme or plugin, Google the site to see what people say about it. Lots of good reviews and links? Then go for it. Hard to find any? Then best leave it alone.</p>
<p>For plugins; there is usually little reason for a free plugin not to be <strong>hosted on WordPress.org</strong>, so always go there first (There are still exceptions though, e.g. <a href="http://www.deliciousdays.com/cforms-plugin/">cforms</a>). Or alternatively, you can buy premium plugins from sites you trust (Again though, check the site first!).</p>
<p>Themes are trickier. The WordPress.org theme directory is great, but <strong>a lot of talented theme developers do choose to host their themes on their own sites</strong> (The auto-update feature of the plugin directory isn&#8217;t so necessary with themes).</p>
<p>If you do take a theme from a 3rd party website, I&#8217;d recommend <a href="http://sitecheck.sucuri.net/scanner/">scanning it</a> right away (Spammy <strong>paid links in the footer</strong> of a theme are a very common find that Google may penalize you for).</p>
<p>And of course, you always have the option of paying for premium themes (Though do be careful of sites selling themes they did not make. You have no guarantee that they haven&#8217;t <strong>tampered with the theme first</strong>).</p>
<h2>Win a Year&#8217;s Free Security Scanning</h2>
<p><img class="alignright size-full wp-image-8036" title="Appsumo" src="http://cdn.problogdesign.com/wp-content/uploads/2011/05/app2.jpg" alt="Appsumo" width="200" height="154" /><a href="http://www.appsumo.com/">Appsumo</a> and <a href="http://sucuri.net/">Sucuri.net</a> have kindly offered us 2 one-year licenses to their WordPress scanning tools to give away. Sucuri will automatically scan your website and notify you if you if it spots any issues. Better than that, <strong>they can help resolve them too</strong>.</p>
<p>To enter the competition, just <strong>tweet the following</strong>:</p>
<p><em><strong>Update (18th May)</strong> &#8211; The competition is now over and the winners have been chosen (@ezyblogger and @wpmodder). We&#8217;ll more again soon!</em></p>
<blockquote><p>I&#8217;d like to win a year of Sucuri website monitoring &amp; malware protection on @problogdesign via @appsumo http://bit.ly/j95q8Z</p></blockquote>
<p>Until midnight tonight only, you can also buy Sucuri&#8217;s Business plan for only <a href="http://www.appsumo.com/sucuri-promo/">$85 for the year on Appsumo</a> (Down from $289!). AppSumo gives you a guaranteed refund, no questions asked, so if you buy it and end up winning here, you <strong>can get your money back anyway</strong>!</p>
<p>Winner will be chosen and emailed <strong>this weekend</strong>, so good luck!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/mbfEsZdOkyo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/tips-to-secure-a-wordpress-site/feed/</wfw:commentRss>
		<slash:comments>105</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/tips-to-secure-a-wordpress-site/</feedburner:origLink></item>
		<item>
		<title>How to Get Facebook, Twitter, and RSS Counts {WP}</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/dPzrjbdt5Ws/</link>
		<comments>http://www.problogdesign.com/wordpress/how-to-get-facebook-twitter-and-rss-counts-wp/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 22:51:22 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Transients]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=7900</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/counts.jpg" alt="Twitter, Facebook, RSS Counts" title="Twitter, Facebook, RSS Counts" width="560" height="145" class="alignnone size-full wp-image-7907" />

You will have seen blogs showcase their Twitter, Facebook, and RSS counts with typography. With these numbers in plain text, you can <strong>style them into your website</strong> far more effectively than if you were to use the old-fashioned "chicklets."

Twitter and Facebook make this easy, but you'll need to take an extra step with FeedBurner first. Log in, then click <strong>Publicize > Awareness API</strong>, and click activate.

All set? Then let's get started!]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-7907" title="Twitter, Facebook, RSS Counts" src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/counts.jpg" alt="Twitter, Facebook, RSS Counts" width="560" height="145" /></p>
<p>You will have seen blogs showcase their Twitter, Facebook, and RSS counts with typography. With these numbers in plain text, you can <strong>style them into your website</strong> far more effectively than if you were to use the old-fashioned &#8220;chicklets.&#8221;</p>
<p>Twitter and Facebook make this easy, but you&#8217;ll need to take an extra step with FeedBurner first. Log in, then click <strong>Publicize &gt; Awareness API</strong>, and click activate.</p>
<p>All set? Then let&#8217;s get started!</p>
<p><img class="alignright size-full wp-image-7908" title="Chicklets" src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/chicklets.jpg" alt="Chicklets" width="256" height="88" />(And yes, I know we&#8217;re still using the chicklets here, but they&#8217;ll be gone for good in a few more days so just ignore that for now!)</p>
<h2>Planning Our Script</h2>
<p>When we get a number from one of these services, we will need to <strong>save it to our server</strong>.</p>
<p>This is so we can retrieve it from there without having to go out to the service again (Otherwise, the service might cut us off for requesting it too often, and it&#8217;s <strong>faster</strong> this way too).</p>
<p>Regular readers will know where this is heading; the <strong>transients API</strong>. Transients are WordPress&#8217; way of letting you store information for a limited time (And refreshing it later when you need).</p>
<p>In previous posts where we have used transients though, we have merged the transient code with the code to get our information. This time, we are going to split the two up (So we don&#8217;t end up <strong>re-writing the transient code each time</strong>).</p>
<p>We will have <strong>one function</strong> that stores/retrieves a transient, and then a function for each service to get the number we need.</p>
<h2>1 &#8211; Our Transient Function</h2>
<p>For a full <strong>introduction to transients</strong>, you can read our <a href="http://www.problogdesign.com/wordpress/use-the-transients-api-to-list-the-latest-commenter/">Transients API tutorial</a>. I&#8217;ll summarize what the following code is doing afterwards though, so don&#8217;t worry if you don&#8217;t follow it right away.</p>
<p>Open up your theme&#8217;s <em>functions.php</em> file, and paste the following between the opening and closing <!--?php and ?--> tags:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Retrieve transient value.
 *
 * Returns a transient value, or updates that transient if it has expired.
 *
 * @param $name String. Name of the service, e.g. twitter
 * @return String. New transient value.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'twitter'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$transName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pbd-transient-<span style="color: #006699; font-weight: bold;">$name</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Name of value in database.</span>
	<span style="color: #000088;">$cacheTime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Time in hours between updates.</span>
&nbsp;
	<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;">$trans</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;">// Make the name of the function that gets our transient value.</span>
		<span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pbd_get_<span style="color: #006699; font-weight: bold;">$name</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$trans</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$func</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$trans</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Function not found.'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Did we get a positive number? If not, use the old value.</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>absint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$trans</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$trans</span> <span style="color: #339933;">=</span> get_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$transName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'-old'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Save our new transient, plus save it in the longer &quot;backup&quot; transient.</span>
		set_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$transName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$trans</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</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>
		set_transient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$transName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-old'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$trans</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</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>
	<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$trans</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now let&#8217;s break down what this is doing:</p>
<ul>
<li>The <em>pbd_get_transient()</em> expects 1 parameter, the name which makes our transient and <strong>&#8220;get&#8221; function names</strong> (In our case, &#8216;twitter&#8217;, &#8216;facebook&#8217;, or &#8216;rss&#8217;, though you could use it for anything else as well).</li>
<li>We set a <strong>cache time</strong> (8 hours), and on line 14, we check if a value is already saved. If it is, we just return it.</li>
<li>Line 17 is where it gets interesting. We take the name that was passed to the function, and <strong>build a function name from it</strong>, e.g. &#8216;twitter&#8217; becomes &#8216;pbd_get_twitter&#8217;. If the function pbd_get_twitter() exists, we will then call it and save the value it gives (Lines 18 and 19).</li>
<li>On line 25, we check the value we have. It should be a positive number. If it isn&#8217;t, odds are that the service didn&#8217;t respond to us, so we will <strong>use the value we saved the last time</strong>.</li>
<li>On line 29, we save the new transient value, and save it again on line 30 with a longer time limit (So we can use it as a <strong>backup if we need to</strong>).</li>
</ul>
<p>And that&#8217;s our caching taken care of. We can now <strong>re-use this same function</strong> for each of our services. Much better, isn&#8217;t it?</p>
<h2>2 &#8211; Get the Numbers</h2>
<p>The next step is to go to each service and <strong>get the value we want</strong>. Thankfully, they&#8217;ve all made it very easy with their APIs.</p>
<p>Let&#8217;s start with <strong>Facebook</strong>, add the following to your <em>functions.php</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Get Facebook Page Fan Count.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_get_facebook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> wp_remote_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://graph.facebook.com/problogdesign&quot;</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>is_wp_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$fbData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</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: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fbData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'likes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>On line 5, you would replace &#8216;problogdesign&#8217; with <strong>your Facebook Page&#8217;s name</strong> of course (You can load the URL directly in your browser to make sure you have it right, e.g. <a href="http://graph.facebook.com/problogdesign">click here</a>).</p>
<p>With line 5, <strong>WordPress will grab the file</strong>. We check that it did this successfully on line 7 (If it didn&#8217;t, then our previous function will know to use its backup value).</p>
<p>With line 10, we <a href="http://php.net/manual/en/function.json-decode.php">convert the JSON document</a> we downloaded into a regular PHP array, and last of all, we pick out <strong>just the fan count</strong> and return it (Making sure it&#8217;s an integer).</p>
<p><strong>Twitter</strong> is almost identical:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Get Twitter follower count.
 *
 * @return Int. Twitter follower number.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_get_twitter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$json</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/users/show.json?screen_name=problogdesign&quot;</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>is_wp_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$twitterData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</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: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twitterData</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'followers_count'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, all we have done is <strong>changed some of the names</strong>. The method is identical, just replace the problogdesign part with your Twitter username.</p>
<p>Now, let&#8217;s do <strong>FeedBurner</strong>. The method here is similar, but instead of JSON, FeedBurner only offers an <strong>XML format</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Get RSS subscriber count from FeedBurner.
 *
 * @return Int. RSS follower number.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_get_rss<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> wp_remote_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=problogdesign'</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>is_wp_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$sxe</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sxe</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'circulation'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We start out the same, by grabbing a file from the service (And again, <strong>replace the problogdesign</strong> part with the name of your feed).</p>
<p>The difference is on line 10, where we use PHP&#8217;s <a href="http://www.php.net/manual/en/book.simplexml.php">SimpleXML extension</a> to get the data we need. And once again, we pick out only the subscriber number to return, and <strong>make sure it&#8217;s an integer</strong>.</p>
<h2>3 &#8211; Display the Numbers</h2>
<p>You&#8217;ve now written the 4 functions to get each number and save it. All that&#8217;s left to do is <strong>print it out</strong> on your site.</p>
<p>Browse to wherever you want to list your numbers (e.g. <em>sidebar.php</em>), and insert the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span>pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rss'</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></pre></div></div>

<p>We call <em>pbd_get_transient(&#8216;rss&#8217;)</em> to return the value, and I&#8217;ve wrapped it in <a href="http://php.net/manual/en/function.number-format.php">number_format()</a> so that <strong>commas will be added</strong> if the number is in the thousands.</p>
<p>Similarly, for <strong>Facebook</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span>pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'facebook'</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></pre></div></div>

<p>And <strong>Twitter</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span>pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'twitter'</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></pre></div></div>

<h2>4 &#8211; Display a Combined Total</h2>
<p>We could have applied number_format() earlier on, before we saved the values. The reason to <strong>store the plain integers instead</strong> is so you can add the 3 numbers together and display a combined total, if you like.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rss'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'twitter'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> pbd_get_transient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'facebook'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sum</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And that&#8217;s us all done! Now you (and I) have no more excuses for using the buttons they give us. Instead, <strong>just well designed typography</strong>.</p>
<p><strong>Update (29th July &#8217;11)</strong> &#8211; Thanks to <a href="http://bryanhadaway.com/">Bryan Hadaway</a> for pointing out &#8216;&amp;gt;&#8217;s in the code!</p>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/dPzrjbdt5Ws" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/how-to-get-facebook-twitter-and-rss-counts-wp/feed/</wfw:commentRss>
		<slash:comments>114</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/how-to-get-facebook-twitter-and-rss-counts-wp/</feedburner:origLink></item>
		<item>
		<title>Add a jQuery DatePicker to Event Posts {WP}</title>
		<link>http://feedproxy.google.com/~r/ProBlogDesign/~3/KzlgNZhV6fM/</link>
		<comments>http://www.problogdesign.com/wordpress/add-a-jquery-datepicker-to-event-posts-wp/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 16:00:58 +0000</pubDate>
		<dc:creator>Michael Martin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Calendar]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.problogdesign.com/?p=7553</guid>
		<description><![CDATA[<img src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/events-datepicker.jpg" alt="jQuery Events DatePicker in WordPress" title="jQuery Events DatePicker in WordPress" width="560" height="145" class="alignnone size-full wp-image-7564" />

In previous posts, we set up an <strong>events system for WordPress</strong>. Users could enter the date of an event, and we had great archives for displaying and categorizing them.

Today, we want to <strong>improve the admin end of things</strong>. Instead of using custom fields, we'll make a box for the event's date, and use jQuery to let users pick the date from a calendar.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-7564" title="jQuery Events DatePicker in WordPress" src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/events-datepicker.jpg" alt="jQuery Events DatePicker in WordPress" width="560" height="145" /></p>
<p>In previous posts, we set up an <strong>events system for WordPress</strong>. Users could enter the date of an event, and we had great archives for displaying and categorizing them.</p>
<p>Today, we want to <strong>improve the admin end of things</strong>. Instead of using custom fields, we&#8217;ll make a box for the event&#8217;s date, and use jQuery to let users pick the date from a calendar.</p>
<p>This tutorial is a self-contained post, which is useful on its own <strong>whether or not you&#8217;ve read the previous posts</strong>. But if you haven&#8217;t and would like to, you can here: (Or there&#8217;s a shortcut afterwards)</p>
<ol>
<li><a href="http://www.problogdesign.com/wordpress/events-list-with-custom-post-types-and-taxonomies/">Setup an Events List</a> (Using Custom Post Types and Taxonomies)</li>
<li><a href="http://www.problogdesign.com/wordpress/make-a-template-for-taxonomies-post-type-archives/">Making the Template</a> (for our Post Types &amp; Taxonomies)</li>
</ol>
<p>If you want <strong>to skip on though</strong>, <a href="http://www.problogdesign.com/uploads/other/events-list/events.phps">grab this file</a>, rename it to <strong>events.php</strong>, and upload it to your theme folder. Then add this line to your theme&#8217;s <em>functions.php</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Events</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'events.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Everything we do in this tutorial will be added to that file, so you&#8217;re good to go now!</p>
<h2>Demo / Download</h2>
<p>So what does <strong>the end result</strong> look like? Well, the meta box itself will appear right below your post editor on Events, like this:</p>
<p><img class="alignnone size-full wp-image-7558" title="Date Meta Box Demo" src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/demo-metabox.jpg" alt="Date Meta Box Demo" width="560" height="272" /></p>
<p>And when you click in the box, you&#8217;ll <strong>see this datepicker calendar</strong>:</p>
<p><img class="alignnone size-full wp-image-7559" title="DatePicker Demo" src="http://cdn.problogdesign.com/wp-content/uploads/2011/04/demo-datepicker.jpg" alt="DatePicker Demo" width="560" height="272" /></p>
<p>The completed file can be found here: <a href="http://www.problogdesign.com/uploads/other/events-list/events-datepicker.phps">events-datepicker.phps</a> (<strong>Rename it</strong> to events.php, and add the same include() line from this post&#8217;s intro to your functions.php file).</p>
<p>Also, <a href="http://www.problogdesign.com/uploads/other/events-list/jquery-ui-datepicker.zip">grab this folder</a>, unzip it, and upload it to your theme folder as well. This holds the jQuery elements we need, which you can download <a href="http://jqueryui.com/download">from jQuery UI itself here</a> (For convenience though, I&#8217;ve just picked out the files we need and zipped them up here).</p>
<h2>1 &#8211; Create a Meta Box</h2>
<p>Right, let&#8217;s start the actual tutorial!</p>
<p>The first thing I want to talk about is <strong>meta boxes</strong>. On your &#8220;Add Post&#8221;, &#8220;Add Page&#8221; etc. screens, you see a series of boxes. There&#8217;s one for the post itself, one for choosing a category, one for custom fields etc.</p>
<p>A meta box is just another box like these. <strong>Specifically, it&#8217;s for custom fields</strong>. Instead of always having to type in a name and a value, which isn&#8217;t very user-friendly, you can add a meta box of your own to make it much simpler. Behind the scenes though, the end result is the same, you&#8217;re still just saving some metadata about the post.</p>
<p>In previous posts in this series, we used a &#8220;Date&#8221; custom field to save the date of the event. We&#8217;re now going to improve it so that the user gets a nice <strong>box to enter their Date custom field in</strong> (Like you saw in the screenshots above).</p>
<p>So now that you know what a meta box is, <strong>how do you add one?</strong> There are 3 stages to it:</p>
<ol>
<li>Tell WordPress about your box.</li>
<li>Write the HTML for the box.</li>
<li>Save the date.</li>
</ol>
<p>WordPress makes this all very easy to do. Here goes!</p>
<h4>1 &#8211; Tell WordPress About Your Box</h4>
<p>Open your <em>events.php</em> file (Downloadable at the start of this post, or just pick up right where we left off in the last post!), and <strong>before the closing ?> tag</strong>, paste the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Register meta box
 *
 * Adds a metabox to Event post types, allowing users to easily enter the date of an event.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_events_meta_box<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	add_meta_box<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pbd-events-meta-box'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Event Date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_create_meta_box'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'events'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'normal'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'high'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'add_meta_boxes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_meta_box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We use the <a href="http://codex.wordpress.org/Function_Reference/add_meta_box">add_meta_box() function</a> to <strong>tell WordPress about the box</strong>. The first parameter is just a CSS class, and the last 2 are to do with where to position the box. The important ones are the 2nd, 3rd, and 4th:</p>
<ul class="nospace">
<li>&#8216;Event Date&#8217; &#8211; The <strong>title</strong> shown on the box.</li>
<li>&#8216;pbd_events_create_meta_box&#8217; &#8211; The <strong>function that creates the HTML</strong>. We&#8217;re going to create this next.</li>
<li>&#8216;events&#8217; &#8211; The <strong>type of post</strong> that this box should appear on. We don&#8217;t need it on posts/pages, just on our &#8216;events&#8217; post type.</li>
</ul>
<p>That line is wrapped in a function, which <strong>hooks into the &#8216;add_meta_boxes&#8217; action</strong>, so WordPress knows to add our box when its needed.</p>
<h4>2 &#8211; Write the HTML for the Box</h4>
<p>WordPress takes care of most of the HTML for us. The divs etc. for the box and the title are taken care of. All we need to do is <strong>add the text input</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">/**
 * Creates the HTML for the meta box.
 */
function pbd_events_create_meta_box($post) {
	// Get already-entered date.
	$date = get_post_meta($post-&gt;ID, 'Date', true);
&nbsp;
	// Nonce for verification.
	wp_nonce_field( plugin_basename(__FILE__), 'pbd_events_nonce');
	?&gt;
&nbsp;
Date (mm/dd/yy):
&lt;input id=&quot;pbd-event-date&quot; name=&quot;pbd-event-date&quot; type=&quot;text&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$date</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, we&#8217;ve given this function <strong>the name we specified</strong> in add_meta_box(). WordPress also sends it the current <a href="http://www.problogdesign.com/wordpress/wordpress-post-term-and-comment-object-cheat-sheet/">$post object</a>, which we use next.</p>
<p>On line 6, we get the <strong>previously saved Date value</strong> (if there was one) to show in our text field. On line 9, we use the <a href="http://codex.wordpress.org/Function_Reference/wp_nonce_field">wp_nonce_field()</a> function to add some verification fields that we will use to secure our save function later.</p>
<p>And lastly, on line 12, we add the <strong>simple text input</strong> in a paragraph.</p>
<h4>3 &#8211; Save the Date</h4>
<p>The main issue with saving, as you might expect, is <strong>security</strong>. So the bulk of our work here is making sure that we actually should save it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Saves the meta box value when the post is saved.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_events_save_meta_box<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Verification check.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>wp_verify_nonce<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pbd_events_nonce'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> plugin_basename<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	      <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// And they're of the right level?</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>current_user_can<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit_posts'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Has the field been used?</span>
	<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pbd-event-date'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<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;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Validate that what was entered is of the form: 00/00/00</span>
	<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;">'(^\d{1,2}\/\d{1,2}\/\d{2}$)'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		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;">'Date'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</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;">'save_post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_save_meta_box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As always, the format here is to create a function, and then hook it into place. In this case, the &#8216;save_post&#8217; hook, which runs <strong>each time WordPress saves a post</strong>.</p>
<p>We start by using the nonce fields we added in step 2. In a nutshell, a nonce makes sure that the user performing this action actually meant to do it (And <strong>wasn&#8217;t tricked into it</strong>). You can read more about them <a href="http://markjaquith.wordpress.com/2006/06/02/wordpress-203-nonces/">on Mark Jaquith&#8217;s blog</a>.</p>
<p>Next, on line 11, we make sure they have the right capabilities. And the last check is on lines 15-16, where we check if a Date has actually been entered (So we <strong>don&#8217;t do anything on regular posts/pages</strong>). This has the slight downside that to delete the date info altogether, users will need to use the &#8220;Custom Fields&#8221; metabox again, but given the purpose of our script, that&#8217;s not likely to be an issue!</p>
<p>We&#8217;ve now <strong>verified</strong> that the user saving the data is allowed to. The next step is checking that <strong>what they entered is valid</strong>.</p>
<p>On line 20, we <strong>use regular expressions to do this</strong>. Our events list requires the date to be in the format: mm/dd/yy, so we verify the Date entered by checking that it&#8217;s in the format 00/00/00. If it&#8217;s not, then it won&#8217;t be saved.</p>
<p>Finally, on line 21, after all of our checks, we can use the <a href="http://codex.wordpress.org/Function_Reference/update_post_meta">update_post_meta() function</a> to <strong>save the new Date</strong>.</p>
<h2>2 &#8211; The jQuery DatePicker</h2>
<p>Your box is now fully functional and working. Next, we just want to <strong>enhance it with an easy-to-use DatePicker</strong>, to make the admin&#8217;s life easier.</p>
<p>Thankfully, <strong>the jQuery UI project</strong> has an awesome datepicker script that we can use. Check out its <a href="http://jqueryui.com/demos/datepicker/">demo page here</a>.</p>
<p>To download the files from jQuery UI itself, you can <a href="http://jqueryui.com/download">go here</a>, tick all of the &#8220;Core&#8221; boxes, and the &#8220;DatePicker&#8221; widget. Then in the right-hand column, choose <strong>the &#8220;Smoothness&#8221; theme</strong>, then hit download.</p>
<p>The resulting package contains quite a few files though, so for simplicity sake, <strong>I&#8217;ve picked out the ones you need</strong> and uploaded them in a <a href="http://www.problogdesign.com/uploads/other/events-list/jquery-ui-datepicker.zip">zip file here</a>. Download that, unzip it, and save the whole folder (&#8220;jquery-ui-datepicker&#8221;) into your theme.</p>
<p>The setup is then quite simple. We need to add the JavaScript and CSS files to the admin page, and initiate it on our form field. We&#8217;ll <strong>start with this last step</strong>.</p>
<p><strong>If you downloaded the zip folder I put together</strong>, this simple JavaScript file has already been made for you. If not, simply save the 3 lines below into a file called <em>pbd-datepicker.js</em> in the datepicker folder.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery<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: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;#pbd-event-date&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> dateFormat<span style="color: #339933;">:</span> <span style="color: #3366CC;">'mm/dd/y'</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></pre></div></div>

<p>We wait for the page to be fully loaded, then call the .datepicker() constructor <strong>on our &#8220;pbd-event-date&#8221; text input</strong> (We added that ID to it back in step 2 of the first section).</p>
<p>DatePicker takes a number of options, but the one we need, dateFormat:, allows us to <strong>set how it should enter the date</strong> in our text box. This way, it matches our mm/dd/yy pattern exactly.</p>
<p>Now, all that&#8217;s left to do is <strong>load our JS and CSS files</strong>. Let&#8217;s start with the JavaScript. Add the following to your <em>events.php</em> file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Adds a jQuery datepicker script to Event pages.
 * http://jqueryui.com/demos/datepicker/
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_events_jquery_datepicker<span style="color: #009900;">&#40;</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-ui-datepicker'</span><span style="color: #339933;">,</span>
		get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/jquery-ui-datepicker/jquery-ui-1.8.11.custom.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: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	wp_enqueue_script<span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'pbd-datepicker'</span><span style="color: #339933;">,</span>
		get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/jquery-ui-datepicker/pbd-datepicker.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: #339933;">,</span> <span style="color: #0000ff;">'jquery-ui-datepicker'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_print_scripts-post-new.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_jquery_datepicker'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_print_scripts-post.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_jquery_datepicker'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script">wp_enqueue_script() function</a> is a safe way to add JavaScript files to WordPress. Here, we call it twice to add our 2 JavaScript files (Using <em>get_bloginfo(&#8216;template_directory&#8217;)</em> to get the link to our theme folder).</p>
<p>The important part in this script is the 2 hooks at the end. <strong>Rather than adding these files to every admin page</strong>, we only want them when we&#8217;re adding a new event (i.e. we&#8217;re on the post.php or post-new.php pages in the dashboard). One of <a href="http://codex.wordpress.org/Plugin_API/Action_Reference#Administrative_Actions">WordPress&#8217; admin hooks</a> is of the format:</p>
<p>admin_print_scripts-<em>filename.php</em></p>
<p>You can use that to add scripts <strong>to any particular page</strong> in the dashboard, just by replacing the <em>filename.php</em> part with the page you need (as we have done in our example).</p>
<p>Adding the CSS is almost identical, with just a slight change in the <strong>function and hook names</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Adds CSS for the jQuery datepicker script to Event pages.
 * http://jqueryui.com/demos/datepicker/
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> pbd_events_jquery_datepicker_css<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	wp_enqueue_style<span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'jquery-ui-datepicker'</span><span style="color: #339933;">,</span>
		get_bloginfo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'template_directory'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/jquery-ui-datepicker/css/smoothness/jquery-ui-1.8.11.custom.css'</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_print_styles-post-new.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_jquery_datepicker_css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'admin_print_styles-post.php'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pbd_events_jquery_datepicker_css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>And that&#8217;s us done!</strong> Now save your file and go add a new event with your snazzy datepicker in all its convenient glory!</p>
<p>If you&#8217;ve made it this far through the 3-4 posts in this series, <strong>well done!</strong> You should now have a very customizable and easy-to-use events list for your blog. If there&#8217;s anything else you&#8217;d like to know about tweaking it, just let me know in the comments!</p>
<p>And just before we end; I&#8217;d love to get your <strong>feedback on tutorials of this length</strong>. I know they can be quite a long read to sift through. I&#8217;m not sure I could make them any shorter (To me, all the explanations are the valuable part), but I could try out screencasts. Would you be interested in seeing that, or are you cool with written guides?</pre>
<img src="http://feeds.feedburner.com/~r/ProBlogDesign/~4/KzlgNZhV6fM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.problogdesign.com/wordpress/add-a-jquery-datepicker-to-event-posts-wp/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		<feedburner:origLink>http://www.problogdesign.com/wordpress/add-a-jquery-datepicker-to-event-posts-wp/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 5.318 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-02-09 14:11:54 --><!-- Compression = gzip -->

