wordpress allow user to change their role - wordpress

I have a wordpress membership site which i am developing.
I want to be able to give users the ability to close there account if they no longer wish to use it.
I would use wp_delete_user but we wish to keep the data of the user for future marketing.
My idea is to give them the option to close there account, but when doing this all it actually does is change their role to "pending" or something similar.
Is there a wordpress function / hook i can use which will allow the user to do this within the front end of the site in their profile area?
Cheers Dan

Managed to figure this out so thought i would post it up incase others had the same issue:
<?php $user_id = $current_user->ID ; $new_role = 'pending'; wp_update_user( array ('ID' => $user_id, 'role' => $new_role ) ) ; ?>

Related

How to count only active users as referred users?

I am using self-hosted WordPress. And, I need a refer system for my site. It's a membership type site.
Where I offer refer code to some specific members. So, they can refer others.
And, when someone sign ups through their refer link, they will get a point.
For referral code, I am using WP Referral Code (https://wordpress.org/plugins/wp-referral-code/).
And, for membership I am using Ultimate Member Plugin!
But the issue is, WP Referral Code is counting registrations when someone hit the sign up button!
Even If I reject he membership via Ultimate Plugin, its still being counted!
How can i count only valid signups which are active right now? Not all sign ups, just manually approved sign ups.
update WP Referral Code to new version and paste this in your functions.php file
add_filter( 'wp_referral_code_validate_submission', function ($result){
return false;
}, 1, 1);
add_action( 'wp_referral_code_before_refer_submitted', function ( $new_user_id, $referrer_user_id){
update_user_meta( $new_user_id, 'shalior_referrer_id', $referrer_user_id);
} , 10 , 2);
add_action( 'um_after_user_is_approved', 'shalior_um_after_user_is_approved', 10, 1 );
function shalior_um_after_user_is_approved( $user_id ) {
$referrer_user_id = get_user_meta( $user_id, 'shalior_referrer_id', true);
update_user_meta( $user_id, 'wrc_referrer_id', $referrer_user_id );
wp_referral_code_add_user_to_referrer_invite_list($user_id , $referrer_user_id);
}

Wordpress ACF Pro & CPT / User Logged In = User in Field Type

I need a little help, if anyone can!
Using ACF Pro, and CPT, I have made a front end form where an admin can choose a user name and other specific information, then click submit and the post is published.
What I want to do now, is only allow the chosen user to see that post as long as they are logged in. Meaning, the chosen user is logged in and can only see the new post that has their name selected.
Thanks in advance! I’ve tried just about everything.
When creating the post, don't store just the username - store the user ID as well as a meta value on the post. Then, for the logged-in user, get the current user get the current user prior to querying your posts, and more specifically, get the user's ID. Then, pass that in your post query to limit the results to just that user's "tagged" posts.
Something like this (assuming we're on an archive, your post type is 'messages'), and you stored the "tagged" user id in a field named tagged_user_id:
add_action( 'pre_get_posts', 'get_users_posts' );
function get_users_posts( $query ){
if( $query->is_main_query() && is_post_type_archive( 'messages' ) && is_user_logged_in() ){
$current_user = wp_get_current_user();
$query->set( 'meta_key', 'tagged_user_id' );
$query->set( 'meta_value', $current_user->ID );
}
}
Doing this in your functions.php file, you ensure that any logged-in user on the 'messages' archive page only sees his/her messages. You can adapt it for single-messages as well, and you'll want to handle users who are not logged in as well (I presume).

How to share database user information between two WordPress installations?

I am currently working on two WordPress installations with the PointFinder theme. One is the main website (website A), whereas the other is a sub website accessible through the main website (website B).
The goal is, that the website A shares it's user information (users, usermeta, comments) with website B.
Website A
|_ Website B (this one should access the user info from website A)
I already studied the database schema and some of the php files. Theoretically, I could define a second database connection in 'wp-config.php' and point every call to the users, usermeta and comments tables within the website B's php files to the database from website A.
But this seems quite extensive and error-prone. Any other ideas how to solve this issue?
UPDATE/SOLUTION:
As codiiv proposed in his answer below, sharing user/meta data between different WP sites is quite easy. Don't forget to add the script that updates the missing user roles when new users are added. Here's the most comprehensive link I found:
https://kinsta.com/blog/share-logins-wordpress/
1) To share users and usermeta information you can add the following lines of code in your secondary (sub site) wp-config.php file
define('CUSTOM_USER_TABLE', 'wp_users');
define('CUSTOM_USER_META_TABLE', 'wp_usermeta');
You will need to replace the wp_ with the right prefix
2) For Comments, or other tables, I haven't done enough research but I think you can achieve that as long as the two are on the same server. Or you can create a custom RSS feed for comments(that include the full comment), and parse that in the subsite.
Sync all User Roles between two Wordpress Installs sharing the same wp_users and wp_usermeta tables.
function ksu_save_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = 'first_';
// Site 2 prefix
// Change value if needed
$prefix_2 = 'second_';
$caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
$level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );
if ( $caps ){
update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
}
if ( $level ){
update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
}
}
add_action( 'add_user_role', 'ksu_save_role', 10, 2 );

Wordpress - Change forum role from outside bbPress by checking user status

I would like to check the user status and update their bbPress forum role accordingly. (Not the Wordpress role.)
The purpose is to add functionality to the BP-Registration-Options plugin that moderates user registration (In BuddyPress. Currently the plugin sets the user status to 69 while the user is unapproved, and blocks access to BuddyPress functionality. However, the user is still able to login.
When they log-in, bbPress automatically sets the user forum role according to your setting in the back end. In this case it is set to 'spectator'. When the user is approved by the admin their status changes and I want it to also update the bbPress role to 'participant'.
Here is my first attempt:
function bp_registration_options_additional() {
if ( is_user_logged_in() ) {
$user_ID = get_current_user_id();
$user = get_userdata( $user_ID );
if (69 !== $user->user_status ) {
// Here is where I need help.
//How to set the bbPress forum role to 'Participant'?
}
}
}
add_action( 'wp_loaded', 'bp_registration_options_additional' );
Thanks!
WP: 3.8
bbPress: Version 2.5.2
EDIT: A bit of additional information. The meta_key for the forum roles is: wp_capabilities. The meta_value of a user with the forum role 'spectator; is: a:2:{s:10:"subscriber";b:1;s:13:"bbp_spectator";b:1;}
Had the same requirement - to update the wp_capabilities field -
$wp_user_capabilities_arr = array(
"subscriber" => true,
"bbp_participant" => true
);
update_user_meta($wp_user_id, "wp_capabilities", $wp_user_capabilities_arr);
And after update the data looks like:
a:2:{s:10:"subscriber";b:1;s:15:"bbp_participant";b:1;}

How to know the role of current user in WordPress?

While a user is creating a new post, how do I determine his current role?
I'm assuming you know what hooks of Wordpress you want to use. So skipping that part, it's pretty easy to get the current role of the user
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array
Now, you can iterate over that array to see if the user has a particular role.
Or, you can use current_user_can to look for specific capabilities, if you just want to check whether or not a user has a specific permission versus whether or not they're in the role. For example:
if (current_user_can('delete_posts')) {
//display the delete posts button.
}
This code will help you
<?php echo array_shift(wp_get_current_user()->roles); ?>

Resources