Hello,
I have created a custom field group that includes two read-only fields (user_name & user_email). Can you tell me how to set the values for these fields so the fields are automatically populated with the current user's info and visible when the user opens the form?
In case it's useful:
I already have shortcodes for a user's details (display_name, email address, etc.) that I created with the Code Snippets plugin.
I haven't had any success just trying to set these fields with plain text:
add_filter('rwmb_frontend_field_value_user_name', 'set_default_value_function', 10, 2);
function set_default_value_function($value, $args) {
if ($args['id'] === 'user-form') {
$value = 'Show User Name'; // Set your desired default value here
}
return $value;
}
To get the above to work, do I need to add something to my shortcode on the front end?
[mb_frontend_form id='user-form']
I've been able to save the user's info on form submission with the following code:
add_action( 'rwmb_user-form_after_save_post', function( $post_id) {
//Get the user's name
$user = wp_get_current_user();
$display_the_name = $user->display_name;
//Update the user_name field with the current user's name
update_post_meta( $post_id, 'user_name', $display_the_name );
});
Appreciate the help!
KG