A totally handy chart of the WordPress DB schema

When working with WordPress, I often need to reference specific columns in the database. I’ve got most of them memorized (ID, post_author, post_date, post_content, etc, etc) but every now and then I’ll need to get at some data and can’t recall where it’s stored. Enter the handy WP DB schema chart!

This is accurate as of WP 3.0, which isn’t exactly cutting edge, but the schema isn’t something they change very often and I’ve yet to find any errors in it. If charts ain’t your thing, check our a full description of the database in the WP Codex.

Learning the columns and tables and the relationships between everything is a great way to pick up speed as a WP developer. Bookmark that chart (or the WP codex page) and check it often. You’ll find yourself making more advanced queries in no time.

Meta Valuables ebook available now

I’m happy to announce the release of my first ebook, Meta Valuables.

Meta Valuables is a 45 page look into the world of meta data, the extra bits of information that WordPress let’s us tack onto posts, pages, and users. The book is filled with code samples and real-world examples which will leave you with a better understanding of how to manipulate meta data in every WordPress project you tackle.

The ebook is available for as little as $2, with name your pricing for anyone feeling generous. For your donation, you’ll get a DRM-free PDF file (both at the time of purchase and emailed to you as a backup), along with a TXT file for good measure.

These lessons are meant for the learning WordPress developer, someone familiar with editing template files, basic PHP functions, the template hierarchy, etc. You’ll need a test or local install to follow along in code, but don’t let that scare you. Getting dirty with your WordPress templates is something every developer should look forward to.

Buy the Book Continue reading

Advice for the new WordPress developer

Best practices are called that because they’ve first been repeated again and again. In my many years of WordPress work, I’ve picked up a couple best practices that I wish I would’ve found sooner.

None of the following advice is ground breaking, it’s just all stuff that is constantly on a developer’s plate while working on a project. Maybe stuff that hasn’t always been there- stuff that exists outside the normal tutorial and how-to. I’ve written this rambling guide to hopefully help some new developers pick up some skills that might otherwise fall through the cracks.

Use a text editor, source control, etc

This might seem like a pretty simple one, so I won’t spend much time on it, but I thought it might be worth mentioning that a smart developer always uses a proper text editor or IDE, and a developer that doesn’t like to repeat work should be using some sort of source control, be it Git, SVN, or even a simple check in/out.

When you edit inside /wp-admin (or even when freestylin’ in a text editor), it’s really possible that you do something wrong. I do it all the time. When I do, I find great comfort in reverting changes to my last known happy place. Save some stress and hair, keep your code backed up.

Don’t make child themes

This is probably a point that might start some arguments, but I say bring it on. This may sound weird, but best practices for a professional might not be the best for beginners. The way you learn to do things is often not exactly the way you end up implementing things in the final version, but that journey is invaluable, especially for a new developer.

While a lot of developers and consultants will swear by child themes, I’d bet money that most of them didn’t start with child themes. I can admit that once you’re comfortable with PHP and theme structure, child themes are a fine preference, but I think when learning you should stick to tinkering right inside someone else’s code. Why?

Because someone smarter than you has already written a ton of great code. Go ahead and jump right into those template files and get dirty. If you’d like to build on their code with a child theme, you’ll at minimum need to know how to use PHP hooks, filters, and actions. Need another reason?

Automattic doesn’t use child themes on WordPress.com. They’ve built their own starter theme, _s (aka “Underscores”), and it’s meant for hacking, not birthing children themes. Each time they begin a new theme, they directly edit this starter, not hook into it. They even call the theme The 1,000 Hour Head Start. And those are hours from folks soooo much smarter than me.

If _s doesn’t suit your fancy, give Bones a try. Bones comes in multiple flavors: responsive, classic, and Genesis. All are filled with robust documentation and meant for direct editing, a great way to learn how things work. When you’re done, simply wash, rinse, and repeat.

Looking at code written by someone else is the main way I learn. Be it themes, plugins, or tutorials on blogs (like this one!). Once you’ve mastered that, feel free to make a child theme. Hook and filter until you’re blue in the face, just don’t do it before you know the guts like the back of your hand.

Don’t stop at the post and page

