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.
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));
?>
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");
?>
Please
include
wp-load.php
and load file is on root. So please include load fileThe 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):
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?
There is error in your code try this:
You are calling
$current_user
directly which gives array as output.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.
I hope it will help you. â»â»â»â»