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?

2
  • 1
    You can use the gettext filter.
    – Sally CJ
    Jul 25, 2019 at 11:00
  • 1
    Thanks! Wasn't aware of this handy little filter. Jul 26, 2019 at 7:38

1 Answer 1

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.org/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!!!

1
  • Was reluctant to use gettext as it seemed like a bit of a generic workaround, but through explaining "because the Slug labels are hardcoded in the Core of WordPress" I can appreciate it's the best option. Thanks for the code snippet! Jul 26, 2019 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.