When I started with WordPress I spent a lot of time wrestling posts into new things. Portfolio items, events, movies, people. I’d add tags and categories and I’d query and I’d filter and things would eventually look like an interstate overpass exchange.

Custom post types and custom meta data are the secret sauce in all my WordPress work. I’m no longer shackled to the post or page, I can have movies and recipes. I’m no longer stuck adding markup to my post body, I can enter each specific bit of information into its own box.

I won’t explain the whole shebang now, I’ll just say to go get comfortable with custom post types and with custom meta data. I believe in meta data so much I wrote a 45 page ebook about it.

Extending WordPress beyond the post and post body is the main thing people are talking about when they mention doing things like a CMS. It used to be a hassle, now it just takes a few minutes at the top of your project. Do it.

Making a plugin is way easier than you’d think

Speaking of custom functionality, let’s talk about plugins. When I started making custom WordPress sites, I was pretty happy just dipping my toe into the WordPress themes folder. From there it was possible to change the styling, make custom templates, even add custom functionality like fields and post types.

But changing themes was a pain. For years I dealt with it, because I figured writing a plugin was complex. WRONG. Writing a custom plugin is about as difficult as making a custom page template. You include a tiny bit of custom markup near the top of a file (which you can even copy and paste), and WordPress does the rest.

In fact, here is the custom heading you’ll need to create a plugin. Simply create a file called something like my-plugin.php, start it with the following, then drop in your functions, shortcodes, includes, etc.

<?php 
/* 
Plugin Name: Name Here
Description: Description Here
Version: 0.1 
Author: ClarkLaB 
Author URI: http://clarklab.com 
*/ 

// do something here! make some cool_functions();

Once you're comfortable with making plugins, you can easily hop from theme to theme without losing things like custom post types, meta boxes, and more. And if you need to go the extra mile, perhaps at the fault of a clumsy client, you can load plugins in /mu-plugins/ to make their include mandatory. No way to disable them, nice and dummy-proof.

Bill Erickson uses such a method with his Core Functionality Plugin, which he's of course shared for you here. Following our theme of using code from folks smarter than us, let's follow his lead and make sure to put our must have functionality tucked away somewhere safe.

Get involved in your local WordPress scene

At the risk of revealing my true colors here, I love to get together with folks and talk about nerdy shit. I go to electronics conferences, I speak at WordPress meetups, and I meet a lot of great people while doing so.

Unless you work in a full-time environment surrounded by WP gurus, you deserve a sounding board for your developing skills. Simply finding some like-minded folks to bounce around ideas and best practices and favorite plugins off of will greatly improve your skillset.

For years while writing PHP code I sat alone (well, in a technical sense), reading blogs and posting in forums. In the past year or so, I've started to get involved in the Austin WordPress Meetup Group and I won't be stopping any time soon. There's a place for everyone. If you're learning, there is plenty to soak up there. If you think you are a pro, reciting your reasoning to a group of others will only help sharpen your resolve.

WordPress is calling this the year of the meetup, which should be reason enough to give it a try. Go to a meetup, go to a WordCamp, find your other nerds.

Learn from your mistakes

Aside from the corny title, this might be the most important bit of info here. When you take the time to learn something, the next time you do it you'll only do it better. And with code, you'll have the exact answer you're looking for.

If you're making themes and writing code, consider signing up for a GitHub account to share Gists or writing or blog or running a Tumblr- just do something that allows you to keep track of your past conquests. I often find myself writing blog posts specifically so I will remember it, sharing with you guys is just a bonus. Not only is reusing and expanding on code a great way to get things done, you can usually do so in a fraction of the time.

It's OK to learn on a client's dime, but know your limits

At Dev Day during WordCamp Austin 2012, an interesting topic came up. Someone asked about learning on the clock, and how to bill for that time. The consensus of the room seemed to be that as long as you've got a reasonable amount of skill and speed on your side, learning on the clock is a fine deal for both the developer AND client. How so?

