Sending Mail After Creating A New Front End Post Using ACF In WordPress!

If you already have a front-end form that submits a new post using acf_form() and now you want to send an email when the form has been submitted, this tutorial will help you.

1. Your front-end form looks like the code below

<?php 

acf_form_head();

get_header();

?>
<div id="content">
	
	<?php
	
	acf_form(array(
		'post_id'	=> 'new_post',
		'post_title'	=> true,
		'post_content'	=> true,
		'new_post'	=> array(
			'post_type'	=> 'post',
			'post_status'	=> 'pending'
		),
		'return'	=> '%post_url%', // Redirect to new post url
		'submit_value'	=> 'Send'
	));
	
	?>
	
</div>

<?php get_footer(); ?>

2. Please copy and paste the below code into your function.php file

// Sending email after Front-end post submission
add_action('acf/save_post', 'yourdomain_save_post', 15);

function yourdomain_save_post($post_id) {
	// Return if not a post
	if( get_post_type($post_id) !== 'post' ) { // here 'post' will be your post type

	    return;	

	}
	
	// Return if editing in admin
	if( is_admin() ) {
	     return;
	}

	// Vars
	$post = get_post( $post_id );

	$toEmail = get_bloginfo('admin_email');
	$subject = $post->post_title;		
	$body    = $post->post_content;
	$from    = '[email protected]';
	
	wp_mail($toEmail, $subject, $body, $from );
}

And that’s it. Thanks!


Posted

in

,

by

Tags:

Comments

One response to “Sending Mail After Creating A New Front End Post Using ACF In WordPress!”

  1. Barsha Subedi Avatar
    Barsha Subedi

    How to select receiver id?

Leave a Reply

Your email address will not be published. Required fields are marked *