[英]Wordpress how can i display user role on custom page?
How can I display the user role on a custom page with php? 如何使用php在自定义页面上显示用户角色? There's one code, but it's just works on the author.php page. 只有一个代码,但是仅在author.php页面上有效。 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.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 在任何自定义页面中,您可以调用以下三行以拉起WordPress堆栈,然后使用任何WordPress功能
<?
//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];
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.