Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active July 12, 2020 17:38
Show Gist options
  • Save RadGH/47672fb832e77c85f551306a67966b82 to your computer and use it in GitHub Desktop.
Save RadGH/47672fb832e77c85f551306a67966b82 to your computer and use it in GitHub Desktop.
Capture a submitted ACF form with fields, and do not create a post out of the data
<?php
// Adds a new hook that catches a submitted acf_form by field group key. Only supports a single field group, but could be expanded.
// Just use the new action:
// capture-acf-form/field_group=%%%%%%%%%%%
// Replace %%%%%%% with your field group key.
/**
* Capture user registration form for processing.
*
* @param $fields
* @param $field_group
* @param $acf_form
*
* @internal param $post_id
*/
function custom_user_registration_submit( $fields, $field_group, $acf_form ) {
var_dump($fields); // Array of fields. Keys are the field name. Values are the submitted data.
var_dump($field_group); // Field group definition array. Includes ['fields'] array.
var_dump($acf_form); // The acf_form() args, including things like the "return" url
exit; // remove this after you test the above and see what the output looks like.
// $fields might look like:
/*
array(6) {
["first_name"]=>
string(6) "Radley"
["last_name"]=>
string(8) "Sustaire"
["email"]=>
string(18) "[email protected]"
["additional-information"]=>
string(5) "hello world this is some text"
["test_repeater"]=>
array(2) {
[0]=>
array(1) {
["mytextfield"]=>
string(6) "lostthegame"
}
[1]=>
array(1) {
["mytextfield"]=>
string(6) "asdfsf"
}
}
}
*/
// ----------------------
// ----------------------
// DO STUFF HERE!
// ----------------------
// ----------------------
}
add_action( 'capture-acf-form/field_group=group_588ba2d0c9450', 'custom_user_registration_submit', 25, 3 );
/**
* Save ACF form data on the page with the registration shortcode.
* Note: This adds the acf_form_head call to any page. You only need it on pages that submit a form, though.
*/
function capture_acf_form_include_header() {
acf_form_head();
}
add_action( 'get_header', 'capture_acf_form_include_header' );
/**
* Hook to intercept saving acf_form's with a single field group. An action will be triggered.
* If a hook is attached to the action, then the original post will be removed and pass all data to the hook instead.
*
* @param $post_id
*/
function _capture_acf_form_submission( $post_id ) {
if ( !is_numeric($post_id) ) return; // This should only work for items created as a post.
if ( empty($GLOBALS['acf_form']['field_groups']) ) return;
if ( count($GLOBALS['acf_form']['field_groups']) != 1 ) return;
$acf_form = $GLOBALS['acf_form'];
$field_group_key = $acf_form['field_groups'][0];
$action_name = 'capture-acf-form/field_group=' . $field_group_key;
if ( !has_action( $action_name ) ) return;
$field_group = _acf_get_field_group_by_key( $field_group_key );
if ( !$field_group ) return;
$field_group['fields'] = acf_get_fields( $field_group );
if ( !$field_group['fields'] ) return;
// Collect all fields into an array
$fields = array();
foreach( $field_group['fields'] as $k => $field ) {
$fields[ $field['name'] ] = get_field( $field['name'], $post_id );
}
// Trigger our action
do_action( $action_name, $fields, $field_group, $acf_form );
// Should we delete the post that ACF created?
if ( apply_filters( 'delete-acf-form-post', true ) ) {
wp_delete_post( $post_id, true );
unset($GLOBALS['acf_form']);
remove_all_actions( 'acf/save_post' );
}
// Should we redirect the user?
$return_url = empty($acf_form['return']) ? false : $acf_form['return'];
$return_url = apply_filters( 'acf-form-return-url', $return_url );
if ( $return_url ) {
wp_redirect( $return_url );
exit;
}
}
add_action( 'acf/save_post', '_capture_acf_form_submission', 25 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment