Did You Know GitHub Supports SVN?

GitHub is an awesome code repository and code distribution website. It’s used by countless people and organizations to develop and release code.

I’ve wanted to made use of GitHub for a very long time now but the site is built around the Git revision control software. However I instead extensively use Subversion (SVN) in my daily life and have no desire or need to learn Git — everything I currently do requires that I use SVN. Not to mention that the Git experience on Windows is absolutely horrendous.

So I was incredibly pleased to learn that GitHub supports SVN! They added full support way back in October 2011 but I somehow missed the memo.

This is super good news because it means I can now start to distribute my code and projects via my my GitHub page. Nothing is there quite yet but I hope to package up and commit some code to there soon. :)

wp_list_pluck() Is An Awesome WordPress Function

If you’re a WordPress developer and you don’t know about wp_list_pluck(), then listen up!

Say you have an array called $posts that contains this:

[0] => stdClass Object
	[ID] => 675
	[post_author] => 5
	[post_date] => 2010-07-25 19:40:01
	[post_date_gmt] => 2010-07-25 19:40:01
	[post_content] => This site is using the standard ...
	[post_title] => About The Tests

[1] => stdClass Object
	[ID] => 501
	[post_author] => 6
	[post_date] => 2010-08-01 09:42:26
	[post_date_gmt] => 2010-08-01 16:42:26
	[post_content] => The last item in this page's content ...
	[post_title] => Clearing Floats

[2] => stdClass Object
	[ID] => 174
	[post_author] => 5
	[post_date] => 2007-12-11 16:25:40
	[post_date_gmt] => 2007-12-11 06:25:40
	[post_content] => Level 1 of the reverse hierarchy test ...
	[post_title] => Level 1
	)

[3] => stdClass Object
	[ID] => 173
	[post_author] => 5
	[post_date] => 2007-12-11 16:23:33
	[post_date_gmt] => 2007-12-11 06:23:33
	[post_content] => Level 2 of the reverse hierarchy test.
	[post_title] => Level 2

How would you go about getting all of the post_title values? Well obviously you could do something like this:

$post_titles = array();
foreach ( $posts as $key => $post ) {
	$post_titles[$key] = $post->post_title;
}

Sure, it’s not that complicated but that can get repetitive in your code. So how about this instead?

$post_titles = wp_list_pluck( $posts, 'post_title' );

Much easier, right? :)

Thanks Michael Fields for reminding me about this great function!

Sublime 2: Open New Tabs On The Right

One of the few things that bugged me about my favorite text editor, Sublime 2, is that new tabs would open up next to the current tab instead on the far right. Since I usually have about a half-dozen tabs open at once, this made keeping them in a sane order frustrating.

Thankfully Stylishmedia has written a simple little Sublime 2 plugin that makes new tabs open on the right side. Perfect. :)

Using Traits In PHP 5.4

I just read a great article by Shameer C. about traits in PHP 5.4. I’m really looking forward to using this functionality. Basically you can create things called traits, modules if you will, that you can then include into classes that you are writing.

For example if you need to deal with the filesystem, you could load in a filesystem trait that provides you with pre-written functionality. Or if you needed to make remote requests, you could load in a HTTP trait. Classes can only extend a single other class but you can use as many traits as you want.

This will be so useful!

Thanks Mo for sending me the link!

How To Remove (Or Change) “Private” Or “Protected” From WordPress Post Titles

If you make a post private in WordPress, the post’s title with be prefixed with “Private”. The same will happen if you password protect the post, although it will be prefixed with “Protected”. If you’d like to remove these, just add the following code to your theme’s functions.php file:

add_filter( 'private_title_format', 'yourprefix_private_title_format' );
add_filter( 'protected_title_format', 'yourprefix_private_title_format' );

function yourprefix_private_title_format( $format ) {
	return '%s';
}

The above code will remove the prefix by changing the format from Private: %s and Protected: %s to just %s where %s is the existing post title.