When you tackle a project and run into a problem that needs solving, always remember that someone else has already found and fixed this specific issue. Not only is being able to find and apply that fix a skill worth paying for, you're only able to do so based on the 1,000's of hours of work you've already poured into your skillset. So your current client is getting a benefit that your past client paid for, and your next client will benefit from the time this client paid for. It's the circccle of lifffe! and any good client should realize he's scoring a deal to have custom solutions achieved specifically for his project.

That's not to say that you should take any project willy nilly and figure it out as you go. It means that a skilled developer can plan out 90% of his or her plan and freestyle the rest during the project. It does not mean that you should take a job with little idea how to complete it.

Ask questions but learn to Google

There are a lot of great WordPress forums and places to get your question answered. You can try the official WordPress support forums, StackExchange, Forrst, or the Austin WordPress Google Group. I'm even a paid member on ThemeHybrid, a community run by Justin Tadlock. The answers there are top notch and the members are super talented.

To be honest, though, you can usually find a solution to your problems with some exhaustive googling. Lots of times I'll be in the middle of the hunt, give up, ask a question, only to find a solution minutes later with some further snooping.

I love starting a good discussion and seeing how other developers do things, but never underestimate the power of teaching yourself to do things. The internet is the best text book ever written, especially where WordPress is concerned. There's more documentation and tutorials than you can shake a stick at. Take advantage of it!

I've even gone on job interviews and said "I'm really good at the internet." I explained that hitting walls and finding solutions are major part of my process, part of the process I'm damn good at. I got the job.

You target is always moving

As you continue to learn, your target will always be moving. A triumph one week will be code you're re-writing the next. This constant improvement and growth is one of the most thrilling things about being a developer and I hope this post helps set up some good habits for those of you looking to learn more.

Intermediate WordPress class this Sunday, limited space available

This Sunday, I’ll be teaching the class Meta Valuables, Unleash the hidden power of WordPress meta data. It’s happening at Cospace, from 3-6PM, we’re going to be coding live in class, and your attendance scores you a free copy of my awesome new ebook.

The course is being offered through SkillShare, a new startup for organizing local learning sessions taught by hobbyists and professionals (like me!). Tickets are $25, but at the time of publishing, I’m a featured class, so you can score a $5 off scholarship from Appssavvy (details on the Skillshare page).

Because we’ll be doing lots of hands-on coding, this class is limited to just 20 students (a third the spots of which are already taken). I anticipate this class filling up, so if you’d like to join us, head on over to Skillshare and claim yourself a ticket.

The ebook: 45 pages of awesome

Included with your attendance is a free, printed copy of my new ebook, written specifically for this course. Meta Valuables is a 45 page ebook that takes a revealing look into the world of WordPress meta data. Jam packed with code samples and quick tips, these lessons will have you developing more dynamic templates in no time!

Aside from the paper version for reference during class, you’ll also get access to free downloads of DRM-free PDF and TXT versions, perfect for throwing on your smart devices, emailing to yourself, etc.

Free hosting for all students from WP Engine

In addition to the ebook, all students will also get a FREE personal hosting plan from WP Engine, the finest damn host WordPress could ever ask for. They’re based here in Ausin and have a habit of swooping in and supporting events like this, so you’ll definitely want to keep an eye on @WPengine.

To claim your free hosting (for life!), simply join us on June 10 and I’ll give you the secret URL to access the offer. And while we’re talking about awesome folks sponsoring the event, I’d like to give a shout out to Cospace, who hooked me up with the class space. If you’re in Austin looking for a coworking space, come give Cospace a look.

What to expect

The full details are available on the Skillshare page, but students should come to class with a computer and test install of WordPress ready to work. We’re going to code PHP live in class, and having your own set of tools is definitely the way to get the most out of this course.

We’re going to look at functions like get_post_meta, add_post_meta, get_user_meta, update_user_meta, it’s going to be madness! Even if you’ve never touched custom fields or meta data before, you’ll leave this class with an solid understand of how all this junk works. See you there!

Passhhh! A new plugin that tells WordPress to shut up.

I’m happy to share that I’m in the process of submitting my second plugin to the WordPress repository. In keeping with the theme of my first plugin, simplicity, this thing is a single function that does almost nothing. But if you run a big site, you’d be mad not to include it.

