How to modify data of custom field which will be stored in custom table

Support › MB Frontend Submission › How to modify data of custom field which will be stored in custom table

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #30601
    pctsaipctsai
    Participant

    Hi, I want to use hook rwmb_frontend_insert_post_data and rwmb_frontend_update_post_data to modify custom field data in submitted post data. For example, an custom fields, called calorie, whose data type is hidden is stored in a custom table. I tried to use the following code to modify the data of calorie but failed.

    add_filter( 'rwmb_frontend_insert_post_data', function( $data, $config ) {
        // Make sure you work on the correct form.
        if ( 'diet_form' !== $config['id'] ) {
            return $data;
        }
    
        $data['calorie'] = '100';
        return $data;
    }, 10, 2 );

    Is there a trick I'm not aware of?

    Thanks,
    pctsai

    #30616
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use the hook rwmb_frontend_after_process to trigger a function update the data in the custom table. Like this:

    add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
        $data = [
            'calorie' => '200',
        ];
        \MetaBox\CustomTable\API::update( $post_id, 'my_custom_table', $data );
    }, 10, 2 );

    Remember to activate the new version of MB Custom Table 2.0.0-rc.2 to use the public API. Please get more details here
    https://docs.metabox.io/extensions/mb-custom-table/#api
    https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions

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