If you’d like to change it to something else, you can just change what the function in my example returns. If you want to change them independently of each other, then you’ll need to use two separate callback functions.

How To Create Custom WordPress Cron Intervals

This is mostly a reminder for myself so I can stop tracking it down every time I forget, but here’s how to have WordPress code execute on a different schedule than the default intervals of hourly, twicedaily, and daily. This specific example is for weekly execution.

<?php

// Add a new interval of a week
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'myprefix_add_weekly_cron_schedule' );
function myprefix_add_weekly_cron_schedule( $schedules ) {
	$schedules['weekly'] = array(
		'interval' => 604800, // 1 week in seconds
		'display'  => __( 'Once Weekly' ),
	);

	return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_my_cron_action' ) ) {
	wp_schedule_event( time(), 'weekly', 'myprefix_my_cron_action' );
}

// Hook into that action that'll fire weekly
add_action( 'myprefix_my_cron_action', 'myprefix_function_to_run' );
function myprefix_function_to_run() {
	// Add some code here
}

?>

On a side note, I believe this must go in a plugin and not your theme’s functions.php which I hear loads too late.

Two Handy Sublime Text 2 Plugins

After using EditPlus for the better part of a decade, I made the switch to the totally awesome Sublime Text 2 text editor a few weeks back. One of the great things about Sublime is it’s support for plugins.

Two such plugins I’d like to recommend are SublimeBrackets and SublimeTagmatcher. Both improve how the highlighting of opening and closing items are handled.

By default, Sublime just underlines paired brackets. When you have a ton of code, this can be really hard to see. SublimeBrackets changes this to something more apparent:

Much better, right? I personally use the solid background green style.

SublimeTagmatcher does something similar but for HTML tags. When you have your cursor inside of an HTML tag, it will highlight both the current tag and it’s opening or closing counterpart. This is helpful for making sure you have the correct number of opening and closing tags.

Translating Strings In WordPress Containing Multiple Placeholders

I really often see a common mistake made when translating strings in WordPress so I thought I’d write a blog post in order to shed more light on the issue.

But first, here’s a quick refresher on how to internationalize code in WordPress:

<?php _e( 'Welcome to my blog!', 'my-plugin-text-domain' ); ?>

For dynamic strings, it’d be something like this:

<?php printf(
	__( 'Welcome to my blog, %s!', 'my-plugin-text-domain' ),
	$name
); ?>

But what about when you have multiple variables to use in your string? A common mistake is to do something like this:

<?php printf(
	__( 'Welcome to my blog, %s! Today\'s date is %s.', 'my-plugin-text-domain' ),
	$name,
	$date
); ?>

The issue with this is that you’re requiring the person’s name to always come before the date. If for internationalization reasons it needs to be in a different order, then it won’t work. You’ll end up with something like Today's date is Alex. Welcome to my blog, November 2nd!.

The solution is to use standard sprintf() argument swapping parameters:

<?php printf(
	__( 'Welcome to my blog, %1$s! Today\'s date is %2$s.', 'my-plugin-text-domain' ),
	$name,
	$date
); ?>

Now translators are free to re-order the string to whatever makes the most sense for the language in question without having to worry the order of the variables.

For a more in-depth review of this, check out the WordPress Codex where many real world examples can be found.

Code Snippet: Generate A Plaintext Data Table

Inspired by some awesome-as-always code written by my co-worker Mike Adams, here’s a helper function to help create plaintext data tables in PHP. This is particularly useful for PHP-based CLI scripts (we use quite a few at Automattic) or for plaintext e-mails.

But first, here’s an example output with the optional dividers enabled:

   ID | First Name     | Last Name   
-------------------------------------
    1 | Alex           | Mills       
    2 | John           | Smith       
    3 | Barack         | Obama       
    4 | Fred           | Flinstone   
12345 | ReallyLongName | IsReallyLong

Click here to keep reading and see the code »