Using Filters to Modify Labels
-
In this review @emangham asked
How do I use filters to modify labels?
For now, modifying labels requires the use of two filters calling two functions (one for plural labels and one for singular labels). Now that I’ve given it a bit more thought, I will probably move this to a single filter. In order to use these, you will want the following code:
add_filter( 'gt_label', 'twxyz_modify_glance_label', 10, 2 ); function twxyz_modify_glance_label( $label, $glance ) { if ( 'comment' == $glance ) { return 'Messages'; } else { return $label; } } add_filter( 'gt_label_singular', 'twxyz_modify_glance_label_singular', 10, 2 ); function twxyz_modify_glance_label( $label, $glance ) { if ( 'comment' == $glance ) { return 'Message'; } else { return $label; } }
As I mentioned, I threw this together pretty quick in version 3.1. In version 3.2, I would expect that this will be consolidated to a single filter
gt_label
. Not sure why I didn’t think of this earlier. Implementation from that point on would be as follows:add_filter( 'gt_labels', 'twxyz_modify_glance_labels', 10, 3 ); function twxyz_modify_glance_labels( $label, $glance, $count ) { if ( 'comment' == $glance ) { return _n( 'Message', 'Messages', $count ); } else { return $label; } }
In the code above,
$glance
should be set to the CPT name.
- The topic ‘Using Filters to Modify Labels’ is closed to new replies.