January 2nd, 2010
As a WordPress lead developer, every time I see someone recommending editing a core WordPress file, a little bit of me dies.
You should always avoid editing the core files and put your modifications into a plugin so as to ensure you have a smooth upgrade experience to a future WordPress version.
Therefore inspired by the following forum post here is how to change one of the translatable strings in WordPress without hacking a core file using the filters available in the translation functions:
<?php
/*
Plugin Name: PJW Translation Mangler
Plugin URI: http://blog.ftwr.co.uk/#
Description: Example of how to mangle translated strings.
Author: Peter Westwood
Version: 0.01
Author URI: http://blog.ftwr.co.uk/
*/
class PJW_Translation_Mangler {
/**
* Filter the translation string before it is displayed.
*
* @param $translation The current translation
* @param $text The text being translated
* @param $context The context for the translation
* @param $domain The domain for the translation
* @return string The translated / filtered text.
*/
function filter_gettext($translation, $text, $domain) {
$translations = &get_translations_for_domain( $domain );
if ( $text == 'View all posts filed under %s' ) {
return $translations->translate( 'See all articles filed under %s' );
}
return $translation;
}
}
add_filter('gettext', array('PJW_Translation_Mangler', 'filter_gettext'), 10, 4);
?>
The filter used in this example gettext is one of a set of filters in the translation functions in wp-includes/l10n.php which also include gettext_with_context, ngettext, and ngettext_with_context.
Posted in Development, Tutorials, WordPress | Read 9 Comments
July 19th, 2009
Last night there was a discussion as to what could be achieved today as part of a “WordHack” session at WordCamp UK and SimonD tweeted that he would like to be able to add extra user meta fields to the back end user profile page easily.
I tweeted back that it was already easy and so here is the proof of concept code which shows you:
- How to add a user meta field to the page
- How to process the new value when it is updated.
These can the been displayed on the front-end using the standard template tags get_the_author_meta() and the_author_meta()
<?php
/*
Plugin Name: PJW User Meta
Plugin URI: http://blog.ftwr.co.uk/archives/2009/07/19/adding-extra-user-meta-fields
Description: Allows users to configure some random extra meta value.
Author: Peter Westwood
Version: 0.02
Author URI: http://blog.ftwr.co.uk/
Use of the frontend as get_the_author_meta('something') or the_author_meta('something')
*/
class pjw_user_meta {
function pjw_user_meta() {
if ( is_admin() )
{
add_action('show_user_profile', array(&$this,'action_show_user_profile'));
add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
add_action('personal_options_update', array(&$this,'action_process_option_update'));
add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
}
}
function action_show_user_profile($user)
{
?>
<h3><?php _e('Other Contact Info') ?></h3>
<table>
<tr>
<th><label for="something"><?php _e('Something else'); ?></label></th>
<td><input type="text" name="something" id="something" value="<?php echo esc_attr(get_the_author_meta('something', $user->ID) ); ?>" /></td>
</tr>
</table>
<?php
}
function action_process_option_update($user_id)
{
update_usermeta($user_id, 'something', ( isset($_POST['something']) ? $_POST['something'] : '' ) );
}
}
/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $pjw_user_meta_instance; $pjw_user_meta_instance = new pjw_user_meta();'));
?>
Enjoy!
Updated to correctly use the $user/$user_id passed to the actions rather than the global $user_id
Posted in Development, Tutorials, WordPress | Read 19 Comments
May 26th, 2009
I had a request recently for help on using my Query Child Of plugin to create a page template to show the grandchildren of a page as well as a number of people asking for better examples on how to use my plugin in general to just list the children of a page.
I am now happy to announce that I can provide these examples and have updated the plugin documentation on in the WordPress plugin directory to match as well.
Read the rest of this entry »
Posted in Development, Tutorials, WordPress | Read 2 Comments
May 25th, 2009
Jane asked -
“I’m nowhere near my iMac, and won’t be for weeks. I need to backup my iPhone onto my MacBook Pro or external hard drive today. How?!”
To which the answer is, suprisingly, use iTunes.
It turns out that once you have connected your iPhones to iTunes on another Mac you can just right click on the device and select backup like so:

Hopefully this will help other people in a similar situation in the future.
Posted in Technology, Tutorials | Comment on this post
July 19th, 2008
A long time ago I wrote about the ways in which you can get involved in WordPress development using free available tools on windows in my post “Windows WordPress toolbox”. Since that post I have come up with a number of useful scripts and shortcuts that I use in my WordPress development.
Here are a number tools that I use most often, these are a set of bash functions which you can stick in your ~/.bashrc file on your linux machine or mac :
//Download a patch from a trac install and apply it to a svn checkout
//e.g. wp-trac-patch http://trac.wordpress.org/attachment/ticket/2345/2345.diff
wp-trac-patch() {
svn revert -R . & curl "$1?format=raw" | patch -p0
}
//Search through a subversion checkout using grep (only search the .php and .js files)
//e.g. wp-grep the_excerpt
wp-grep() {
find . \( -name "*.php" -print , -name "*.js" -print \) | xargs grep "$1"
}
//Merge a change from trunk to a branch in a subversion checkout of the whole repository
//e.g. wp-merge 1234 2.6
wp-merge() {
svn merge -r $(($1-1)):$1 trunk branches/$2
}
//Output the svn:eol-style property of all the files in a repository
svn-eol() {
find . \( -name "*.php" -print , -name "*.js" -print \) | xargs svn propget svn:eolstyle
}
I hope you find this useful
Posted in Development, Technology, Tutorials, WordPress | Comment on this post
March 20th, 2008
Now WordPress 2.5 RC1 is out it the wild for testing we are receiving some reports of strange problems with some of the ajax functionality in the admin pages so I have prepared a quick tutorial to help people collect the relevant debugging information to help us investigate the problems.
Here is how to prepare:
- Install Firefox (if you don’t have it already!)
- Install firebug. This is the web debugging tool of choice.
- Load up the relevant WordPress admin page that is not working for you.
Read the rest of this entry »
Posted in Development, Firefox, Technology, Tutorials, Web dev, WordPress | Comment on this post
March 10th, 2006
WordPress v2.0.2 has been released with a number of security updates so it is well worth upgrading.
To simplfy the upgrade process I decided to try generating a diff file between 2.0.1 and 2.0.2 and using that to patch my sites rather than uploading a full set of new files.
Here is the process I took on my local machine to generate the diif file:
- Exported both 2.0.1 and 2.0.2 from svn using the following commands:
To get 2.0.2 - svn --force export http://svn.automattic.com/wordpress/branches/2.0
To get 2.0.1 - svn --force export http://svn.automattic.com/wordpress/tags/2.0.1
- Generated a patch file using diff:
diff -U3 -r 2.0.1 2.0 > 2.0.1.to.2.0.2.patch
I then uploaded the patch file to my server and placed it in my home directory and applied the patch as follows:
Read the rest of this entry »
Posted in Tutorials, WordPress | Read 5 Comments