简体   繁体   中英

Wordpress how can i display user role on custom page?

How can I display the user role on a custom page with php? There's one code, but it's just works on the author.php page. How do I run this code on a custom page?

<?php $aid = get_the_author_meta('ID'); 
$role = get_user_role($aid); 
if ('subscriber' === $role)
{
  echo "Subscriber";
} 
elseif ('editor' === $role)
{
      echo "Editor";
}`?>`

Function.php

function get_user_role($id) {
    $user = new WP_User($id);
    return array_shift($user->roles);
}

In any custom page you can call below three lines to pull up WordPress stack and then use any WordPress functionality

<?
    //Imp to include
    include('wp-load.php');
    define('WP_USE_THEMES', false);
    require('wp-blog-header.php');

    // check is user is logged - if yes then print its role
    if(is_user_logged_in() ) {
        $user = wp_get_current_user();
        $role = ( array ) $user->roles;
        echo "role is ".$role[0];

    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM