Support » Plugin: Advanced Forms » entry_saved hook

  • Resolved codevelop

    (@codevelop)


    It’d be great if we could have a hook within entry_saved, so that we can add some more info to the newly created entry.

    For example, if my form is submitted by a logged-in user, I want to save their user ID. I don’t want to create a form field for this, as I’d need hide it and make it readonly

Viewing 3 replies - 1 through 3 (of 3 total)
  • Correction, I think it would be best to hook into create_entry (not entry_saved)

    
    /**
    	 * Create entry when form is submitted
    	 *
    	 * @since 1.0.0
    	 *
    	 */
    	function create_entry( $form ) {
    
    		// Make sure entries should be created
    		if ( ! $form['create_entries'] ) {
    			return;
    		}
    
    		// Create entry post
    		$post_data = array(
    			'post_type' 		=> 'af_entry',
    			'post_status' 		=> 'publish',
    			'post_title'		=> '',
    		);
    
    		$entry_id = wp_insert_post( $post_data );
    
    		if ( ! $entry_id ) {
    			return;
    		}
    
    		// Update post title
    		$updated_title_data = array(
    			'ID' 			=> $entry_id,
    			'post_title' 	=> sprintf( '#%s', $entry_id ),
    		);
    
    		wp_update_post( $updated_title_data );
    
    		// Save general entry info
    		update_post_meta( $entry_id, 'entry_form', $form['key'] );
    		update_post_meta( $entry_id, 'entry_submission_date', date( 'Y-m-d H:i:s' ) );
    		update_post_meta( $entry_id, 'entry_ip_address', $_SERVER['REMOTE_ADDR'] );
    
    		// Transfer all fields to the entry
    		af_save_all_fields( $entry_id );
    
    		// Save generated entry ID to submission object
    		AF()->submission['entry'] = $entry_id;
    
            apply_filters( 'af/form/entry', $entry_id );
            apply_filters( 'af/form/entry/id=' . $form['post_id'], $entry_id );
            apply_filters( 'af/form/entry/key=' . $form['key'], $entry_id );
    
    	}
    
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Good idea! I’ll add an action to the upcoming release.

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘entry_saved hook’ is closed to new replies.