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! :)

WordPress Trick: How To Show Hidden Custom Fields

Often when I’m developing some WordPress code, I need to be able to debug by being able to see hidden custom fields (any custom field with an ID that’s prefixed with an underscore won’t show up on the edit screen). In order to do so, I use this little bit of code:

add_action( 'admin_head', 'showhiddencustomfields' );

function showhiddencustomfields() {
	echo "<style type='text/css'>#postcustom .hidden { display: table-row; }</style>\n";
}

You can drop it into a new plugin or if you’re lazy, your theme’s functions.php file.

WordPress Code Snippet: Output Advanced Page Generation Statistics

Here’s a little bit of code to display the percent of a page’s generation time used by PHP and MySQL.

First, add this into your theme’s functions.php file wp-config.php file so that data about each MySQL query is stored, namely the time taken.

define( 'SAVEQUERIES', true );

Then in your footer.php, use this code to display it:

<?php

global $wpdb;

// Get the total page generation time
$totaltime = timer_stop( false, 22 );

// Calculate the time spent on MySQL queries by adding up the time spent on each query
$mysqltime = 0;
foreach ( $wpdb->queries as $query )
	$mysqltime = $mysqltime + $query[1];

// The time spent on PHP is the remainder
$phptime = $totaltime - $mysqltime;

// Create the percentages
$mysqlper = number_format_i18n( $mysqltime / $totaltime * 100, 2 );
$phpper   = number_format_i18n( $phptime / $totaltime * 100, 2 );

// Output the stats
echo 'Page generated in ' . number_format_i18n( $totaltime, 5 ) . " seconds ( {$phpper}% PHP, {$mysqlper}% MySQL )";

?>

That will output code something like this: Page generated in 0.24691 seconds ( 95.50% PHP, 4.50% MySQL )

“Quoter” Validation Fix

Seems Quoter doesn’t play 100% nice with WordPress 2.5 (breaks page validation). It’s a simple fix though.

Open the plugin and find this text on line 568:

$text = wpautop($text, 0);

Before it, add this dirty little fix:

$text = str_replace( array('<p><p>', '</p></p>'), array('<p>', '</p>'), $text);

It only works around the problem rather than fixing it, but it seems to do the job as most pages now validate. :)

EDIT: Seems WordPress originally mucked up the code that I had posted. It’s now been fixed.

How To: Display Text At End Of Post In Only Single View

Ever wanted to display some text at the end of your post that would only show up on the single post view? Perhaps a message to commenters? Well, WordPress has the ability built in. No need for any plugins.

The trick is to use the <!--more--> tag. However, I bet you’re thinking “but I don’t want a ‘Read More’ link!”. Well, once again, WordPress to the rescue.

For a few major versions now, you can do custom more text. You simply format it like this: <!--more Check out the rest of my post!-->. Or, to remove the link altogether while still leaving the feature intact, just use a space: <!--more -->.

Here’s an example:

This is a cool post. It has lots of interesting stuff.

More interesting stuff.

<!--more -->

This text will only show up on the single post view.

You can check it out in action as the following text will only show up in single post view: