Title: Changing user&#8217;s function with sql and php code
Last modified: February 16, 2018

---

# Changing user’s function with sql and php code

 *  Resolved [David Junior](https://wordpress.org/support/users/pondwar/)
 * (@pondwar)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/)
 * Hello, I am trying to create a function to change the database table wp_usermeta.
   
   Where do I need to fetch the id of the logged-in user and change the role of 
   that logged-in user only. I’m creating a site where I need to give specific access
   to a page only to users registered with this function, and for me not to have
   to change manually, I would like to have a link where the user clicks and changes
   the function automatically. I can not leave it as a default function because 
   it will have more functions and each user will click on a certain link.
 * Ex. I am a user and my function needs to be “sun” so I will click the “SUN” button
   and my function will change automatically, and from there I will have access 
   to the “sun” part of the site.
 * In theory I would need to fetch the logged-in user ID and only change the capabilities
   of wp_usermeta all via sql.
    Anyone have any ideas to help me.
 * Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [danieltj](https://wordpress.org/support/users/danieltj/)
 * (@danieltj)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9982512)
 * It sounds like what you’re after will be something that is very custom and specific
   so I think you’ll need to do some special development work using the Roles API
   to get what you’re after.
 * I’d suggest [creating a new role](https://codex.wordpress.org/Function_Reference/add_role)
   using the `add_role` function:
 * `add_role( $role, $display_name, $capabilities );`
 * Then use the `add_cap` function to [add a new capability](https://codex.wordpress.org/Function_Reference/add_cap)
   that allows a user access to a specific area/page or button.
 * `$wp_roles->add_cap( $role, $cap );`
 * Like I said though, it will require a lot of custom development to get what you’re
   after. You then need to add the custom role to the user using something like 
   this:
 *     ```
       $user = new WP_User( $user_id );
       $user->add_role( $role );
       ```
   
 *  Thread Starter [David Junior](https://wordpress.org/support/users/pondwar/)
 * (@pondwar)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9990650)
 * Thanks for the info, I have solved the problems to create the capabilities, I
   found a plugin that helped me very well, now my problem is in creating a function
   for the user to select which role he wants to belong ‘sun’ or ‘moon’.
 * Ex. I register and I want to belong to the sun group so I click on the button‘
   Participate in the sun group’ or ‘Join the moon group’
 * However I am still in doubt on what functions I should work on and how my SQL
   should handle those changes.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9990853)
 * Look through the wp-admin/user-edit.php file. It outputs the user profile screen.
   There are a number of actions you can hook where you can output custom fields
   at certain locations. Output a field that lets the user select their group preference.
 * Also hook ‘personal_options_update’ action. Check if your custom field’s value
   is in $_POST, indicating the form has been submitted. If so, add a role or capability
   to the user object like Daniel had suggested. The add_cap() method manages the
   SQL, you don’t need to fuss with it yourself.
 * I’m assuming you agree that the profile page is a good place for such a field.
   Any number of alternate locations is possible. The same concept would apply, 
   but of course the actions to hook will change.
 *  Thread Starter [David Junior](https://wordpress.org/support/users/pondwar/)
 * (@pondwar)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9991652)
 * Thanks for the help and the information was a great help.
    As I’m trying to create
   a QR Code reader to make these changes for me I decided to make a page and not
   a plugin or a call in the includ.php of the theme, so it looked like this:
 *     ```
       add_action( 'profile_update', 'my_profile_update', 10, 2 );
   
       if (is_user_logged_in()){
   
       $user = my_profile_update(wp_get_current_user()->ID);
   
       echo array_shift($user->roles);
       }else{
       	echo "<br><p>Deslogado<br></p>";
       }
   
       function my_profile_update( $user_id ) {
       	$u = new WP_User( $user_id );		
       	$u->set_role('subscriber');
       	return $u;
       }
       ```
   
 * So whenever I call this page.php it looks up the current logged-in user ID and
   changes its role.
 * Now a next doubt taking advantage of the open forum:
 * I need to create a QR Code reader that will call this page for all the readers
   I encounter or it from the problem with the Android camera or IOS camera, someone
   knows some way or some library to make it work for both.
    Or wordpress has something
   similar already developed.
 * Thank you so far for everything!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9994676)
 * You’re welcome. QR code reading is outside my knowledge base, I’m not too sure
   if anything exists because camera resources are not available to mobile browsers
   AFAIK. I might be wrong, but I believe you would need to be running an installed
   app to access camera resources. Maybe someone with better knowledge can shed 
   some light.
 * There is a WP app available. Someone in the mobile group would have a better 
   idea. You can reach them in the #mobile channel of [WP Slack](https://make.wordpress.org/chat/).
   They have weekly chats there on Mondays at 16:00 UTC. There might be members 
   hanging out there at any time, IDK.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Changing user’s function with sql and php code’ is closed to new replies.

## Tags

 * [capabilities](https://wordpress.org/support/topic-tag/capabilities/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 3 months ago](https://wordpress.org/support/topic/changing-users-function-with-sql-and-php-code/#post-9994676)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
