How to filter value of spesific field before saving it to DB
- This topic has 5 replies, 2 voices, and was last updated 7 years ago by
foundbutlost.
-
AuthorPosts
-
April 24, 2018 at 12:55 PM #9347
foundbutlost
ParticipantWhat is the hook to change text field value before saving it to database?
it seems i have to use this filter but i still dont get it how to do it.
$meta_box = apply_filters( 'rwmb_normalize_field', $field );
Example: i type "this is my value" on the text field, but when i publish/update the post, i want only "my value" saved to the database. i know how to change it, just tell me what hook and where to put the code. Thanks!
April 24, 2018 at 1:37 PM #9348Anh Tran
KeymasterYou can use
rwmb_{$field_type}_value
orrwmb_{$field_id}_value
. Please see this docs.April 24, 2018 at 2:00 PM #9350foundbutlost
ParticipantAh silly me, how could i've missed that :'D, but still i'm not too familiar with the code explanation.
$new = apply_filters( "rwmb_{$field['id']}_value", $new, $field, $old );
Where do i put this code? how come even i have $old when i have nothing as i'm inserting a new data? which variable holding this value “this is my value”? because i need to do str replace on it.
i'm really confused, usually when i want to change output of plugin i just simply use add filter and a function like this:
add_filter( 'the_content', 'filter_my_content' ); function filter_my_content( $content ) { some code here }
please help me
April 24, 2018 at 5:51 PM #9354foundbutlost
ParticipantI tried this after found this thread https://support.metabox.io/topic/woocommerce-prdoucts-image-gallery-with-metabox-image-fields/#post-7904
add_filter( 'rwmb_meta_boxes', 'my_meta2_box' ); function my_meta2_box( $meta_boxes ) { $meta_boxes[] = array ( 'title' => 'Meta song', 'table' => 'wp_xxxx', 'storage_type' => 'custom_table', 'post_types' => array ( 'post', ), 'context' => 'form_top', 'priority' => 'high', 'status' => 'publish', 'autosave' => false, 'fields' => array ( array ( 'id' => 'my_field_id',.... add_filter( 'rwmb_my_field_id_value', function ( $value ) { $value = 'it works'; return $value; } );
but it doesnt work, welp 🙁
April 24, 2018 at 11:07 PM #9358Anh Tran
KeymasterHi,
The filter:
$new = apply_filters( "rwmb_{$field['id']}_value", $new, $field, $old );
means that the value of the field (
$new
) will run through a filter calledrwmb_YOURFIELDID_value
. This filter has 3 params:$new
: the submitted value of the field$field
: field settings (array)$old
: the current value of the field, which is stored in the custom field. If field is new, then it's an empty string.
So, to add a filter to change the field value, please run:
add_filter( 'rwmb_YOURFIELDID_value', function( $new, $field, $old ) { $new = 'Your new value'; return $new; }, 10, 3 );
Just add it to your
functions.php
file of your theme. That's all.Hope that's clear 🙂
April 25, 2018 at 9:43 AM #9365foundbutlost
ParticipantYes now it's become very clear. i actually trying to input youtube url on oEmbed field but i only want save its video ID when it goes to database so i can save more space but still be able to see the video preview below the oEmbed field using thereverse filter
rwmb_fieldID_field_meta
.Thank you so much and case closed!
-
AuthorPosts
- The topic ‘How to filter value of spesific field before saving it to DB’ is closed to new replies.