Any time a user resets or changes a password, the admin email gets a notification. If you run a small site, no big deal. If you have thousands of users, this can get annoying. The solution was a simple one, which is included in-full below:

<?php 
/*
Plugin Name: Passhhh
Description: Stop the annoying admin emails when a user changes/forgets a password.
Version: 0.1
Author: ClarkLab
Author URI: http://clarklab.com
*/

if ( !function_exists( 'wp_password_change_notification' ) ) {
 function wp_password_change_notification() {}
}

Basically WordPress has a function called wp_password_change_notification(), which we simply detect then replace with an empty function of our own. This means that any time wp_password_change_notification() would normally run, instead nothing will happen.

Success! This plugin should be in the respository in the next few days. If you can't wait, copy/paste the source above into passhhh.php and get rolling!

Making custom category templates in WordPress

A question via @UmarfarukM: Is it possible to apply a new template for a specific category?

There are a couple of different ways to customize templates based on a specific category. If you’re looking to exchange the entire template, we can easily swap which template is loaded by naming our templates correctly. If you just need to change some markup or minor styling, a conditional statement within your generic category template might do the trick. Today we’ll take a look at both, starting with the file naming method.

Just like custom page templates, you can create a custom category template by using the filename category-slug.php, where “slug” is, of course, the slug of the category you’re trying to target. So if you wanted to make a custom template for the category “Movies”, you’d create category-movies.php. To jumpstart the file, you might want to copy category.php (or archive.php, depending on your theme).

You can also target by category ID, meaning if your “Movies” category had an ID of 18, you’d name your custom template category-18.php. You can learn more about which category template will be loaded over in the WordPress Codex.

If the above file-naming information doesn’t sound familiar, I’ll take this time to urge you to get comfortable with the Template Hierarchy. In every situation, WordPress has rules that determine which template files are loaded. Knowing which file will be loaded when your site renders is huge advantage when creating a custom theme. If you’re a visual learner, check out the Template Hierarchy flow chart.

If your changes aren’t extensive enough to warrant swapping out the entire template, consider using a conditional tag inside your normal category or archive template.

Using the following functions, you can easily check when a specific category is being displayed. Take a peek then join me down below for an explanation:

<?php 
is_category( '9' ) 
// check by category ID

is_category( 'Stinky Cheeses' ) 
// check by category name

is_category( 'blue-cheese' ) 
// check by category slug

is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) 
// mix and match!

Using any of the above functions will return true or false and let our templates know which category is being displayed. Simply use the functions inside an if/else statement and you're on your way!

For instance, if you wanted to do something like not display the sidebar when viewing the "Art" category, you could wrap the_sidebar() in something like the following:

<?php 
if (is_category( 'quotes' )) {
// do nothing
} else {
the_sidebar();
}

The above snippet, which you'd place in category.php or archive.php, first tests to see if we're in the category "Art", then takes the appropriate action. If the function returns true, we do not render the sidebar. If it returns false, we show the sidebar as we normally would.

Wrapping up

Using the above methods should be enough to handle just about anything, especially if you go the route of swapping out the file based on slug. Styling your category templates in different ways is a great method for creating 'sections' or 'channels' in your site that have a unique look and feel.

Here is your to-do list:

WordPress front-end post form using wp_insert_post

At WordCamp Austin, I hosted an awesome Q&A session about the underbelly of Android and Me. One of the most requested bits of code was the form action utilizing wp_insert_post().

(I was leading a session on wp_insert_post() at the time, the slides for which you can find here.)

If you’re not aware, we recently launched a featured on Android and Me called Threads. Threads allow users with enough points (oh yeah, our blog runs on a point system, too- take notes!) to create a post directly from the front-end of the site, never seeing /wp-admin. While my production implementation is quite a bit more complex than this, below I’ve shared a stripped down version of the functionality. Join me at the bottom for some extra, dirty details. Continue reading

I’m teaching a WordPress class in Austin on June 10

I’ve got a class coming up at Cospace on Sunday, June 10, called Meta Valuables – Unleash the hidden power of WordPress meta data. The class is being presented by Skillshare, a new startup for organizing local learning sessions taught by hobbyists and professionals (like me!).

