set current userid as default value in hidden field

Support MB User Meta set current userid as default value in hidden field

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #43196
    Leo HermantoLeo Hermanto
    Participant

    Is there a way to set current userid in the default value of the hidden field?

    #43209
    PeterPeter
    Moderator

    Hello Leo,

    Yes, it is possible. You need to use the code to get the current user ID then register the meta box and custom field and assign the user ID to the default value of the field. For example:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_hidden' );
    
    function your_prefix_hidden( $meta_boxes ) {
        $current_user_id = get_current_user_id();
        
        $meta_boxes[] = [
            'title'      => __( 'hidden', 'your-text-domain' ),
            'id'         => 'hidden',
            'post_types' => ['post'],
            'fields'     => [
                [
                    'id'      => 'test_hidden_field',
                    'type'    => 'hidden',
                    'std'     => $current_user_id,
                ]
            ],
        ];
    
        return $meta_boxes;
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.