WordPress wp_get_current_user() not showing result

I’m just testing some PHP to try and find out what role a user has because I want to try and do the following:

foreach($current_user->roles as $key => $value){
    if($value == ''){
        $npl= true;
    }
}

Basically updating $npl to true if no user role is chosen.

Read More

The code below only returns Array when it should be showing me a fair bit more than that.

<?php

include 'wp-blog-header.php';

$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$user = new WP_User( $user_id );

echo "id:" . $current_user;

        foreach($current_user->roles as $key => $value){
            if($value == ''){
                echo "yes";
            }
            else {
                echo "no";
            }
        }

print_r(array_values($user->roles));

?>

array

I’ve tried at several stages to make it work but showing the id early isn’t showing anything either

EDIT

I even tried:

<?php

include '../wp-blog-header.php';
include '../includes/wp-load.php';
include_once('../wp-config.php');

$current_user = wp_get_current_user();
$user_id = $current_user->ID;

echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';

foreach($current_user->roles as $key => $value){
    if($value == ''){
        echo "yes" . '<br />';
    }
    else {
        echo "no" . '<br />';
    }
}

print_r(array_values($current_user->roles));

echo '<br />' . "The time is " . date("h:i:sa");

?>

Related posts

3 comments

  1. Please include wp-load.php and load file is on root. So please include load file

    The simplest way to require/include wp-load.php

    If you want to use WordPress functionality in a PHP file that exists outside of your WordPress installation then you need to include wp-load.php. Perhaps you could call this “hooking into WordPress”.

    Maybe you’re already using some sort of relative path method, like:

    But this can create problems if directories change. You need a clean, dynamic way to get wp-load.php. So here is the simplest way to do it, with just two lines of code (place it at the very top of your file):

    <?php include '../../../wp-load.php'; ?>
    

    Short and sweet 🙂

    Disclaimer: This is intended for experimental and development purposes only. It is not advised to redundantly load WordPress on live production environments. But, why?

  2. There is error in your code try this:

    <?php
    
    include 'wp-blog-header.php';
    
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $user = new WP_User( $user_id );
    
    echo "id:" . $user_id;
    
        foreach($current_user->roles as $key => $value){
            if($value == ''){
                echo "yes";
            }
            else {
                echo "no";
            }
        }
    
        print_r(array_values($current_user->roles));
    
      ?>
    

    You are calling $current_user directly which gives array as output.

  3. I got your question and since yet, no one gave you the correct answer, i have what your are looking for. I used it recently on the WP API to give sensible access to some users based on if the current-user has the appropriate permission. Here is my code.

    function check_user_role(){
    
    global $ne;
    
    //check current user capabilities to determin the role type he pocess
    if (!current_user_can('activate_plugins')) {
        $ne =  "Subscriber";
    } else {
        $ne =  "admintrator";
    }
    
    $infos = array(
        'role' => $ne
    );
    
    return $ne;
    }
    
    echo check_user_role();
    

    I hope it will help you. ☻☻☻☻

Comments are closed.