Skip to content

Create form post when a form is registered programmatically #127

@git-temak

Description

@git-temak

When a form is registered programmatically, just the form is created and a form post (very important in WP use) is not created. The form post attached to each form allows the form to be used across WP in various locations, such as ACF relationship fields that use 'af_form'. The form post saves the forms to the db and allows the form to be retrieved in WP queries. This extends the usage of forms created programmatically and is very handy for scalability.

This also means programmatically registered forms don't need to be added to the array of valid forms manually
image

A working solution to extend the af_register_form() function can be found below:

 function af_register_form( $form ) {
	global $af_registered_forms;

	if ( ! $af_registered_forms || ! is_array( $af_registered_forms ) ) {
		$af_registered_forms = array();
	}

	$form = af_get_valid_form( $form );

        if ( $form ) {
                $form_key = $form['key'];
                $existing_form = af_get_form( $form_key );
        
                // if form post exists, update the form
                if ( $existing_form ) {
                    $form_post_id = get_post_meta( $existing_form->ID, 'form_post_id', true );
		    af_form_to_post( $form, $form_post_id );
                    return $form;
                } else {
                    // create new form post when a form is created programmatically
                    $form_title = $form['title'] ?: 'New Programmatic Form - ' . date('d/m/y h:i:s');
                    if (af_is_valid_form_key($form_key)) {
                        $form_post = wp_insert_post([
                            'post_title' => $form_title,
                            'post_type' => 'af_form',
                            'post_status' => 'publish',
                            'post_content' => '',
                        ]);
                    }
        
                    // save the post id to the form array and post object so it can be used to retrieve the form post later to check for if the programmatic form has a form post
                    if ( !is_wp_error( $form_post ) ) {
                        $form['form_post_id'] = $form_post;
                        update_post_meta( $form_post, 'form_key', $form_key );
                        update_post_meta( $form_post, 'form_post_id', $form_post );
                        update_post_meta( $form_post, 'form_create_entries', $form['create_entries'] ); //ensure entries are created if ticked in options
                    }
                }
        
		$af_registered_forms[ $form_key ] = $form;
	}

	return $form;
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions