So I am running the following to update user meta on regsitration:
function set_user_rcp_default_subscriber($user_id) {
update_user_meta( $user_id, 'wp_user_level', '0' );
update_user_meta( $user_id, 'rcp_subscription_level', '1' );
update_user_meta( $user_id, 'rcp_status', 'active' );
update_user_meta( $user_id, 'rcp_expiration', '2014-06-30' );
}
add_action("user_register", "set_user_rcp_default_subscriber", 10, 1);
However, I only want this to apply to users that are registered as the default type (subscriber in my case).
Users that are registered with a different role type I dont want to update with these meta values.
Any help very much appreciated.
Many thanks.
So I am running the following to update user meta on regsitration:
function set_user_rcp_default_subscriber($user_id) {
update_user_meta( $user_id, 'wp_user_level', '0' );
update_user_meta( $user_id, 'rcp_subscription_level', '1' );
update_user_meta( $user_id, 'rcp_status', 'active' );
update_user_meta( $user_id, 'rcp_expiration', '2014-06-30' );
}
add_action("user_register", "set_user_rcp_default_subscriber", 10, 1);
However, I only want this to apply to users that are registered as the default type (subscriber in my case).
Users that are registered with a different role type I dont want to update with these meta values.
Any help very much appreciated.
Many thanks.
Share Improve this question asked Aug 19, 2013 at 6:29 user1385827user1385827 852 silver badges9 bronze badges2 Answers
Reset to default 0Here is what you need to do:
function set_user_rcp_default_subscriber($user_id) {
$user = new WP_User( $user_id );
foreach( $user->roles as $role ) {
if ( $role === 'subscriber' ) {
update_user_meta( $user_id, 'wp_user_level', '0' );
update_user_meta( $user_id, 'rcp_subscription_level', '1' );
update_user_meta( $user_id, 'rcp_status', 'active' );
update_user_meta( $user_id, 'rcp_expiration', '2014-06-30' );
}
}
}
add_action("user_register", "set_user_rcp_default_subscriber", 10, 1);
We need to loop through it because potentially a user can have multiple roles.
You can achieve this by checking whether user role is subscribe or not by using user_can function and checking it in the user_register action hook function as following.
function set_user_rcp_default_subscriber($user_id) {
if ( !user_can( $user_id, 'edit_post') ) {
update_user_meta( $user_id, 'wp_user_level', '0' );
update_user_meta( $user_id, 'rcp_subscription_level', '1' );
update_user_meta( $user_id, 'rcp_status', 'active' );
update_user_meta( $user_id, 'rcp_expiration', '2014-06-30' );
}
}
add_action("user_register", "set_user_rcp_default_subscriber", 10, 1);