0

I have an archive page to show each author domain.com/author. On the archive page for an author domain.com/author/john-doe I want to display few information like name, e-mail address and role like John Doe / [email protected] / Écrivain. (Écrivain is the human readable Label for my wordpress writer role).

I don't want to show the role of the author of the page but the readable role of the author for this archive page.

I know how to display the role of the author of the page (i.e creator of the page) which is of no interest for me here.

For the role, I have to use a short code and I can't succeed in showing the Label of the role with what I was able to find and understand from searching on the Web (I'm not a developer). It's just returning nothing:

// Shortcode pour récupérer le rôle de l'adhérent dans la page de recherche
add_shortcode('adherent_role', 'author_name_shortcode', true);
function author_name_shortcode(){
global $post;
$post_id = $post->ID;
$author = get_role($post_id);
return $author;
}

This is probably a very basic function, but what would you advise to improve it?

4
  • Can you explain this better? It's very confusing, especially the sentence with the parts written in bold. Are you trying to ask how to display the human readable label of a role rather than the raw internal name? e.g. administrator vs Administrator?
    – Tom J Nowell
    Commented Feb 29 at 11:45
  • yes the human readable name (edited) of the target author Commented Feb 29 at 12:59
  • so the label of the role, not the name of the user? Just clarifying as the readable name of the target author is their display name, be very specific and precise, with lots of examples
    – Tom J Nowell
    Commented Feb 29 at 14:55
  • I'm only writing about the role of the author and the role name. If John Doe is editor then it outputs Éditeur and if Jane Smith is writer it outputs Écrivain. I know how to display name and e-mail. What actually lacks is the role (under a readable label) of the target author. Commented Feb 29 at 15:08

2 Answers 2

1

You can use the main WP_Roles object to get an array of role names/labels, e.g.

$role = 'author';
$label = ''; // if there is no label this will be used instead
if ( wp_roles()->has_role( $role ) ) { // this protects us if the role does not exist.
    $role_names = wp_roles()->get_names();
    $label = $role_names[$role]; // Author
}
echo $label; // prints Author not author

translate_user_role will translate it into the currently configured locale if available.

Here's the output of get_names():

wp> wp_roles()->get_names();
=> array(14) {
  ["administrator"]=>
  string(13) "Administrator"
  ["editor"]=>
  string(6) "Editor"
  ["author"]=>
  string(6) "Author"
  ["contributor"]=>
  string(11) "Contributor"
  ["subscriber"]=>
  string(10) "Subscriber"
....
1
  • not sure to understand your answer :-/ Commented Feb 29 at 15:30
0

I'm posting what worked for me with the ability to have custom label as the readable name of the selected WP roles:

// Shortcode pour récupérer le rôle de l'utilisateur dans la page d'archive des utilisateurs
add_shortcode('adherent_role', 'get_user_role_shortcode');
function get_user_role_shortcode() {
// Obtenez l'ID de l'auteur de la page d'archive d'auteur actuelle
$author_id = get_queried_object_id();

// Vérifiez si l'ID de l'auteur est valide
if (!empty($author_id)) {
    // Obtenir les rôles de l'utilisateur
    $user_roles = get_userdata($author_id)->roles;

    // Définir les rôles à afficher
    $allowed_roles = array('subscriber', 'anonymous');

    // Filtrer les rôles de l'utilisateur pour ne garder que ceux autorisés
    $filtered_roles = array_intersect($user_roles, $allowed_roles);

    // Vérifier s'il y a des rôles autorisés pour cet utilisateur
    if (!empty($filtered_roles)) {
        $output = ''; // Initialisation de la sortie du shortcode

        // Boucle pour obtenir tous les rôles autorisés
        foreach ($filtered_roles as $role) {
            // Remplacer les rôles par leurs libellés respectifs
            $role_label = ($role == 'subscriber') ? 'Adhérent•e' : 'Ex-adhérent•e';
            $output .= $role_label . ', ';
        }

        // Retirer la virgule et l'espace en trop à la fin de la chaîne
        $output = rtrim($output, ', ');
    } else {
        $output = 'Aucun rôle trouvé pour cet utilisateur.';
    }
} else {
    $output = 'Aucun utilisateur trouvé sur cette page.';
}

return $output;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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