WordPress.org

Support

Support » Plugins and Hacks » Contact Form 7: Automatically send with username and email

Contact Form 7: Automatically send with username and email

Viewing 10 replies - 1 through 10 (of 10 total)
  • i’m facing the same problem, did you sort it out already?

    No. I’m hoping that somebody around here knows how Contact Form 7 works.

    I managed to hack my way to a solution regarding sending the username of the logged in user.

    To the file plugins/contact-form-7/modules/special-mail-tags.php I added this code:

    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_userdata', 10, 2 );
    
    function wpcf7_special_mail_tag_userdata( $output, $name ) {
    
    	// For backwards compat.
    	$name = preg_replace( '/^wpcf7\./', '_', $name );
    
    	global $current_user;
    
    	if ( 'user_login' == $name )
    		$output = $current_user->user_login;
    
    	return $output;
    }

    This allows me to use the special mail tag [user_login] just like these special mail tags:
    http://contactform7.com/2009/12/25/special-mail-tags/

    JonasVorwerk
    Participant

    @jonasvorwerk

    Is there also a way to take a email address from a customfield from the post/page where the form is placed?

    @jonas – I just posted a way to output custom fields in another thread, might help achieve what you’re trying to do http://wordpress.org/support/topic/plugin-contact-form-7-mail-recipient-always-different?replies=2#post-1641672

    propulso
    Member

    @propulso

    Hi,

    my problem is the same. I need the forms sent to me, with the logged user name in it. I tried modifying special-mail-tags.php as Jacob Smith said, and it worked. The mail was sent with the user name using a [user_login] tag. But, when I tried to log out, I got problems:

    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-login.php on line 337
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-login.php on line 349
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 717
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 718
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 719
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 720
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 721
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 722
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 725
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 726
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 727
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 728
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 731
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 732
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 733
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 734
    
    Warning: Cannot modify header information - headers already sent by (output started at /XXX/wp-content/plugins/contact-form-7/modules/special-mail-tags.php:86) in /XXX/wp-includes/pluggable.php on line 890

    What did I do wrong? Could you please give the full special-mail-tags.php text so that I can use it in my site?

    Thanks a lot.

    propulso
    Member

    @propulso

    No answers?

    Jakob Smith
    Member

    @diakrisis

    @propulso: Just saw your comments now.

    Here is the full contents of my special-mail-tags.php:

    <?php
    /**
    ** Filters for Special Mail Tags
    **/
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag', 10, 2 );
    
    function wpcf7_special_mail_tag( $output, $name ) {
    
        // For backwards compat.
        $name = preg_replace( '/^wpcf7\./', '_', $name );
    
        if ( '_remote_ip' == $name )
            $output = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
    
        elseif ( '_url' == $name )
            $output = get_option( 'home' ) . wpcf7_get_request_uri();
    
        elseif ( '_date' == $name )
            $output = date_i18n( get_option( 'date_format' ) );
    
        elseif ( '_time' == $name )
            $output = date_i18n( get_option( 'time_format' ) );
    
        return $output;
    }
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_for_post_data', 10, 2 );
    
    function wpcf7_special_mail_tag_for_post_data( $output, $name ) {
    
        if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
            return $output;
    
        if ( ! preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $_POST['_wpcf7_unit_tag'], $matches ) )
            return $output;
    
        $post_id = (int) $matches[2];
    
        if ( ! $post = get_post( $post_id ) )
            return $output;
    
        $user = new WP_User( $post->post_author );
    
        // For backwards compat.
        $name = preg_replace( '/^wpcf7\./', '_', $name );
    
        if ( '_post_id' == $name )
            $output = (string) $post->ID;
    
        elseif ( '_post_name' == $name )
            $output = $post->post_name;
    
        elseif ( '_post_title' == $name )
            $output = $post->post_title;
    
        elseif ( '_post_url' == $name )
            $output = get_permalink( $post->ID );
    
        elseif ( '_post_author' == $name )
            $output = $user->display_name;
    
        elseif ( '_post_author_email' == $name )
            $output = $user->user_email;
    
        return $output;
    }
    
    add_filter( 'wpcf7_special_mail_tags', 'wpcf7_special_mail_tag_userdata', 10, 2 );
    
    function wpcf7_special_mail_tag_userdata( $output, $name ) {
    
    // For backwards compat.
    
    $name = preg_replace( '/^wpcf7\./', '_', $name );
    
    global $current_user;
    
    if ( 'user_login' == $name )
    $output = $current_user->user_login;
    
    return $output;
    }
    
    ?>

    I hope it helps; I haven’t encountered that error.

    It seems that that kind of error can be related to spaces before and/or after PHP code in various files:
    http://www.hongkiat.com/blog/wordpress-error-warning-cannot-modify-header-information/

    Hi Jacob,

    sorry I didn’t comment before just to thank you for your help. It works just FINE. Great job, great help, big thanks.

    🙂

    mdiessner
    Member

    @mdiessner

    It would be great if this became a DEFAULT option otherwise everytime there is an update of the plugin this gets overwritten.

    If a blog user is logged in, then the email/name should be pre-populated by default.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Contact Form 7: Automatically send with username and email’ is closed to new replies.