Created
October 21, 2020 03:02
-
-
Save mishterk/b6ab8c9cdbe849023a1fe57c50c0da74 to your computer and use it in GitHub Desktop.
How to use ACF Forms to manage non ACF controlled meta fields with Advanced Forms Pro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'af/field/prefill_value', 'prefill_form_field', 10, 4 ); | |
/** | |
* @param mixed $value | |
* @param array $field | |
* @param array $form | |
* @param array $args | |
* | |
* @return string | |
*/ | |
function prefill_form_field( $value, $field, $form, $args ) { | |
// Only run on our desired form. | |
if ( $form['key'] !== 'form_5f8f5999245e0' ) { | |
return $value; | |
} | |
// If the user ID can't be determined, bail early and return the original value. | |
if ( ! isset( $args['user'] ) or ! is_numeric( $args['user'] ) ) { | |
return $value; | |
} | |
if ( $field['name'] === 'status' ) { | |
return get_user_meta( $args['user'], 'membership_status', true ); | |
} | |
return $value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'af/form/editing/user_updated', 'afp_demo_store_additional_user_meta', 10, 3 ); | |
add_action( 'af/form/editing/user_created', 'afp_demo_store_additional_user_meta', 10, 3 ); | |
/** | |
* @param WP_User $user | |
* @param array $form | |
* @param array $args | |
*/ | |
function afp_demo_store_additional_user_meta( $user, $form, $args ) { | |
// Only run on our desired form. | |
if ( $form['key'] !== 'form_5f8f5999245e0' ) { | |
return; | |
} | |
// Get the value from the submission. | |
$status = af_get_field( 'status' ); | |
// If field value could not be retrieved, bail. | |
if ( $status === false ) { | |
return; | |
} | |
// Save the value in a custom user meta key. | |
update_user_meta( $user->ID, 'membership_status', $status ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment