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).

Updated Notes from Cultivating Community

The following are some key links from my talk on Cultivating Community at WordUp Austin on January 21, 2012 (and now again at the WordPress Austin meetup on Feb 28, 2012). You can find the slides over on Google Docs, but the real magic came in person…

Follw on Twitter

@wpatx – I’m running the official WordPress Austin Twitter account now, you should follow it!

Comments

Jetpack – Has a new module that allows users to subscribe to comments.
Disqus – A managed plugin that adds new community features to comments.
Intense Debate – A plugin managed by Automattic that adds social features to comments.
Social Plugin for WordPress – A light(er)weight plugin by Crowd Favorite for allowing comments via Twitter and Facebook.
Livefyre – The new kid on the block, with added features like live comments.
Akismet – Quality spam protection for your growing comment section.

Ask questions

Topic Tueday – A new question once a week that is locked to comments from users only. A point multiplier is also in effect.

Poll Your Audience

PollDaddy – A powerful and easy to use poll plugin maintained by Automattic.
WP-Polls – A lightweight poll plugin written by Lester ‘Gamerz’ Chan.

Newsletters

Mailchimp – A paid (and thus pretty awesome) email newsletter solution.
Mailchimp Subscribe Plugin – A easy widget for newsletter subscriptions.
Jetpack – Has a new module that allows users to subscribe to your blog.
Feedburner – An old-school RSS tool that offers an email newsletter digest.

Get Social

Social Bartender – Easy, WP-menu-like drag and drop setup for all your social network links.
Sociable – Configurable sharing links and widgets for all your content.
Explore – There are more social plugins than hairs on my head. Check them out and find out that works for you!
Pin it! – A Pinterest plugin for adding a ‘Pin it’ button to your content.
Easy Pinterest – Display your latest pins and boards in your WordPress sidebar.

Identity / Influence

And Me Account – A sample account from my site, showing real name, user pic, user score, percentile rank, and links to various social accounts.
Gravatar – A widely used avatar service maintained by Automattic.
Cubepoints – A lightweight point tracking system with modules that make for easy customization.
Bunchball – Full-bore gamification with badges and achievements and all that stuff.

Contests

Pick Giveaway Winner – A super simple plugin for running bespoke contests with user comments.
25 Days of Tegra – My biggest promo to date, where we gave away a device per day only to users, generating tens of thousands of user interactions.
#HackTheNews – A sample contest I ran on Android and Me where users could tweet stories with tweaked headlines, generating over 1k tweets in barely a week.
Midweek Madness – A weekly contest with usually small prizes.

Do Things with your Users

get_users – A new (since 3.1) function for working with users. Makes things super simple (compared to the old ways, at least).
update_user_meta – A super-handy function for adding and updating extra information to any user account. Use with get_user_meta.
orderby=comment_count – A parameter you can use when querying posts to return the most commented posts.

Favorite community-focused WordPress plugins

I’m giving a talk tomorrow night called Cultivating Community where I’ll be sharing some insight into the process of running a blog with 10 authors and 45k registered users. I’ve given the talk once before and already my have own list of favorite plugins growing, but I was curious what you guys use.

Are there any community plugins you can’t live without? Or something new you’ve seen floating around that you’d like me to investigate and talk about during my session tomorrow? It doesn’t even have to be a plugin really, feel free to leave me some broad questions about practice or strategy, I’m really just looking for some extra angles to cover to make sure everyone finds use in tomorrow’s talk.

Change the default avatar in WordPress

If you’ve ever wanted to change the default avatar in WordPress, all you need is a quick filter in functions.php that inserts and names your new avatar. Once you add the markup below, you’ll see your new avatar type listed in wp-admin > Settings > Discussion along with the choices like Identicons and MonsterID. The code:

<?php 
add_filter( 'avatar_defaults', 'newavatar' );  
function newavatar ($avatar_defaults) {  
$myavatar = get_bloginfo('template_directory') . '/images/avatar.png';  
$avatar_defaults[$myavatar] = "Pigs";  
return $avatar_defaults; }

This is a pretty simple one. Our function newavatar() is hooked to the avatar_defaults, and we simply add our new avatar choice, which includes a path to the image URL along with a title (pigs!). At the end, we return the defaults, which has been impregnated with our new custom avatar, and no one is ever the wiser!