Support » Plugin: Contact Form 7 » wp_get_current_user() return Null inside wpcf7_before_send_mail

  • Resolved ddevuono


    I need to get the current user login inside the wpcf7_before_send_mail() action in order to add it to the message of the email, but wp_get_current_user() is returning null instead of the current user.

    Here is my code:

    add_action("wpcf7_before_send_mail", "wpcf7_add_user_info_to_mail");    
    function wpcf7_add_user_info_to_mail ($wpcf7) {
            $submission = WPCF7_Submission::get_instance();
            if ( $submission ) {
    	        $posted_data = $submission->get_posted_data();
    	        $mail = $wpcf7->prop('mail');       
                    $current_user = wp_get_current_user();
    	    	$current_login = $current_user->user_login;	    	
                    $mail['body'] = 'User:'.$current_login.' - '.$mail['body'];
    		$wpcf7->set_properties(array('mail' => $mail));

    I’m using WordPress 4.7.5 and CF7 4.8.1

    Please let me know what i’m doing wrong or if I can get the user login info in a different way.

    Thanks in advance!!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Same here. WordPress 4.8.1 and CF7 also 4.8.1. Mine is an infrequently-used form that did work and now doesn’t, but haven’t pinpointed when yet. I’ll post here if I find something useful.


    wp_get_current_user() returns user with ID 0 in wpcf7_posted_data() and wpcf7_before_send_mail() action, probably in other actions as well.

    WordPress 4.8.1 and CF7 4.8

    • This reply was modified 2 years, 3 months ago by stajan.

    That’s what I found too @stajan. I don’t think it always worked like that but I could be wrong.

    In case it helps anyone I have a workaround for now but it does mean exposing user details (either login name or ID).

    Workaround is to add a hidden field to the form, which defaults to the user ID:

    [hidden current_user_id "user_id"]

    CF7 fills this in automatically (see notes in the built-in tag generator).

    Then in my wpcf7_before_send_mail() action I pull it out with:

       $posted_data = $submission->get_posted_data();
       $user_id = $posted_data['current_user_id'];

    From there I can do what my action is for (adding a user_meta item):

    add_user_meta($user_id, 'my_meta_key', 'my_meta_value');

    With the user ID you should be able to do whatever else you need.

    Putting user IDs in the page isn’t great practice, so this is a workaround, not a complete solution.


    Downgraded to CF7 v4.7 – seems to work now.

    Does anyone know if it works with the new version of contact form 4.9?

    • This reply was modified 2 years, 3 months ago by ddevuono.

    I tried cf7 v4.9 and has the same issue.

    I’ve had the same issue and find out it was because the author moved to the REST API. The submit request was sent without the rest’s nonce so the WordPress REST API downgraded the request to a logged-out user for security purposes.

    The author has to add the nonce to the submit request to fix this.
    I have specified which part of the plugin code has to be changed to fix this issue there:

    Please note that the code provided there is not fully tested so use it at your own risk (just grab a fresh copy of the plugin on if something breaks).
    and obviously editing any plugin’s file is NOT recommended.

    If anyone still needs the answer, the author of the plugin answered mathieuhays question and it works great. I just added the following line to my functions.php as the author suggested:

    add_filter( 'wpcf7_verify_nonce', '__return_true' );

    This is the author response:


    Use the subscribers-only mode or set the WPCF7_VERIFY_NONCE constant to true.

    It seems that most number of problems cound be solved by downgrading to 4.7.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_get_current_user() return Null inside wpcf7_before_send_mail’ is closed to new replies.