Support » Plugin: Contact Form 7 » wpcf7_before_send_mail and is_user_logged_in()

  • Resolved Colir



    I’m trying to check if the current user is logged in before sending my CF7 form.
    I would like to use this to modify the subject of my email.

    But i can’t get this work… this returning NULL

    here is my code

    function before_send_mail_handler($wpcf7){
    	global $current_user;
    	$mail = $wpcf7->prop('mail');
    	$mail['subject'] = ( is_user_logged_in() ) ?  $mail['subject'] :  $mail['subject'].' - Utilisateur non connecté';
    	$form_name = $wpcf7->name();
    	$mail['recipient'] = get_form_email($form_name);
    	$wpcf7->set_properties(array("mail" => $mail));
    	if($form_name == 'request_access' || $form_name == 'request_access-2'){

    Thank you for any help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Takayuki Miyoshi


    This function returns nothing so it’s obvious you see NULL returned.

    Thread Starter Colir


    I’m sorry Takayuki, but i don’t understand your answer.

    My mail subject is well modified, but the function is_user_logged_in();

    return null (

    thank you

    I had the same problem.

    The quick-fix is to save the logged user ID in a global variable while in the init hook, and then check if it’s not zero in your wpcf7_before_send_mail hook :

    $GLOBALS[‘wpcf7_author_id’] = wp_get_current_user()->ID;

    add_action( ‘wpcf7_before_send_mail’, function($contact_form ){
    if($GLOBALS[‘wpcf7_author_id’] != 0){
    // code for logged user

    That solved the problem for me.

    I don’t know why is_user_logged_in() is not working in the wpcf7_before_send_mail hook, but I suspect whether other part of the code mess with it before or something in relation with hook order. Not sure.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wpcf7_before_send_mail and is_user_logged_in()’ is closed to new replies.