I am using the wp_get_current_user()
function inside a hook which fires before email is sent from contactform7 . The function works but the main problem is that the function doesnt return the current user , if i provide the userid manually then the function works else it does not.
The function -
add_action('wpcf7_before_send_mail', 'ccdit',10,1);
function ccdit($cf7) {
try{
$wpcf7 = WPCF7_ContactForm::get_current();
$u = wp_get_current_user();
if($wpcf7->id() == 505) {
if ( is_user_has_role('phone_verified')==false ) {
$u->add_role('phone_verified');
}
}
}
catch(Exception $e){
throw new Exception($e.Message);
}
}
function is_user_has_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
}
else {
$user = wp_get_current_user();
}
if ( !empty( $user ) ) {
return in_array( $role, (array) $user->roles );
}
else
{
return false;
}
}
Tried many things to make it work but it doesnt work.