First of all, I’m new to programming so I could use a little help.
I have been given the task to make a custom JWT login plugin.
With that said, I have a small problem – one of them is that when I select roles in the plugin, they change only in the plugin and in the DB, but I want them to change in the WordPress Backend as well.
I am attaching a code snippet below, hopefully you will be able to help me further.
My PHP:
public function checkUserRole($apps)
{
$thisApp = get_option('kt_jwt_login_client_id');
$key = array_search($thisApp, array_column($apps, 'app'));
if (in_array('admin', $apps[$key]->roles)) {
return 'admin';
} elseif (in_array('manager', $apps[$key]->roles)) {
return 'manager';
} else {
return false;
}
}
/**
*
* Create User in WordPress if not already there
* @return int user ID
* @throws Exception
*
*/
public function createUserIfnotExists($userInfo)
{
//get fields from admin settings
$email = get_option('kt_jwt_login_email');
$first_name = get_option('kt_jwt_login_first_name');
$family_name = get_option('kt_jwt_login_last_name');
$full_name = get_option('kt_jwt_login_first_name') . ' ' . get_option('kt_jwt_login_last_name');
$company = get_option('kt_jwt_login_company');
if ($this->checkUserRole($userInfo->applications) == 'admin') {
$role = add_role('Administrator');
} elseif ($this->checkUserRole($userInfo->applications) == 'manager') {
$role = add_role('Contributor');
} elseif (strpos(strtolower($userInfo->$company), strtolower(get_option('kt_jwt_login_employee'))) !== false) {
$role = add_role('Author');
} else {
$role = add_role('Subscriber');
}
echo "Role: $rolen";
var_dump($role);
if (email_exists($userInfo->email)) {
$user = get_user_by('email', $userInfo->email);
$user_id = $user->ID;
$user->remove_role($role);
$user->add_role($role);
//update user data
$user_data = wp_update_user(array(
'ID' => $user_id,
'first_name' => $userInfo->first_name,
'last_name' => $userInfo->last_name,
'display_name' => $userInfo->first_name . ' ' . $userInfo->last_name,
'role' => $role
));
if (is_wp_error($user_data)) {
echo '<h1>There was an error; possibly this user does not exists</h1>';
} else {
echo '<h1>Success! User profile updated.</h1>';
}
// end of update user data
} else {
$user_id = wp_insert_user(array(
'user_login' => $userInfo->email,
'user_pass' => random_bytes(24),
'user_email' => $userInfo->email,
'first_name' => $userInfo->first_name,
'last_name' => $userInfo->last_name,
'display_name' => $userInfo->full_name,
'role' => $role
));
}
// Set or update woocommerce userinfo:
$this->updateWoocommerceMetaData($user_id, $userInfo);
// Set user role using taxonomy 'role':
$user = new WP_User($user_id);
$user->set_role($role);
return $user_id;
}
My frontend:
<div class="wrap">
<h1>Settings</h1>
<br>
<div class="container">
<div class="col-sm-12">
<div class="panel panel-primary">
<div class="panel-body">
<form class="form-horizontal" action="options.php" method="post" style="margin-left: 40px;">
<h4>Connection Settings</h4>
<div class="card" style="max-width: 650px">
<?php
settings_fields('admin_section');
do_settings_sections('plugin-options');
?>
<button type="submit"
class="btn btn-outline-success"
style="width: 20%; align-self: center; margin-top: 20px">
Save
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
...
function display_role_form_element()
{
$roles = wp_roles();
?>
<select name="kt_jwt_login_role" id="kt_jwt_login_role" style="padding: 0 176px 0 8px">
<?php
foreach ($roles->roles as $role => $roleObj) {
echo '<option value="' . $role . '" ';
if ($role == get_option('kt_jwt_login_role')) echo ' selected ';
echo '>' . $roleObj['name'] . '</option>';
}
?>
</select>
<?php
}
function display_userProfileUrl_form_element()