I developed a small plugin that adds (or to be more precise: first deletes then adds again) some custom user roles. Each of these roles is set up in a way like this:

function wpdev_141551_add_role_someone() {

    $role = 'someone';

    remove_role( $role );

    $capabilities = array(
        'read'          => TRUE,
        'publish_posts' => TRUE,
        'edit_posts'    => TRUE,
        'delete_posts'  => FALSE,
        // ...

    add_role( $role, 'Someone', $capabilities );

Now the problem is, these custom user roles don't get translated at all.

The translations of the default user roles (Author, Contributor etc.) are included in WordPress's default text domain. So how/where should I step in and translate these custom role names?

If I add the role with a gettext call for its name __( 'Someone', 'plugin-text-domain' ) then the string that gets written to the database will be translated according to the current language. What I would like to have, however, is a solution that lets me switch languages as I want and have the roles be displayed accordingly—without having to de- and then reactivate the plugin (i.e., delete and add the role, again).

I will post my current solution to this, but I'm still very interested in any other (possibly more efficient) approach.

  • I've seen that before editing question you used a translated role label, i.e. you used add_role( $role, __('Someone', 'text-domain'), $capabilities ); but then you deleted it... so what's wrong there? – gmazzap Apr 17 '14 at 19:50
  • You're right, I had the gettext call there but that is not what this question is about. I would like the user roles to be translatable on-the-fly, meaning: If I switch to English the roles are displayed in English, and if I choose German I get the according German translations. All that without having to de- and then reactivate the plugin. I hope that makes sense now. – tfrommen Apr 17 '14 at 20:20
  • 1
    Yep, makes perfectly sense. And I don't think you can do more that what you've posted... on-the-fly translation are always a pain in WordPress. (Some ideas comes into my mind, but nothing better than your code) – gmazzap Apr 17 '14 at 20:42

The translate_user_role function is just a wrapper for translate_with_gettext_context, defining the context 'User role'.

In this last function, there is the filter hook gettext_with_context, which provides the context as well as the currently used domain.

So we could do this:

function wpdev_141551_translate_user_roles( $translations, $text, $context, $domain ) {

    $plugin_domain = 'plugin-text-domain';

    $roles = array(
        // ...

    if (
        $context === 'User role'
        && in_array( $text, $roles )
        && $domain !== $plugin_domain
    ) {
        return translate_with_gettext_context( $text, $context, $plugin_domain );

    return $translations;
add_filter( 'gettext_with_context', 'wpdev_141551_translate_user_roles', 10, 4 );

In order to make this work, we have to do a dummy gettext call, like so:

_x( 'Someone', 'User role', 'plugin-text-domain' );

We could just put this right after add_role.

This works, but it doesn't seem to be an efficient approach as every gettext call with a context has to pass our function(s).


You should use :

translate_user_role( __('some','textdomains ') );

example :

    translate_user_role( __('Developer',$this->textdomains ) ), 
    array( 'read' => true, 'level_12' => true ) 

https://developer.wordpress.org/reference/functions/remove_role/ https://developer.wordpress.org/reference/functions/translate_user_role/

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.