• AJ

    (@permaculturetreegeek)


    In case anyone else has this question. I found most of this solution online… and modified it a bit.

    Add the following code to the functions.php file of contact form 7 plugin. It is commented to help with configuration.

    /*
    *********************************************
    		contact 7 subscribe to wysiga email list
    *********************************************
    	*/
    
    function wpcf7_array_flatten( $input ) {
    	if ( ! is_array( $input ) )
    		return array( $input );
    
    	$output = array();
    
    	foreach ( $input as $value )
    		$output = array_merge( $output, wpcf7_array_flatten( $value ) );
    
    	return $output;
    }
    
    function wpcf7_support_html5() {
    	return (bool) apply_filters( 'wpcf7_support_html5', true );
    }
    
    function wpcf7_format_atts( $atts ) {
    	$html = '';
    
    	$prioritized_atts = array( 'type', 'name', 'value' );
    
    	foreach ( $prioritized_atts as $att ) {
    		if ( isset( $atts[$att] ) ) {
    			$value = trim( $atts[$att] );
    			$html .= sprintf( ' %s="%s"', $att, esc_attr( $value ) );
    			unset( $atts[$att] );
    		}
    	}
    
    	foreach ( $atts as $key => $value ) {
    		$value = trim( $value );
    
    		if ( '' !== $value )
    			$html .= sprintf( ' %s="%s"', $key, esc_attr( $value ) );
    	}
    
    	$html = trim( $html );
    
    	return $html;
    }
    
    function wysija_contactform7_subscribe($cfdata) {
    
    	$formdata = $cfdata->posted_data;
    
    	/*
    'your-name' and 'your-email' are from the default template change them to match the ones you use in your form.
    	*/
    
    	$user_name = $formdata['your-name'];
    
    	$user_email = $formdata['your-email'];
    
    	/*
    
    Insert the list id # you want the user to be subscribed to. The default list id is 1. This can be an array if you have more than one list. Example: $listID = array( '1,2' );
    	*/
    
    	$listID = array( '1' );	
    
    	$userData=array(
    
    		'email'		=>	$user_email,
    
    		'firstname'	=>	$user_name
    
    	);
    
    	$data=array(
    
    		'user'		=>	$userData,
    
    		'user_list'	=>	array( 'list_ids'=> $listID )
    
    	);
    
    	$userHelper=&WYSIJA::get('user','helper');
    
    	$userHelper->addSubscriber($data);
    
    }
    /*
    Nothing will happen unless you give the user a chance to opt in. Create a radio select on your CF7 form with the options 'yes', 'no'. Name it 'opt-in', or change the variable below to match your form.
    
    	*/
    if(isset($_POST['opt-in']) &&
    $_POST['opt-in'] == 'yes')
    {
    add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 1);
    }

    https://www.ads-software.com/extend/plugins/contact-form-7/

