Guide To Having PhpStorm Use Windows Subsystem For Linux’s Git

I’m one of the rare Windows users at Automattic but thanks to the introduction of Windows Subsystem for Linux, my life is way easier. I can now run pretty much any Bash script without having to spin up a virtual machine like I used to have to. While it’s possible to build Node.js-powered projects natively under Windows thanks to the Windows-Build-Tools package, it’s a lot easier to just do everything within Linux. This is largely because building in one environment means that you can’t run the tools in the other environment.

The main time that this bites you is when the project you’re working on has a pre-commit hook in Git. If you build your project in Linux but attempt to commit from Windows, it won’t work. IDEs such as PhpStorm do all of their Git operations from Windows. This blog post will explain how to get it to use the copy of Git within Linux.

  1. Install Pageant from PuTTY or if you’re signing your commits, then Gpg4win (its gpg-connect-agent can be set to be PuTTY compatible). Have it start with Windows in order to load your SSH keys.
  2. Verify that the agent is working by using PuTTY to connect to github.com with the username git. GitHub won’t let you open a shell but it’ll let you know that you have successfully authenticated. Note that command line ssh won’t work because it’s not Pageant-compatible.
  3. Install weasel-pageant to allow you to use SSH keys from within Linux.
  4. Verify that the agent is working by running ssh-add -l in Linux. Your key(s) should be listed.
  5. Verify SSH is working by running ssh git@github.com. If it doesn’t work, append -vvv to the end of the command to enable debug output to see what’s wrong.

Now that you have Git authentication working within Linux, it’s time to get PhpStorm (or whatever IDE you use) to use Linux’s Git.

The trick here is instead of having PhpStorm use git.exe, you need to point it at a batch script. This Stack Overflow question helped me a ton, but I needed to modify it a little bit.

For whatever reason, my .bashrc file wasn’t being loaded when calling bash.exe -c which meant that agent wasn’t being loaded. So as a part of my batch script, I’m manually loading my personal file that contains my shell customizations. Here’s my full batch file:

@echo off
setlocal enabledelayedexpansion
set command=%*
set find=C:\Users\%USERNAME%\AppData\Local\Temp\git-commit-msg-.txt
set replace=/mnt/c/Users/%USERNAME%/AppData/Local/Temp/git-commit-msg-.txt
call set command=%%command:!find!=!replace!%%
If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\sysnative\bash.exe -c 'source ~/.viper-common; git %command%'
) Else (
    bash.exe -c 'source ~/.viper-common; git %command%'
)

In PhpStorm, go to File → Settings (or Default Settings) → Version Control → Git and set the “Path to Git executable” to point at the batch file. Verify that it works by clicking the Test button.

With that, everything should work now!

Questions? Problems? Leave a comment below.

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.