Make additional wordpress profile fields uneditable - wordpress

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.

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.)

how to remove delete option when mouse hover on all users in wordpress?

I am working on a project in wordpress.
In user options, when I select all users it shows a list of all users with edit / delete option. In my project, I want to remove delete option.
What do I need to do for this
Here is a snippet that hides the delete option from user with specific role (administrator in this case)
$role = get_role( 'administrator' ); // This is the user role
$role->remove_cap( 'delete_users' ); // This is the capability you remove
Refer to the codex for more info. Here are the two functions you need to use:
remove_cap()
get_role()
roles and capabilities - https://codex.wordpress.org/Roles_and_Capabilities#delete_users
You could use CSS to hide the delete option from appearing on hover. The following code adds custom styles the Admin area.
add_action('admin_head', 'hide_user_delete_option');
function hide_user_delete_option() {
echo '<style>
.users-php tr:hover .row-actions .delete{
visibility: hidden;
}
</style>';
}

How to assign admin role to a user in wordpress?

I am working on a website where I have student guardian relationships. So when a guardian is visiting his student profile, he should be able to edit, delete all the fields that a user itself can do. I am using buddypress and have used rtmedia plugin. Now For example, student uploads media, images. By default, other user can't delete or edit the media. But in my case, guardian should be capable of editing and deleting students media. I am thinking that if I assign the admin role to guardian when he is viewing students profile then my problem can be solved. But I don't know how to assign a user amdin role. Thanks
I would recommend reading up on the User admin screen in the WordPress codex:
https://codex.wordpress.org/Users_Screen#Change_Roles_to
It explains how to switch roles for existing users, as well as assigning roles to existing ones:
https://codex.wordpress.org/Users_Add_New_Screen
You don't want to give them an admin role.
You want to check for a student/guardian relationship and if found allow edit access.
To allow such access, you'll have to do various operations, for example, in BP, filter the profile screens loop to allow editing.
Create php file to plugins directory and add below content
<?php
/* Plugin Name: Add role */
function add_roles() {
$result = add_role(
'admin',
__( 'Admin' ),
array(
'edit_posts' => true, // true allows this capability
'read' => true,
'delete_posts' => false, // Use false to explicitly deny
)
);
if ( $result !== null ) {
echo 'New role created';
}
else {
echo 'Role already exists..';
}
}
register_activation_hook( __FILE__, 'add_roles' );
Above more capabilities can be added accordingly
More about capabilities at http://code.tutsplus.com/articles/wordpress-roles-and-capabilities-the-basics--wp-25921
Activate plugin and check under users to assign admin role
for guardian role assign capabilities to delete or edit the media.
For guardian (after visiting his student profile to edit or delete all the fields that a user itself can do) assign admin role and assign capabilities same as student by editing capabilities of plugin.
Then after reactivate plugin.

Hide admin menu items and restrict access based on Role

i know about the way to show/hide menu items in admin based on user roles, but anyone can just type the address and access the specific menu. I was wondering if there is a way to restrict that as well.
For the moment i'm using this code:
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
// If the user does not have access to publish posts
if(!current_user_can('add_users')) {
// Remove the "Tools" menu
remove_menu_page('tools.php');
}
}
How about going the other way:
http://codex.wordpress.org/Function_Reference/add_role

How to add the user role in the registration form

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'];
}

Resources