Populate fields with user name and current date

Support General Populate fields with user name and current date

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #29491
    VoBVoB
    Participant

    Hi!

    We have custom field group with 2 custom fields:
    - user
    - date

    We would like these fields to ne prefilled by default with the current user editing the post and current timestamp. For timestamp we want it to be updated every time the post is saved.

    Example of the generated code:

    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'        => __( 'VoB Edit Status', 'your-text-domain' ),
            'id'           => 'vob-edit-status',
            'post_types'   => ['post', 'vob-post-news', 'page'],
            'context'      => 'side',
            'storage_type' => 'custom_table',
            'table'        => 'wp_custom_vob_edit',
            'fields'       => [
                [
                    'name'          => __( 'Editor', 'your-text-domain' ),
                    'id'            => $prefix . 'vob_editor',
                    'type'          => 'user',
                    'field_type'    => 'select_advanced',
                    'multiple'      => true,
                    'required'      => true,
                    'admin_columns' => [
                        'position'   => 'after date',
                        'title'      => 'Editor',
                        'sort'       => true,
                        'searchable' => true,
                        'filterable' => true,
                    ],
                ],
                [
                    'name'          => __( 'Edit Date', 'your-text-domain' ),
                    'id'            => $prefix . 'vob_edit_date',
                    'type'          => 'datetime',
                    'timestamp'     => true,
                    'required'      => true,
                    'admin_columns' => [
                        'position'   => 'after vob_editor',
                        'title'      => 'Edit Date',
                        'sort'       => true,
                        'searchable' => true,
                        'filterable' => true,
                    ],
                ],
            ],
            'validation'   => [
                'rules' => [
                    $prefix . 'vob_editor' => [
                        'required' => true,
                    ],
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Thank you for the advise!

    #29495
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the setting std to set the default value for a field. For example

    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
        $user_id = get_current_user_id();
    
        $meta_boxes[] = [
            ...
            'fields'       => [
                [
                    'name'          => __( 'Editor', 'your-text-domain' ),
                    'id'            => $prefix . 'vob_editor',
                    'type'          => 'user',
                    'field_type'    => 'select_advanced',
                    'multiple'      => true,
                    'required'      => true,
                    'admin_columns' => [
                        'position'   => 'after date',
                        'title'      => 'Editor',
                        'sort'       => true,
                        'searchable' => true,
                        'filterable' => true,
                    ],
                    'std' => $user_id, //here
                ],
            ],
            ...
        ];
    
        return $meta_boxes;
    }

    Get more details on the documentation https://docs.metabox.io/field-settings/#general

    #29509
    VoBVoB
    Participant

    Thanks for the support, Long! Does it mean that such fields cannot be controlled via Meta Box web UI then? I do not see where to add a call to get_current_user_id() to assign $user_id on the ui.

    Thanks!

    #29513
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The builder helps you to create meta boxes and custom fields with the default settings. If you want to add the dynamic value or advanced cases, you have to use the code.

    FYI, the function get_current_user_id() is a WordPress function, please find it here https://developer.wordpress.org/reference/functions/get_current_user_id/

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