最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Rename a slug label

programmeradmin0浏览0评论

In my custom post type there is a custom taxonomy that has the usual Title, Slug and Description fields.

I'm expecting the end user to not understand what the slug is for, and in my case it needs to be a language code e.g. en_GB, so I just want to be able to rename the Slug label to read "Language Code" instead.

TLDR; Can I rename the slug label of a custom taxonomy?

In my custom post type there is a custom taxonomy that has the usual Title, Slug and Description fields.

I'm expecting the end user to not understand what the slug is for, and in my case it needs to be a language code e.g. en_GB, so I just want to be able to rename the Slug label to read "Language Code" instead.

TLDR; Can I rename the slug label of a custom taxonomy?

Share Improve this question asked Jul 25, 2019 at 8:09 WongKongPhooeyWongKongPhooey 1134 bronze badges 2
  • 1 You can use the gettext filter. – Sally CJ Commented Jul 25, 2019 at 11:00
  • 1 Thanks! Wasn't aware of this handy little filter. – WongKongPhooey Commented Jul 26, 2019 at 7:38
Add a comment  | 

1 Answer 1

Reset to default 2

As SallyCJ mentioned, gettext is really the only option you have with this, because the Slug labels are hardcoded in the Core of WordPress.

https://core.trac.wordpress/browser/tags/5.2/src/wp-admin/edit-tags.php#L439

Here is a snippet I wrote and tested that should work for you, and it will only run on the backend and only on the two pages where you will see the and want to change the Slug:

add_action('current_screen', 'current_screen_callback');

function current_screen_callback($screen) {

  // Put some checks and balances in place to only replace where and when we need to
  if(
    is_object($screen) &&
    ($screen->base==='edit-tags' || $screen->base==='term') &&
    $screen->taxonomy==='custom_tax_slug'){
        add_filter( 'gettext', 'change_out_slug_labels', 99, 3 );
  }
}

function change_out_slug_labels($translated_text, $untranslated_text, $domain){

  if(stripos( $untranslated_text, 'slug' )!== FALSE ){
      $translated_text = str_ireplace('Slug', 'Language Code', $untranslated_text);
  }

  return $translated_text;
}

current_screen is an admin only hook, so the call to swap out what you need is only called in the backend. I took the change_out_slug_labels() function out so that you can use it on the front end if you need to.

I hope this helps!!!

发布评论

评论列表(0)

  1. 暂无评论