Last active
October 4, 2024 02:35
-
-
Save tannerm/ffd437a0094b925fdca15864971ee95c to your computer and use it in GitHub Desktop.
Update Post Type Labels
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 | |
function refsites_update_labels() { | |
// Only run this function on the front end | |
if ( is_admin() ) { | |
return; | |
} | |
$post_types = array( 'post', 'sr_playlist' ); | |
$new_labels = array( | |
'post' => array( | |
'name' => 'Articles', // New plural label | |
'singular_name' => 'Article', // New singular label | |
), | |
'sr_playlist' => array( | |
'name' => 'Podcasts', // New plural label | |
'singular_name' => 'Podcast', // New singular label | |
), | |
); | |
foreach( $post_types as $post_type ) { | |
// Get the existing post type object | |
$post_type_object = get_post_type_object( $post_type ); | |
if ( ! $post_type_object ) { | |
return; | |
} | |
// Retrieve the existing arguments | |
$args = (array) $post_type_object; | |
// Modify the labels while keeping all other arguments | |
$args['label'] = $new_labels[ $post_type ]['name']; | |
$args['labels'] = array_merge( (array) $post_type_object->labels, $new_labels[ $post_type ] ); | |
// Re-register the post type with updated labels and original arguments | |
register_post_type( $post_type, $args ); | |
} | |
} | |
add_action( 'init', 'refsites_update_labels', 500 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment