Translating WordPress Plugin Details

Plugin authors: did you know that you can allow translators to localize the plugin details that show up in the plugins list in the WordPress administration area? Your plugin’s name, description, and so forth? Well you can! It’s actually really simple to do and all you need to do is add one or two additional plugin headers to your file.

The first is Text Domain and this is the text domain for your plugin, i.e. the first argument that you are passing to load_plugin_textdomain().

The second one is Domain Path and is optional. It’s only needed if you store your translation files in a subfolder inside of your plugin’s folder.

Here’s an example load_plugin_textdomain() call from one of my newest plugins:

load_plugin_textdomain(
	'add-descendants-as-submenu-items',
	false,
	dirname( plugin_basename( __FILE__ ) ) . '/localization/'
);

That loads translation files from a subfolder called “localization” inside of my plugin’s folder. This turns into the following plugin header:

Plugin Name:   Add Descendants As Submenu Items
Plugin URI:    http://www.viper007bond.com/wordpress-plugins/add-descendants-as-submenu-items/
Description:   Automatically all of a nav menu item's descendants as submenu items. Designed for pages but will work with any hierarchical post type or taxonomy.
Version:       1.1.0
Author:        Alex Mills (Viper007Bond)
Author URI:    http://www.viper007bond.com/

Text Domain:   add-descendants-as-submenu-items
Domain Path:   /localization/

An extra line break isn’t needed nor is the extra spacing but I added both just for aesthetic reasons.

And that’s it! WordPress will then attempt to translate the plugin’s name, URI, description, author, author URI, and version fields. I personally only include the plugin’s name and description in my translation template files though as I don’t feel translators need to localize the other fields.

If you need help generating a translation template file for your plugin, log into WordPress.org and then visit the “Admin” tab on your plugin’s page on WordPress.org. You can generate a POT file for your plugin there.

New Plugin: Add Descendants As Submenu Items

When I created the navigation menu at the top of my site using the menu feature in WordPress (Appearance → Menus), I didn’t want to have to maintain anything but the top-level menu items. When I released a new plugin and created a page for it, I didn’t want to have to go into my menu UI and add it there too. So instead I wrote this plugin.

Add Descendants As Submenu Items adds a checkbox to each menu item that is of a hierarchical post type (i.e. pages). Checking this box will automatically display all descendants as submenu items on the front end of your site. You can see it in action at the top of my site — I only configured the top level menu items and everything that shows up when you hover over them has been added by my plugin rather than manually by me.

For more details and to download the plugin, check out the plugin’s homepage.

Displaying Shortcodes

Have you ever wanted to display a shortcode in a post? i.e. like this: [gallery]

It’s actually quite simple thanks to a little-known feature of WordPress. All you need to do is double up on the brackets:

[[gallery]]
[[example]foobar[/example]]

Note where the double brackets are in that last example.

Accessing Other Databases/Servers From Within WordPress

Say you have a WordPress install where you need to pull in data from a separate database or server. The normal $wpdb instance of the wpdb class is limited to the database that your WordPress tables are located in. Yes, you could use mysql_connect() and the other standard PHP database functions, but then you don’t get all of the WordPress magic. So what to do?

The answer is surprisingly simple: make a new instance of the wpdb class! Dion Hulse (DD32) was kind enough to point out this great solution to me a few weeks back and I thought it was worth sharing with you all.

$myotherdb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );

$myotherdb->get_results( "SELECT id, name FROM mytable" );

Changing Core WordPress Strings

One of the lesser known filters in WordPress is gettext. All strings that are run through the WordPress translation functions are also run through this filter after being translated thanks to a ticket I opened way back in 2008. That means you can actually use it to change pretty much any string in WordPress, namely in the admin area.

I’ve used this in some plugins I’ve written in the past and it works incredibly well.

My co-worker and one of the lead developers of WordPress Peter Westwood (westi) wrote a really good blog post about this, however I wasn’t completely happy with his code so I’m writing this blog post to share how I would write some code to take advantage of this versatile filter. Don’t get me wrong — he wrote good and valid code, but it’s not exactly the easiest thing for a novice to extend for their own uses.

function youruniqueprefix_filter_gettext( $translated, $original, $domain ) {

	// This is an array of original strings
	// and what they should be replaced with
	$strings = array(
		'View all posts filed under %s' => 'See all articles filed under %s',
		'Howdy, %1$s' => 'Greetings, %1$s!',
		// Add some more strings here
	);

	// See if the current string is in the $strings array
	// If so, replace it's translation
	if ( isset( $strings[$original] ) ) {
		// This accomplishes the same thing as __()
		// but without running it through the filter again
		$translations = &get_translations_for_domain( $domain );
		$translated = $translations->translate( $strings[$original] );
	}

	return $translated;
}

add_filter( 'gettext', 'youruniqueprefix_filter_gettext', 10, 3 );

So as you can see at it’s core it accomplishes the same thing as Peter’s code however my code should be a bit more clear on how to make it translate multiple strings.

Hope someone finds this helpful! :)

Painting The Bike Shed

Normally I’d just retweet this and be done with it, but I feel this is worth blogging:


I never fully understood the concept of “painting the bike shed” until I changed the typeface in the world’s most popular blogging software.
@iammattthomas
Matt Thomas

Matt Thomas also has a blog post if you’re interested in further reading.

(Have no idea what “painting the bike shed” is referring to? Wikipedia has your back.)

Regenerate Thumbnails v2.1.0 (and v2.1.1) Released

I’ve released a major update to my popular Regenerate Thumbnails plugin. From the changelog:

  • Thanks to a lot of jQuery help from Boris Schapira, a failed image regeneration will no longer stop the whole process.
  • The results of each image regeneration is now outputted. You can easily see which images were successfully regenerated and which failed. Was inspired by a concept by Boris.
  • There is now a button on the regeneration page that will allow you to abort resizing images for any reason. Based on code by Boris.
  • You can now regenerate single images from the Media page. The link to do so will show up in the actions list when you hover over the row.
  • You can now bulk regenerate multiple from the Media page. Check the boxes and then select “Regenerate Thumbnails” form the “Bulk Actions” dropdown. WordPress 3.1+ only.
  • The total time that the regeneration process took is now displayed in the final status message.
  • jQuery UI Progressbar version upgraded.

As you can see, lots of great new stuff. I hope you all enjoy it. :)

SyntaxHighlighter v3.1.0 Released, Features Old Style Script Option

Not everyone was happy with the new highlighting package featured in SyntaxHighlighter v3.0.0 and using old versions of plugins is a bad idea (you miss out on features, bug fixes, etc.) so I’ve added the ability to toggle between v2 and v3 of Alex G’s SyntaxHighlighting package. I’ve also fixed a few bugs that were discovered post-release (such as HTML entities being broken in the Visual editor).

Everyone, including those who downgraded to v2.x of my plugin, should upgrade to v3.1.0 of my plugin.

One thing to note by the way: I would stay far, far away from TinyMCE (the Visual editor/tab) when blogging about code. It has the nasty little habit of attempting to “clean up” your code (namely HTML) for you and in the process with mess up your code. If you’re writing code, what are you doing using a WYSIWYG editor anyway? ;)

SyntaxHighlighter Evolved v3.0.0: What’s New

I finally found some time to work on my SyntaxHighlighter Evolved plugin and upgrade it use the latest version of Alex Gorbatchev’s highlighter.

What’s New

  • The new version of Alex G.’s script makes it easier to select and copy code. You can just drag your mouse to highlight and you will no longer get line numbers or you can double-click the code to highlight it all (in plain text to avoid getting the colors). Click off of the code to get it to go back to the colorized version.
  • You can specify a range of line numbers to highlight. Instead of having to do highlight="5,6,7,8,9,10,14" you can now just do highlight="5-10,14".
  • BuddyPress support.
  • A few new custom brushes (Clojure and the R language) and a Ukrainian translation.

Upgrade or download it now! :)