Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add opt in checkbox to contact form 7 for wysija plugin WordPress

  • SOLVED

Hi everyone,

I would like to add a checkbox to my contact form 7 like this: [checkbox newsletter_subscribe default:1 "Subscribe to Newsletter?"]

I would like that the first name, last name and email address are sent to wysija (mailpoet) automatycally when user presses submit button.

I guess we can add a function to contact form 7 such as commented on this page http://wordpress.org/support/topic/contact-form-7-subscribe-to-wysiga-newsletter-on-submit?replies=8#post-4970543

But I'd like to sure sure I can update the plugins later without having to do the process all over again.


thanks in advance for your help,

PS: I can send a link to my website via private message is nesessary.

Answers (2)

2013-12-08

Hariprasad Vijayan answers:

Hello,

check this http://wpquestions.com/question/showChrono/id/8262

hope it will help you. please ask if you have any doubt in this.


artaudlemomo comments:

Hi Hariprasad,

thanks for your answer,

but in the link they never managed to make it work, and I still don't know what si going to happen if I add this function to contact form 7 and then later update the plugin.

on top of that I don't what to add (checkbox) to contact form 7 to trigger this function.

any help with that is more than welcome! :)


Hariprasad Vijayan comments:

Try following code in theme's function.php
<?php
function wysija_contactform7_subscribe($cfdata)
{
$formdata = $cfdata->posted_data;
/*
if you use different name/id attribute for CF7
please change 'your-name' and 'your-email'
*/
$user_name = $formdata['your-name'];
$user_email = $formdata['your-email'];
$newsletter_subscribe = $formdata['newsletter_subscribe'];
/*
change this according to your user list id you want this user to subscribe,
*/
$listID = array( 'my_list_ID_1', 'my_list_ID_2' );
$userData=array(
'email' => $user_email,
'firstname' => $user_name
);
$data=array(
'user' => $userData,
'user_list' => array( 'list_ids'=> $listID )
);
if(!empty($newsletter_subscribe)) // Checking checkbox is checked or not
{
$userHelper=&WYSIJA::get('user','helper');
$userHelper->addSubscriber($data);
}
}
add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 1);
?>

Let me know if you have any trouble.


Hariprasad Vijayan comments:

Don't add it in plugin files. Add it in function.php of theme. Then you can update plugins.


artaudlemomo comments:

Ok I've added this code to my function.php in my child theme but I get an error.

I did it this way, i'm must i've done it wrong somewhere: <?php

function wysija_contactform7_subscribe($cfdata)

{

$formdata = $cfdata->posted_data;

/*

if you use different name/id attribute for CF7

please change 'your-name' and 'your-email'

*/

$user_name = $formdata['your-name'];

$user_email = $formdata['your-email'];

$newsletter_subscribe = $formdata['newsletter_subscribe'];

/*

change this according to your user list id you want this user to subscribe,

*/

$listID = array( 'my_list_ID_1', 'my_list_ID_2' );

$userData=array(

'email' => $user_email,

'firstname' => $user_name

);

$data=array(

'user' => $userData,

'user_list' => array( 'list_ids'=> $listID )

);

if(!empty($newsletter_subscribe)) // Checking checkbox is checked or not

{

$userHelper=&WYSIJA::get('user','helper');

$userHelper->addSubscriber($data);

}

}

add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 1);

?>

/*
*
* Dante Functions - Child Theme
* ------------------------------------------------
* These functions will override the parent theme
* functions. We have provided some examples below.
*
*
*/


// OVERWRITE PAGE BUILDER ASSETS
// function spb_regsiter_assets() {
// require_once( get_stylesheet_directory_uri() . '/default.php' );
// }
// if (is_admin()) {
// add_action('admin_init', 'spb_regsiter_assets', 2);
// }
// if (!is_admin()) {
// add_action('wp', 'spb_regsiter_assets', 2);
// }


?>


artaudlemomo comments:

So for those looking for an answer, this code in my child theme function.php did the tricked, thanks to Hariprasad:

function wysija_contactform7_subscribe($cfdata)
{
$formdata = $cfdata->posted_data;
/*
if you use different name/id attribute for CF7
please change 'your-name' and 'your-email'
*/
$user_name = $formdata['your-first-name'];
$last_name = $formdata['your-last-name'];
$user_email = $formdata['your-email'];
$newsletter_subscribe = $formdata['newsletter_subscribe'];
/*
change this according to your user list id you want this user to subscribe,
*/
$listID = array( 'my_list_ID_1', 'my_list_ID_2' );
$userData=array(
'email' => $user_email,
'firstname' => $user_name,
'lastname' => $last_name
);
$data=array(
'user' => $userData,
'user_list' => array( 'list_ids'=> $listID )
);
if(!empty($newsletter_subscribe)) // Checking checkbox is checked or not
{
$userHelper=&WYSIJA::get('user','helper');
$userHelper->addSubscriber($data);
}
}
add_action('wpcf7_mail_sent', 'wysija_contactform7_subscribe', 2);


hope this helps.

2013-12-08

Just Me answers:

You are almost there with @Hariprasad 's code.

<?php and ?> come in pairs. You have two times ?> now without a <?php between them, that is causing an error.

Either remove the <?php and ?> from @Harisprasad's code or include an addition <?php after his ?>