Block-level comments trick

Block-level comments are useful for commenting out an entire block of code in PHP, CSS, and other code contexts.

/*
$this = 'code is deactivated';
$and = 'so is this';
*/

The only problem with this is that when you go to re-activate this code, you have to change both the opening and closing comment markers. That’s a pain.

While I was at WordCamp NYC last week, I saw Daisy Olsen using a very clever trick in her lightning round talk.

/*
$this = 'code is deactivated';
$and = 'so is this';
/**/

See what she did there? The closing comment marker is preceded by another opening marker. Because comment blocks can’t be nested, this second opening comment marker is ignored. This enabled her to re-enable this code by removing the opening marker.

$this = 'code is reactivated';
$and = 'so is this';
/**/

Brilliant! I can’t believe I haven’t seen this before. The one downside to this is that you are deleting two characters and destroying the opening marker. Here’s an even better method.

//*
$this = 'code is reactivated';
$and = 'so is this';
/**/

By adding a slash in front of the opening comment marker, I comment out the comment marker. It only takes one key press, and the corpse of the original opening marker is retained, allowing you to reinstate it with the deletion of a single character.

Props to Aleem Bawany for the second trick (he uses //*/ as the closing comment, which works pretty much the same way).

What other commenting tricks do you know?

Using PHP5 object constructors in WP Widget API

Someone mentioned to me that they couldn’t use PHP5-style object constructors when using the WP Widget API. I looked into it, and it turns out it does work. Example:

class My_Widget extends WP_Widget {
	function __construct() {
		$widget_ops = array( 'classname' => 'css-class', 'description' => 'Description' );
		parent::__construct( 'css-class', 'Title', $widget_ops );
	}
	// Rest of your widget subclass goes here
}

The key is using parent::__construct() instead of $this->WP_Widget().

Tip for merging two WordPress blogs

WordPress’ built-in export/import functionality makes it a snap to merge two blogs into one. You simply export from the blog you’re going to retire, import into the blog you’re going to keep, and then forward the old domain to the new one.

Here’s my tip: do not select an existing blog user to “own” the imported content! Instead, create a new temporary user just for that content. Here is why: the imported posts may need some manual cleanup, like integration into the new blog’s category structure, or adding/removing tags. By importing into a new author, you’re creating a way to view only the posts imported in that batch. Simply go to the Users → Edit screen and then click the number of posts next to the temporary import author. Boom, you’re looking at only the imported posts. Then, as you clean up their categories and tags using 2.7′s bulk edit or quick edit features, you can simultaneously reassign the posts to their proper author. Once there are no more posts for that temporary import author, delete that author, and you’re done!