Support

Account

Home Forums General Issues acf/save_post on specific form

Solved

acf/save_post on specific form

  • How do I use acf/save_post when a specific form is submitted?

    I have several different forms using acf_form. But I want to be able to identify and do different actions with acf/save_post depending on what form is submitted.

  • Hi @adser

    I believe you can add a hidden field on the front end and check if the submitted data contains that field. Please take a look at this thread to learn more about it: http://support.advancedcustomfields.com/forums/topic/acf-form-validation-only-on-certain-page/.

    I hope this helps.

  • Perfect, thank you for that.

    Would this also be suitable to do if I want to differ from posts that are new or updated?

    Or is there a better solution for identifying if the post already exists or if it is new with acf/save_post? Because I want to send an email to the author when they create the post but not when they update it.

    I send an url query that identifies if the form should update an existing post or create a new one (by sending post=79 for example). I also use some checks to identify if it is the correct author.

    if(isset($_GET['post'])) {
    		$post_id = $_GET['post'];
    	}
    	$correct_author = true;
    	if(empty($post_id)) {
    		$post_id = 'new_post';
    		$submit_value = 'create';
    	} else {
    		$submit_value = 'update';
    		$post_author = get_post_field('post_author', $post_id);
    		$current_user = get_current_user_id();
    		
    		if( $post_author == $current_user ) {
    			$correct_author = true;
    		} else {
    			$correct_author = false;
    		}
    
    	}
    acf_form(array(
        'html_after_fields' => '<input type="hidden" name="acf[' . $submit_value .']" value="true"/>',
    ));
  • Hi @adser

    Your code looks great for me. You can also use “transition_post_status” hook to check if a post is new or updated. You can do it like this:

    function on_post_status_change( $new_status, $old_status, $post ) {
        if ( $new_status != $old_status ) {
            // A function to perform actions any time any post changes status.
            $postedCustomFieldName = $_POST['acf']['field_1234567890'];
    
            //email function here
        }
    }
    add_action(  'transition_post_status',  'on_post_status_change', 10, 3 );

    Where “field_1234567890” is the field key you want to add to the email. To learn more about this hook, please take a look at this page: https://codex.wordpress.org/Post_Status_Transitions.

    I hope this helps.

  • Great, thank you James for your help and support!

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

The topic ‘acf/save_post on specific form’ is closed to new replies.