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 using the add_role function:
add_role( $role, $display_name, $capabilities );
Then use the add_cap function to add a new capability 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 );
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.
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.
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!
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. They have weekly chats there on Mondays at 16:00 UTC. There might be members hanging out there at any time, IDK.