Display get_user_role and get_author_role function in English

Asked

Viewed 30 times

0

I’m developing a Wordpress application that uses the function get_author_role, however I am unable to make her appear in Portuguese.

The first function modifies the default names:

function change_role_name() {
    global $wp_roles;
    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

    // Administrador - tem acesso a virtualmente todas as funções de administração.
    $wp_roles->roles['administrator']['name'] = 'Administrador';
    $wp_roles->role_names['administrator'] = 'Administrador';

    // Editor - pode publicar e gerenciar posts e páginas, além de gerenciar posts de outros usuários.
    $wp_roles->roles['editor']['name'] = 'Moderador';
    $wp_roles->role_names['editor'] = 'Moderador';

    // Autor - só pode publicar e gerenciar seus próprios posts.
    $wp_roles->roles['author']['name'] = 'Suporte';
    $wp_roles->role_names['author'] = 'Suporte';

    // Colaborador - pode escrever e gerenciar suas posts, mas não publicá-los: são enviados para revisão de além com função superior.
    $wp_roles->roles['contributor']['name'] = 'Estagiário';
    $wp_roles->role_names['contributor'] = 'Estagiário';

    // Assinante - só pode gerenciar o próprio perfil, mas não escrever posts. Útil para marcação de favoritos, gerenciamento de comentários, etc.
    $wp_roles->roles['subscriber']['name'] = 'Membro';
    $wp_roles->role_names['subscriber'] = 'Membro';
}

add_action('init', 'change_role_name');

The second shows what is the level of the author of the post:

function get_author_role() {
    // https://stackoverflow.com/questions/10489214/getting-an-authors-role-in-wordpress
    global $authordata;
    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);
    return $author_role;
}

I call the function with the code below:

<?php echo get_author_role(); ?>

The problem starts when and to display the function get_author_role, instead of appearing in writing Administrator, appears written Administrator.

I’ve tried every method available on the internet, tried the translate_user_role however it did not work.

1 answer

0


After 8 days looking for a solution, I managed to solve by looking at the wordpress site.

Solution link: By Vladimir Garagulia (@shinephp)

Assuming $authordata contains a valid author user function array

function get_author_role_name()
{
    global $authordata;

    $author_roles = $authordata->roles;
    $role = array_shift($author_roles);
    $wp_roles = wp_roles();
    // take role display name by role ID
    $role_name = $wp_roles->role_names[$role];

    return $role_name;
}

Use this function then where you want to output the display name of the author function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.