The class costs $25 will be a 3 hour look into WordPress meta data, the extra bits of info we can tack on to posts, pages, and users to do some seriously awesome things. I’ll be doing live PHP development up on the big screen and everyone is encouraged to bring a laptop and code along. This is a class for those that are comfortable editing theme files, putting files to an FTP, reading the codex, etc. You’ll leave with a better understanding of how WordPress works as a whole, along with a slew of real-life code examples (demonstrated and in a take-home booklet). If that sounds up your alley, check out the URL below for full details:

http://clarklab.com/skillshare

I’m posting today to mention that Appssavvy has sponsored my class, providing a $5 off scholarship for all students. I wasn’t told how long this deal would last, so make sure you check the details on the page if you’re thinking about joining us. We’ve got space for 20 students, and tickets are on sale now!

If you’ve got any questions, or are planning to join us, feel free to use this post to sound off.

Welcome to my new theme, and ridiculous new experiment

At WordCamp Austin this past weekend, there was a lot of talk about frameworks and version control and process and I figured now might be a good time to try some new things. Here’s what you’re currently looking at:

A new theme, powered by the Automattic-built _s. Underscores, as it’s also known, is a bare-bones starter theme meant for hacking, NOT child theming. To build on top of _s, you simply fork and edit to your heart’s content.

To share all my edits with you guys, this entire theme is hosted over on GitHub, where you can fork and clone until your fingers fall off. You’ll notice I’m only a handful of commits in- what you’re looking at is maybe 1 hour worth of coding, which says a lot for the power of _s.

If you’ve seen me speak, you know how much I hate making child themes, and you probably also know how much I’ve enjoyed Bones over the past few months, which shares a similar philosophy. I’m mainly testing _s because I have a class coming up for beginning developers, and I’d like the simplest, most best-practice filled theme I can find. Don’t get me wrong, I looooove Bones, but I wanted something closer to the themes I remembered learning on.

You’ll also probably ask yourself, “Doesn’t WP Engine provide you a staging area? Why are you doing this live?” That’s a good question. One for which I don’t totally have an answer, except to say that my personal site is of little consequence to my day-to-day so I figured it’d be a fun place for an experiment like this. If you visit one day and notice something different, go check my GitHub source and see what I’ve done. Take the code. Learn from it. Or if I’ve done something wrong, help me learn.

I’m not done with the styling, it’s not fully responsive, some of my old functionality might not work, but it’s getting there. A few more hours of tinkering and this thing will be justttt right.

Convert links, username mentions, and hashtags in a tweet with PHP in WordPress

While working a recent project, we had a WP cron running that scraped Twitter and saved all the returned tweets as a custom post type. When displaying the tweet content, I realllly wanted to make the @username mentions, hashtags, and URLs auto-convert into proper links. Even if you aren’t make a silly webapp, there are still plenty of times when you might be displaying Twitter content.

In my hunt, I noticed WordPress has a built-in core function called make_clickable(). Like the name implies, it would scan the provided value and make all the URLs into clickable links. It worked just fine, but I also wanted user mentions and hashtags, so I went a step further and made my own function, aptly named make_twitter(). Check it out below:

<?php 
//place the following in functions.php or the like

function make_twitter($tweetcontent) {
  $tweetcontent = preg_replace("/[@]+([A-Za-z0-9-_]+)/", "\\0", $tweetcontent );
  $tweetcontent = preg_replace("/[#]+([A-Za-z0-9-_]+)/", "\\0", $tweetcontent );
  $tweetcontent = preg_replace("/((http)+(s)?:\/\/[^<>\s]+)/i", "\\0", $tweetcontent );
  return $tweetcontent;
}

//then use the function in your templates like so:

echo make_twitter(get_the_content());

What it does is actually pretty simple. First, place the make_twitter() function into your functions.php file (or wherever you're stashing your functions). Next, use make_twitter() in your templates. All you've gotta do is provide the tweet content, in this case my post content, so I use get_the_content().

Once my function receives the tweet, it simply runs a couple of regular expressions to sniff out the usernames, hashtags, and URLs, replacing them with proper links. On my project, I even changed the user link to the local site profile (since our accounts mirrored the Twitter account namespace).