How to filter value of spesific field before saving it to DB

Support General How to filter value of spesific field before saving it to DB

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #9347
    foundbutlostfoundbutlost
    Participant

    What 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!

    #9348
    Anh TranAnh Tran
    Keymaster

    You can use rwmb_{$field_type}_value or rwmb_{$field_id}_value. Please see this docs.

    #9350
    foundbutlostfoundbutlost
    Participant

    Ah 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

    #9354
    foundbutlostfoundbutlost
    Participant

    I 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 🙁

    #9358
    Anh TranAnh Tran
    Keymaster

    Hi,

    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 called rwmb_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 🙂

    #9365
    foundbutlostfoundbutlost
    Participant

    Yes 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!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘How to filter value of spesific field before saving it to DB’ is closed to new replies.