Set form values to the current user's info

Support MB Frontend Submission Set form values to the current user's info

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #49687
    KGKG
    Participant

    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

    #49688
    PeterPeter
    Moderator

    Hello,

    Thanks for reaching out.

    You can use the filter hook rwmb_{$field_id}_field_meta to set the field value on the editing page or frontend form. Here is an example:

    Field ID: user_name

    add_filter( 'rwmb_user_name_field_meta', function( $value, $field, $saved)  {
        $user = wp_get_current_user();
        $user_name = $user->display_name;
        return $user_name;
    }, 10, 3 );

    Let me know if it helps.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.