Send email if checkbox is ticked
Support › MB Frontend Submission › Send email if checkbox is tickedResolved
- This topic has 14 replies, 2 voices, and was last updated 3 years, 2 months ago by
[email protected].
-
AuthorPosts
-
January 19, 2022 at 10:29 PM #33337
[email protected]
ParticipantI want to send an email to an admin if the user ticks a checkbox to say that the form is complete.
I'm using the submit button as a save the form button so that the user can continue to fill out the form as they wish - once completed I want the admin to be notified that's it's complete and can be checked by them to verify completion.
I have added a checkbox yet - I want to know if it's possible firsthttps://sfgqualitymark.org.uk/green-care-form/
is this possible?
thanks
Paul
January 20, 2022 at 6:43 AM #33343Long Nguyen
ModeratorHi,
It is possible. You can check the field value via the global variable
$_POST
and use the form action hookrwmb_frontend_after_process
to send the email after the user submits the form.add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { $value = empty( $_POST['field_id'] ) ? null : $_POST['field_id']; if ( 'yes' === $value ) { wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted.' ); wp_safe_redirect( 'thank-you' ); die; } }, 10, 2 );
Refer to the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions
January 20, 2022 at 3:49 PM #33346[email protected]
ParticipantI have tried to set this up - but the email doesn't seem to be sending - what anm I doiung wrong? using a code snippets type of plugin for this as I'm using oxygen builder and no theme is used
<?php add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review']; if ( 'yes' === $value ) { wp_mail( '[email protected]', 'New submission', 'A Green Care Quality Mark Form has been just submitted for review.' ); wp_safe_redirect( 'review' ); die; } }, 10, 2 ); ?>
January 20, 2022 at 11:16 PM #33355Long Nguyen
ModeratorHi,
After submitting the form, does the function
wp_safe_redirect()
work, redirect to the review page? If yes, the functionwp_mail()
is not working due to some problem with SMTP on your hosting. You can contact your hosting support to fix this issue.Refer to this note in the WP documentation
https://developer.wordpress.org/reference/functions/wp_mail/#notesJanuary 21, 2022 at 12:39 AM #33359[email protected]
Participantno it doesn't redirect to the page - goes to the user dashboard. - other emails seem to be sending fine
January 21, 2022 at 12:45 PM #33372Long Nguyen
ModeratorHi,
If you use the
checkbox
field, the value submitted is1
or0
. So you should compare it with this value.$value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review']; if ( $value == 1 ) { ... }
Refer to this documentation https://docs.metabox.io/fields/checkbox/#template-usage
January 21, 2022 at 1:34 PM #33373[email protected]
Participantthank you so much that worked
January 21, 2022 at 1:37 PM #33374[email protected]
Participantcan the email give any info the form submitted - who it was from or a link to it etc - at the moment the admin can't tell who submitted the form
January 22, 2022 at 8:43 AM #33387Long Nguyen
ModeratorHi,
You can create a name or email field, mark it required, and include this value in the email content.
January 22, 2022 at 9:54 PM #33401[email protected]
ParticipantI;m sorry - I'm not sure how to add ther field to the email in the code you sent me - the post title is required - could we add that to the email that is sent?
I added the code below to change it's name on the front end - not sure if that affects it
thank you for your help so far
Paul
add_filter( 'rwmb_frontend_post_title', function( $field ) {
if ( is_page( "Green Care Form" ) ) {
$field['required'] = true;
$field['name'] = 'Name of green care site';
return $field;
}
} );January 23, 2022 at 9:19 AM #33404Long Nguyen
ModeratorHi,
The code will look like
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { $value = empty( $_POST['post_title'] ) ? null : $_POST['post_title']; wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted from '. $value .' user' ); }, 10, 2 );
January 23, 2022 at 11:00 PM #33416[email protected]
Participantthe last code you sent doesn't check for the checkbox field for submission as in the code below
I want it so that the email is sent when the user ticks the checkbox and the post title is included in the email so the admin know which post is ready for review
sorry for all the questions on this but not a coder
<?php
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
$value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review'];
if ( $value == 1 ) {
wp_mail( '[email protected]', 'New submission', 'A Green Care Quality Mark Form has been just submitted for review.' );
wp_safe_redirect( 'submitted' );
die;
}
}, 10, 2 );?>
January 24, 2022 at 7:45 PM #33431Long Nguyen
ModeratorHi,
You can just combine to code to add the post title to email content.
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) { $value = empty( $_POST['submit_form_for_review'] ) ? null : $_POST['submit_form_for_review']; if ( $value == 1 ) { $value = empty( $_POST['post_title'] ) ? null : $_POST['post_title']; wp_mail( '[email protected]', 'New submission', 'A new post has been just submitted from '. $value .' user' ); wp_safe_redirect( 'submitted' ); die; } }, 10, 2 );
January 24, 2022 at 8:01 PM #33432[email protected]
Participantbrilliant - thank you so much for all your help - works really well - one more question - is it possible to add html formatting to the email - like bold or headings etc
regards
Paul
January 24, 2022 at 8:07 PM #33433[email protected]
Participantsorry - I have worked out the html formatting - thanks again - really good/helpful support from you!!
-
AuthorPosts
- You must be logged in to reply to this topic.