Support

Account

Home Forums Front-end Issues Front End Form send email

Solved

Front End Form send email

  • Hey everyone, I’ve got a question about sending an email when using an ACF form on the front end.

    I followed this article on setting up a front end form (which is working fine): http://www.advancedcustomfields.com/resources/create-a-front-end-form/

    The code is very straight forward. I’m wanting to send an email to the site admin after a user submits the form from the front end which creates/saves a custom post type. I have added an action for “acf/save_post” which appears to only fire from the admin. It never fires from the front end at all.

    Does anyone have any examples or suggestions to accomplish what I’m looking for?

    I also don’t mind using a plugin to get this done just can’t find any that work for what I’m doing. Also, I have Gravity forms but my client will also control the ACF fields and gravity forms doesn’t seem that it will work well because of how often my client needs to adjust fields. From what I’ve seen, there’s no way to automatically have gravity forms show fields based on the ACF form.

    Thanks for any help!

    Alan

  • Hi @alanjuden

    I think acf/save_post should run even on front end.. what does your function look like?
    Do you run it with 20 in priority?

  • Hi @jonathan, thanks for replying.

    My action that I’m setting up looks like this:

    add_action('acf/save_post', array($this, 'my_acf_save_post'), 20);

    The function itself looks like:

    function my_acf_save_post($post_id) {
    	if(get_post_type($post_id) == 'my_custom_post_type') {
    		$post = get_post($post_id);
    		$custom = get_post_custom($post_id);
    		
    		$toEmail = get_bloginfo('admin_email');
    		$subject = 'Example Subject';		
    		$email_body = 'Example Body Content';
    		
    		$was_email_sent = wp_mail($toEmail, $subject, $email_body);
    	}
    }

    One thing I feel that I should mention is that my add_action method is being called in my plugin’s constructor.

    Thanks,

    Alan

  • Hmm..
    I don’t think that’s an issue.

    But really you could just use the regular save_post hook from WP core and check $_POST for ‘acf’..

    
    function my_acf_save_post($post_id) {
    	
    	if( !isset($_POST['acf']) )
    		return;
    	
    	if(get_post_type($post_id) == 'my_custom_post_type') {
    		$post = get_post($post_id);
    		$custom = get_post_custom($post_id);
    		
    		$toEmail = get_bloginfo('admin_email');
    		$subject = 'Example Subject';		
    		$email_body = 'Example Body Content';
    		
    		$was_email_sent = wp_mail($toEmail, $subject, $email_body);
    	}
    }
    
    add_action('save_post', array($this, 'my_acf_save_post'), 20);
    
  • Thanks @jonathan, I’ll give that a try in a little bit and see if that solves the issue for me.

    Alan

  • No problem. You might also want to add a custom hidden input that you could check against instead.. that way you’d be sure your email is only sent when it’s a post save from the specific form.

  • @jonathan – thanks for the help. I ran into an issue with the wordpress “save_post” action that it was running prior to the saving of all of the custom post meta fields. I wound up messing around with several other actions before trying the “acf/save_post” again and I pulled the priority 20 and now it runs as I’m expecting it too.

    I also stuck a hidden value into my front end form to look for on the server side so that I know if it’s a post from the front end or not.

    Thanks for all of the help!

    Alan

  • No problem, glad it worked out!

    Feel free to open up another topic if you have further questions and best of luck in your project!

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Front End Form send email’ is closed to new replies.