Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active October 28, 2023 17:15
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 yuriinalivaiko/80530ed879b7973f12cfbee3483b6ba3 to your computer and use it in GitHub Desktop.
Save yuriinalivaiko/80530ed879b7973f12cfbee3483b6ba3 to your computer and use it in GitHub Desktop.
This code adds hidden fields to the Ultimate Member registration form.
<?php
// Add hidden fields you need to this array.
$GLOBALS['um_registration_extra_data'] = array(
'field_key_1' => 'abc',
'field_key_2' => 'xyz',
);
// This code adds hidden fields to the form.
add_action( 'um_after_register_fields', 'add_a_hidden_field_to_register', 10, 1 );
function add_a_hidden_field_to_register( $args ) {
foreach ( $GLOBALS['um_registration_extra_data'] as $key => $val ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
// This code saves hidden field values on the form submit.
add_action( 'um_registration_set_extra_data', 'save_hidden_field_on_redistration', 10, 3 );
function save_hidden_field_on_redistration( $user_id, $args, $form_data ) {
foreach ( $GLOBALS['um_registration_extra_data'] as $key => $val ) {
if ( isset( $args[ $key ] ) ) {
update_user_meta( $user_id, $key, sanitize_meta( $key, $args[ $key ], 'user' ) );
}
}
}
@yuriinalivaiko
Copy link
Author

This gist is a part of the article Add a hidden field to your register form.

You can add this code to the functions.php file in the active theme directory. Skip the opening <?php tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment