How to add the user role in the registration form - wordpress

I want to display the user role field in the wordpress user registration form.
I did not see any option form the admin side.
How will I do this? So that the user select his role in the registration form. If he is a student then he will select student role otherwise staff.
Any help?

In Wordpress, the roles are stored in $wp_roles global variable. So, if you want to display all the roles try this:
global $wp_roles;
foreach ($wp_roles->roles as $role) {
echo $role['name'];
}

Related

Show custom tabs on specific user role profiles in Youzer plugin?

I made a custom tab in youzer profile and I need to show it to specific roles' profiles only.
Please let me know any hooks that I can use to accomplish this functionality.
Here's how to change the tabs using a filter. ($bpCoreNavItem is an instance of BP_Core_Nav_Item)
function filterProfileTabs(array $tabs) {
$newTabs = [];
foreach ($tabs as $bpCoreNavItem) {
if ($bpCoreNavItem->slug !== 'posts') {
$newTabs[] = $bpCoreNavItem;
}
}
return $newTabs;
}
add_filter('yz_profile_primary_nav', 'filterProfileTabs');
In that code I'm simply removing the "Posts" tab when finding it by its slug. To remove items based on user roles, you'll want to look up the user's role and implement any logic based on the given role. (Ex. Only remove "Posts" for users without the administrator role or the like.)

Wordpress custom link for every user

How can i create a page without creating an actual page? I want to create websitelink.com/username where username will be the username of every user on my site. I want a profile page of every user and they can view other profile too. User dont have admin privileged. Thank you. I dont want to use plugin because I need to customize the view. I also want to view user profile even not logged in.
You can create custom page template and then write a code to get user by username. So your link will be like websitelink/users/{username} username will be dynamic.
<?php
/**
* Template Name: User Profile Page
**/
// Getting the username from the url
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
$loginname = end($link_array);
// Getting user info by wordpress default function
$user = get_userdatabylogin($loginname);
if($user){
echo $user;
}
$user will have all the info which you will need.

Make additional wordpress profile fields uneditable

I have created extra profile fields for WP users but I want to make sure they are only editable by admins, is this possible?
Thnaks
Pam
While adding the fields, you need to check if logged in user has admin capabilities or not.
if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
} else {
/* A user without admin privileges */
}
accordingly you can show/hide the fields as per the user role.

WordPress plugin to add user role using textbox and form submit

I'm trying to create a plugin where an admin (custom defined user role, not regular administrator) can access an options page, enter a specific username into a textbox, hit submit, and by doing so add a second user role to that user. I know that this code:
$user = new WP_User( null, 'username' );
$user->add_role( 'admin' );
will add the "admin" role to someone with the username "username." So in my plugin's settings page, I have a textbox:
Add an Admin: <input type="text" size="57" name="sscaur_options[txt_admin]" value="<?php echo $options['txt_admin']; ?>" />
and then this at the end:
function sscaur_add_admin($textbox) {
$options = get_option('sscaur_options');
$textbox = $options['txt_admin'];
$user = new WP_User( null, '{$textbox}' );
$user->add_role( 'admin' );
return $textbox;
}
I know that's totally wrong, but don't know how to begin to make it right.
I know that adding second user roles can be done with the User Role Editor plugin, but that's hardcoded to only give access to actual administrators, and I need my custom user roles to be able to add roles to specific users without being full administrators.

Drupal: redirecting users with specific role to another page

How can I redirect only users with a specific role to the admin page when they login ?
Well, you need to get the global $user variable. And then check if the user has the desired role, (in this case I assumed the desired role is 'authenticated user'), and the current page is not the admin page (so you don't get a redirection problem), then redirect him to the admin page, or a page of your choice inside drupal_goto('admin');
There you go:
<?php
global $user;
if(in_array('authenticated user', $user->roles) && arg(0) != 'admin') {
drupal_goto('admin');
}
?>

Resources