3

I'm using Justin Tadlock's Members plugin to create new roles and WP Role on Registration to display them on the registration form.

I've got a multilang site (English and Spanish) and I've localized my custom taxonomies using qTranslate.

I can't use qTranslate's quicktags in the Role Label field on the Member's plugin because it doesn't save it properly (even with $new_role_label = strip_tags( $_POST['role-label'] ); commented out on role-new.php) and I can't go into the database to make the changes there because then it breaks.

I've seen very scarce info about the WP function translate_user_role but I'm not sure it. There's not much else to go on.

3

I don't know the function you mention either, but I would venture to guess that that applies to default roles only anyhow.

Still, the link you posted does contain a pointer in the right direction. It speaks of "dummy gettext calls", which is exactly what you have to do.

In the following we will adjust the global $wp_roles object on init and hook a filter to the get_option( 'wp_user_roles' ) call.

function wpse92529_translatable_role_names() {
    global $wp_roles;

    $roles = $wp_roles->roles;
    $role_names = $wp_roles->role_names;

    /* define the translatable roles here */
    $translated_roles = array(
        'superuser' => _x( 'Superuser', 'Role Names', 'your-text-domain' ),
        'custom-role' => _x( 'The custom role', 'Role Names', 'your-text-domain' )
    );

    foreach ( $roles as $role_slug => $role_info ) {
        if ( array_key_exists( $role_slug, $translated_roles ) ) {
            $roles[$role_slug]['name'] = $translated_roles[$role_slug];
            $role_names[$role_slug] = $translated_roles[$role_slug];
        }
    }

    $wp_roles->roles = $roles;
    $wp_roles->role_names = $role_names;

    return $roles;
}

add_action( 'init', 'wpse92529_translatable_role_names', 1 );
add_filter( 'option_wp_user_roles', 'wpse92529_translatable_role_names', 11 );

The values of the $translated_roles array will now appear in your theme's / plugin's .pot file.

  • Indeed it's a very helpful function, thanks for sharing! Anyway, I was wondering if is it possible to filter the user role names returned by $wp_roles->get_names(); - If any plugin or theme uses the global $wp_roles object with along the get_names() function, it seems not to have any effect for the user roles names returned by those. – Adriano Monecchi Feb 17 '17 at 16:54

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.