Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/b6ab8c9cdbe849023a1fe57c50c0da74 to your computer and use it in GitHub Desktop.
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.
<?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