0

It is possible to obtain the user role in Wordpress from an external PHP application?

I explain myself better: I am using the OAuth server plugin in my Wordpress site and I have created a script in another PHP site so that it is only possible to access this site if the user is registered in the Wordpress site. Now what I need is to obtain the role of the user that is entering my PHP site, using Curl I have been able to obtain the Id assigned to the user in Wordpress but I need the role to assign privileges in my PHP site. Any way to do this? How do I get the user's credentials from an external PHP site?

2
  • Note that roles are just collections of capabilities, if you had a capability you were looking for, then that would be significantly easier to answer, and significantly more useful
    – Tom J Nowell
    Commented Jul 30, 2018 at 21:56
  • As Tom said roles are just capabilities. Capabilities are stored in the wp user meta table. So any script that has access to the DB & knows the wp prefix for the site, can connect to the data base, and then query the prefix_usermeta for metavalues with user id and meta key = prefix_capabilties. The value will ve a serialised array of capabilties. Unserialise and check for the capability the user should have. See i.imgur.com/MhrbP0r.jpg
    – anmari
    Commented Jul 30, 2018 at 23:49

1 Answer 1

0

I wanted the logged in user to get one form from other application only if he/she is Administrator and here is how I checked it. You can check for any role (even new role made through custom user role).

<?
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');  //to load from any folder
$current_user = wp_get_current_user();
$user_info = get_userdata($current_user->ID);
if (in_array("administrator", $user_info->roles)) {
//greet or any other function to call if you want
} else {
die("Login as Admin to continue");
}
// other code below if user passed your role requirement
// otherwise the content will not be shown to him/her.
?>

We have to use in_array for checking user roles because you can assign multiple roles to users.

I saved that code in "chkuser.php" and in the beginning of any other php page, I do <? include "chkuser.php"; ?> and program will stop there if user was not an Administrator.

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.