Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use ACF Forms to manage non ACF controlled meta fields with Advanced Forms Pro.
<?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;
}
<?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