Populate fields with user name and current date
- This topic has 3 replies, 2 voices, and was last updated 3 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
July 14, 2021 at 1:01 PM #29491
VoB
ParticipantHi!
We have custom field group with 2 custom fields:
- user
- dateWe 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!
July 14, 2021 at 4:55 PM #29495Long Nguyen
ModeratorHi,
You can use the setting
std
to set the default value for a field. For examplefunction 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
July 14, 2021 at 10:04 PM #29509VoB
ParticipantThanks 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!
July 14, 2021 at 11:11 PM #29513Long Nguyen
ModeratorHi,
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/ -
AuthorPosts
- You must be logged in to reply to this topic.