Created
December 15, 2016 16:57
-
-
Save richjenks/c29e95214587475b2af0ad8b807a14b9 to your computer and use it in GitHub Desktop.
Allows you to rename any text string for a given post type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allows you to rename any text string for a given post type | |
* | |
* @todo Make it work with text from all gettext domains | |
* | |
* @param array $strings Associative array of old strings and new strings | |
* @param string|array $post_types String or array of Post Types to change the string for | |
*/ | |
function rename( $strings, $post_types ) { | |
// Ensure $post_types is an array | |
if ( !is_array( $post_types ) ) $post_types = [ $post_types ]; | |
// Set each string one-by-one | |
foreach ( $strings as $old => $new ) { | |
// Handle special cases | |
switch ( $old ) { | |
case 'Title': | |
foreach ( $post_types as $post_type ) { | |
add_filter( 'manage_' . $post_type . '_posts_columns', function ( $columns ) use ( $new ) { | |
$columns['title'] = $new; | |
return $columns; | |
} ); | |
} | |
break; | |
} | |
// Add filter to change text | |
add_filter( 'gettext', function( $translation, $text ) use ( $old, $new, $post_types ) { | |
// Loop through each post type | |
foreach ( $post_types as $post_type ) { | |
// Check we're in the right post type | |
if ( self::get_post_type() == $post_type ) { | |
// If current string is the one to change, do so! | |
if ( $text == $old ) return $new; | |
} | |
} | |
// Return all translations so later instances are updated too | |
return $translation; | |
}, 10, 2 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment