I created a custom user role in WordPress:
add_role('warehouse', 'Warehouse', get_role('administrator')->capabilities);
How to add translations for the role name so that it shows up in admin dashboard in the correct language based on the preferences of the currently logged in admin?
Adding a basic translation function call does not seem to help as add_role function runs once and then fetches the saved role from DB.
add_role('warehouse', __('Warehouse', 'mytextdomain'), get_role('administrator')->capabilities);
2
Answers
WordPress uses the
translate_user_role
function to render the translated role names. This expects the localized strings to be set in thedefault
translations domain.You can override the
default
translations with the following filter:We can achieve this by using the
editable_roles
hook of the WordPress, with the help of this hook we can dynamically translate the role name based on the current admin’s language choice.We need to add the given code to the
functions.php file
of the theme.