What is the best way to process submitted field data before save it?

Support MB Frontend Submission What is the best way to process submitted field data before save it?Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33143
    Marco T NascimentoMarco T Nascimento
    Participant

    What is the best way to process frontend submitted field data before save it? Is there a filter that has access to all submitted fields at the same time? I need to process multiple fields and save the result in one of them.

    #33150
    Long NguyenLong Nguyen
    Moderator

    Hi Marco,

    There is a filter rwmb_frontend_insert_post_data that is used to modify the submitted post data before saving it to the database. Read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#post-data-filters

    #33166
    Marco T NascimentoMarco T Nascimento
    Participant

    Thank you. I've succeeded saving standard post fields with the following code. However, it doesn't work with custom meta fields. Could you please give me some guidance on how to do that? Tks.

    ps: I'm using MB Frontend Submission and MB Custom Table extensions.

    Here is the sample code:

    add_filter( 'rwmb_frontend_update_post_data', 'zy_before_save_post', 10, 2 );
    add_filter( 'rwmb_frontend_insert_post_data', 'zy_before_save_post', 10, 2 );
    function zy_before_save_post( $data, $config ) {
    
    $data['post_title'] = "some title"; // okay
    $data['post_content'] = "some content"; // okay
    $data['my_field_id'] = "some data"; // do not work
    $data['meta_input']['my_field_id'] = "some data"; // do not work
    return $data;
    
    }
    #33179
    Long NguyenLong Nguyen
    Moderator

    Hi Marco,

    It looks like those filters do not work with custom fields, you can try to use the action hook rwmb_frontend_after_save_post to update the field value after saving the post. For example

    add_action( 'rwmb_frontend_after_save_post', 'zy_after_save_post' );
    function zy_after_save_post( $object ) {
    $wysiwyg = $_POST['wysiwyg'];
    
    $wysiwyg = '123456';
    rwmb_set_meta( $object->post_id, 'wysiwyg', $wysiwyg );
    }

    Access the field value via the global variable $_POST. Refer to this topic https://support.metabox.io/topic/use-rwmb_frontend_after_save_post/

    #33197
    Marco NascimentoMarco Nascimento
    Participant

    Thank you

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