hooks - wp_get_current_user Not working

admin2025-06-02  1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748862452a314341.html

最新回复(0)