Default Value - pre propulate - filter ?
- This topic has 6 replies, 2 voices, and was last updated 8 years, 6 months ago by
Anh Tran.
-
AuthorPosts
-
September 19, 2016 at 5:00 PM #4113
Sinusite
ParticipantHi !
I was wondering how i could prefill the custom fields in a new post.
i know i can use the 'std' parameter but i would like to make it conditionnal and dynamic.For example in a new post, be able to get metadata of another custom post type which the POST_ID would be in a hidden field or a parameter in the URL.
Something like http://www.wpbeginner.com/wp-tutorials/how-to-add-default-content-in-your-wordpress-post-editor/ but for custom fields.
May be with https://metabox.io/docs/filters/#section-rwmb_normalize_field ?
What is the best syntax or way to do this ?
Thank You for your help !
September 21, 2016 at 9:46 AM #4119Anh Tran
KeymasterHi,
I think normalize filter or field_meta filter both works. However, using normalize filter seems to be better.
But why don't you just use a condition check when defined meta boxes? I think it's faster. Something like this:
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) { $field = array( 'id' => 'test', 'type' => 'text', 'std' => 'Default value', ); if ( is_admin() ) { $post_id = isset( $_GET['post'] ) ? $_GET['post'] : ( isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : false ); if ( 1 == $post_id ) { $field['std'] = 'Another default value'; } } $meta_boxes[] = array( // ... 'fields' => array( $field, // ... ), ); return $meta_boxes; } );
Or you can filter directly to
rwmb_meta_boxes
to change thestd
value of a field:add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) { if ( is_admin() ) { $post_id = isset( $_GET['post'] ) ? $_GET['post'] : ( isset( $_POST['post_ID'] ) ? $_POST['post_ID'] : false ); if ( 1 == $post_id ) { $meta_boxes['fields'][0]['std'] = 'Another default value'; // First field } } return $meta_boxes; }, 9999 ); // High priority to make sure this function run last
September 25, 2016 at 10:34 PM #4140Sinusite
ParticipantHmmm thank you. My context is a little bit more complex...
I am using add_filter('wp_get_object_terms' but it's not easy to perform query sometimes.
I'll try the second choice you gave me - the problem with this one is you have to care about the fields order.
Thank you!
September 25, 2016 at 11:56 PM #4141Sinusite
ParticipantIt would be great to have $meta_boxes['fields']['field_id']['std']
September 27, 2016 at 10:22 PM #4154Anh Tran
KeymasterThere’s nothing stop you doing that. Simply add keys to
fields
param, like this:’fields’ => array( ‘phone’ => array( ‘id’ => ‘phone’, ’type’ => ’text’, ’name’ => ‘Phone’, ), )
September 30, 2016 at 11:37 PM #4200Sinusite
ParticipantGreat !
But is it working with Taxonomy fields ?
(Please see this issue)October 3, 2016 at 8:28 AM #4217Anh Tran
KeymasterIt should. The taxonomy field needs to work as normal fields. I will check and fix it on Github.
-
AuthorPosts
- You must be logged in to reply to this topic.