Viewing 9 replies - 1 through 9 (of 9 total)
  • I tried the following as it is very interesting and I get the following:
    Fatal error: Cannot redeclare wpcf7_array_flatten() (previously declared in /home/content/40/10295440/html/wp-content/plugins/contact-form-7/includes/functions.php:258) in /home/content/40/10295440/html/wp-content/plugins/contact-form-7/includes/functions.php on line 315

    I’m no dev so not sure how to modify to make it work properly.

    Mike

    Got it working with only the following:

    function wysija_contactform7_subscribe($cfdata) {
    
    	$formdata = $cfdata->posted_data;
    
    	/*
    'your-name' and 'your-email' are from the default template change them to match the ones you use in your form.
    	*/
    
    	$user_name = $formdata['your-name'];
    
    	$user_email = $formdata['your-email'];
    
    	/*
    
    Insert the list id # you want the user to be subscribed to. The default list id is 1. This can be an array if you have more than one list. Example: $listID = array( '1,2' );
    	*/
    
    	$listID = array( '1' );	
    
    	$userData=array(
    
    		'email'		=>	$user_email,
    
    		'firstname'	=>	$user_name
    
    	);
    
    	$data=array(
    
    		'user'		=>	$userData,
    
    		'user_list'	=>	array( 'list_ids'=> $listID )
    
    	);
    
    	$userHelper=&WYSIJA::get('user','helper');
    
    	$userHelper->addSubscriber($data);
    
    }
    /*
    Nothing will happen unless you give the user a chance to opt in. Create a radio select on your CF7 form with the options 'yes', 'no'. Name it 'opt-in', or change the variable below to match your form.
    
    	*/
    if(isset($_POST['opt-in']) &&
    $_POST['opt-in'] == 'yes')
    {
    add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 1);
    }

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    How do I add a users last name?

    Thread Starter AJ

    (@permaculturetreegeek)

    Cool, simpler solution.

    I haven’t tested this, but you should be able to add another string for last name, as long as it matches the one in the wisija database.

    SO, instead of this:

    /*
    'your-name' and 'your-email' are from the default template change them to match the ones you use in your form.
    	*/
    
    	$user_name = $formdata['your-name'];
    
    	$user_email = $formdata['your-email'];
    
    	/*
    
    Insert the list id # you want the user to be subscribed to. The default list id is 1. This can be an array if you have more than one list. Example: $listID = array( '1,2' );
    	*/
    
    	$listID = array( '1' );	
    
    	$userData=array(
    
    		'email'		=>	$user_email,
    
    		'firstname'	=>	$user_name
    
    	);

    Add the last name string:

    /*
    'your-name', 'your-email', 'your-lastname' << change the identifier to match the ones you use in your cf7 form. This section gets the data from your form and stores it an a string.
    	*/
    
    	$user_name = $formdata['your-name'];
    
    	$user_email = $formdata['your-email'];
    
            $user_lastname = $formdata['your-lastname'];
    
    	/*
    Insert the list id # you want the user to be subscribed to. The default list id is 1. This can be an array if you have more than one list. [Example: $listID = array( '1,2' );]
    	*/
    
    	$listID = array( '1' );	
    
    /* The $userData=array forms the $string data from cf7 by connecting it to the appropriate wysija database table.
    	*/
    
    	$userData=array(
    
    		'email'		=>	$user_email,
    
    		'firstname'	=>	$user_name,
    
                     'lastname'	=>	$user_lastname
    
    	);

    Thanks for the update, sorry for the delay in response, I will give the last name fix a try soon, but first I found an issue that is preventing from submitting if the ‘opt-in’ field is no. If I understand the code properly, there is a condition to the if that if the ‘opt-in’ is “yes” the do the mailsend to add the user to the wysija list, but there is nothing that states what to do if ‘opt-in’ is “no”. Not being a dev I don’t know how to fix this. At this point my contact from does not work if the ‘opt-in’ in field is blank or “no”.

    Thanks in advance

    Thread Starter AJ

    (@permaculturetreegeek)

    Hi.
    I realized in the version of code that I posted some of the original functions were duplicated resulting in the error that you mentioned.

    The version you posted is the correct code and all that is needed.

    I don’t have an answer to your other question. It’s working fine on my site as it is.

    An else statement shouldn’t be needed.

    Hi, thanks for the prompt response. Here is what I have at the moment for that new function:

    *********************************************
    		contact 7 subscribe to wysiga email list
    *********************************************
    	*/
    
    function wysija_contactform7_subscribe($cfdata) {
    
    	$formdata = $cfdata->posted_data;
    
    	/*
    'your-name' and 'your-email' are from the default template change them to match the ones you use in your form.
    	*/
    
    	$user_name = $formdata['prenom'];
    
    	$user_email = $formdata['email_add'];
    
    	/*
    
    Insert the list id # you want the user to be subscribed to. The default list id is 1. This can be an array if you have more than one list. Example: $listID = array( '1,2' );
    	*/
    
    	$listID = array( '1' );	
    
    	$userData=array(
    
    		'email'		=>	$user_email,
    
    		'firstname'	=>	$user_name
    
    	);
    
    	$data=array(
    
    		'user'		=>	$userData,
    
    		'user_list'	=>	array( 'list_ids'=> $listID )
    
    	);
    
    	$userHelper=&WYSIJA::get('user','helper');
    
    	$userHelper->addSubscriber($data);
    
    }
    /*
    Nothing will happen unless you give the user a chance to opt in. Create a radio select on your CF7 form with the options 'yes', 'no'. Name it 'opt-in', or change the variable below to match your form.
    
    	*/
    if(isset($_POST['opt-in']) &&
    $_POST['opt-in'] == 'Oui')
    {
    add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 1);
    }
    ?>

    Being a French website I changed the Yes to Oui, this works if there is an email address, the only issue were it does not work is if there is no email address and No or Non (french for No) is selected. Is there a way that we can tell it if No then continue?

    Thanks
    Mike

    Hi,

    I’m also interested in making this work but I have no coding skills…

    What to we need to add on the contact form side for this to work?

    something like that is enough? [checkbox checkbox-98]

    also what happens if we update contact form 7? it is all gonna be gone?

    thanks a lot for you help,

    Thread Starter AJ

    (@permaculturetreegeek)

    Sry Mike, this one slipped by, its been a while, you should just be able to add this at the end:

    else{add_action('wpcf7_mail_sent');}

    ——–
    Artaudiomomo:

    As far as I know the way not to loose modifications to a plugin functions.php file is to save your add-in code, update and then re-add the code. Some plugins are set up to work within child themes, but I don’t know about CF7.

    There are instructions in the code on how to set this up in CF7, basically:

    Create a form and use the default CF7 name and email.
    Create a radio select on your CF7 form and name it ‘opt-in’ with the options ‘yes’, ‘no’.

    That should do it.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Contact form 7 subscribe to wysiga newsletter on submit’ is closed to new replies.