I want to change default Draft
text for my custom post type views, and I found a function that could do this:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
The problem is that I get only singular change for this. So if I had one Draft
I’ll have one Unapproved
. But when I have more than one I’ll have two Drafts
and automatically I’ll have two Unapproveds
. Which is terrible. It should be two unapproved. So I tried to use _n()
for the plurals, by creating an array to check against with my str_replace
:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$draft_array = array(esc_html__('Draft', 'custom_post_type_name'), _n('Draft', 'Drafts' , 10, 'custom_post_type_name'));
$views['draft'] = str_replace($draft_array, esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
But I still get Unapproveds
instead Unapproved
.
Any help how to solve this?
** ANSWER **
And once again I found the solution immediately after I post on SO xD
Just do a wp_count_posts('custom_post_type')
like
$custom_posts = wp_count_posts('custom_post_type_name');
if($custom_posts->draft == 1){
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
} else{
$views['draft'] = str_replace(esc_html__('Drafts', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
}