I am using Sendy (Sendy.co) (list/email) and have created custom user registration forms with different user roles. There are many plugins with subscribe forms for Sendy, but not with the registration. I would like the user to choose to subscribe when the user registers with a generic checkbox on the form.
I have found something like this, but I'd like to incorporate this in a form. (Forgive the syntax. This is a copy/paste.
add_filter( 'register_form', 'adding_custom_registration_fields' );
function adding_custom_registration_fields( ) {
echo '<div class="form-row form-row-wide"><label for="reg_subscribe"><input type="checkbox" name="reg_subscribe" id="reg_subscribe" value="" />Subscribe to the newsletter</label></div>';
// email of the person being added to the email list
$sendy_email = $_POST['email'];
$url = "<em><u>hidden link</u></em>";
?>
<script>
jQuery( ".register" ).submit(function() {
var user_email = jQuery("#reg_email").val();
if(jQuery('#reg_subscribe').is(':checked')) {
jQuery.post( "<em><u>hidden link</u></em>"+ user_email +"/1/" );
}
</script>
<?php
}
Is something like this possible? How are other Toolset users handling email lists and sending?
Thank you!
Hi, there is no Forms JavaScript API, so there's not an easy way to tap into the "submit" event with JavaScript as shown in this example. You could use the PHP API cred_save_data to hook into the Form's submission lifecycle with PHP, and trigger a PHP post to the Sendy subscribe URL.
Here's an example of submitting a POST request with PHP: https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php
Here's the documentation for our cred_save_data hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Let me know if you have questions about accessing Form data in the $_POST superglobal with PHP.
Thanks, this is a bit beyond my capability. Do you have anyone you would recommend for this?
What apps are Toolset users using for this functionality?
Thanks!
Do you have anyone you would recommend for this?
For smaller projects like this you might be able to get assistance at https://codeable.io/developers/toolset/
We also have a contractors portal available where you can get in touch with other independent professionals: https://toolset.com/contractors
What apps are Toolset users using for this functionality?
Toolset Forms contains built-in notification features, but I'm not aware of any other 3rd-party app integration with Forms and email list signups. We usually recommend using the cred_save_data API to integrate with a 3rd-party's PHP API, since there are no built-in integrations for these signup features.
Thank you! I'm not hearing back from contractors and thought I'd give it a shot. I'm using a check box for the form field.
Can you tell me if I'm on the right track?
add_action('cred_save_data', 'my_subscribe_action',10,2);
function my_subscribe_action($post_id, $form_data)
{
if ($form_data['id']==56)
{
if (isset($_POST['subs-performer']))
{
$name = get_post_meta($post_id, 'first_name', true);
$email = get_post_meta($post_id, 'email', true);
$url = "<em><u>hidden link</u></em>";
}
}
}
return $url;
I did a little more reading and modified.
add_action('cred_save_data', 'my_subscribe_action',10,2);
function my_subscribe_action($post_id, $form_data)
{
if ($form_data['id']==56)
{
if (isset($_POST['subs-performer']))
{
$name = get_post_meta($post_id, 'first_name', true);
$email = get_post_meta($post_id, 'email', true);
$list = 'listID ';
$api_key = ' theapikey';
$post_url = '<em><u>hidden link</u></em>';
$body = array(
'name' => $name,
'email' => $email,
'list' => $list,
'api_key' => $api_key,
'boolean' => 'true'
);
$request = new WP_Http();
$response = $request->post($post_url, array('body' => $body));
}
}
}
After testing, this last code didn't work. Is the selection of the checkbox field correct?
if (isset($_POST['subs-performer']))
Is the selection of the checkbox field correct?
Assuming this is a generic checkbox field with the slug subs-performer, then yes it's correct.
if (isset($_POST['subs-performer']))
One thing to note is that Types fields use the prefix 'wpcf-' in the database, so any time you're using native WordPress APIs like get_post_meta you must use the same prefix for Types custom fields (whether attached to a post, term, or User profile):
$name = get_post_meta($post_id, 'wpcf-first_name', true);
Thanks! Would "wpcf-" also apply to 'email'?
Thanks! Would "wpcf-" also apply to 'email'?
Definitely maybe 🙂 The slug of any custom field created in Types will have a wpcf- prefix in the database. If your code calls a native WP function like get_post_meta to get or set that field value, you should use the prefix. If you're calling a Toolset API like types_render_field, then no prefix is required.
If you have questions about which functions are native WP functions and which are Toolset APIs, then it's probably easiest to use the WP developer reference here to search by function name: https://developer.wordpress.org/reference/