rwmb_my_custom_field_after_save_field can't update my own fields in the database

Support MB User Profile rwmb_my_custom_field_after_save_field can't update my own fields in the databaseResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14264
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    I'm using metabox type user with the custom table users.

    I want to use this action:

    add_action( 'rwmb_my_custom_field_A_after_save_field', 'usuario_aprobado_con_codigo_acceso' , 20, 5 );
    function usuario_aprobado_con_codigo_acceso ( $null, $field, $new, $old, $post_id ) {

    Depending of the field my_custom_field_A (the value is in $new), I want to update another custom field of the custom table users, my_custom_field_B

    I see in another posts
    https://support.metabox.io/topic/using-custom-attributes-from-rwmb_before_save_post-action/
    something like this:

    $storage = $field['storage'];
    $storage->update( $post_id, 'last_name', "blablabla" );

    And it run well, it update the filed last_name. It run well with the populate user fields.

    But if I want to update another custom fields that I added in this table, I don't know what I have to do to update the value...
    I try:

    $storage->update( $post_id, 'my_custom_field_B', "blablabla" );

    don't work...
    I try a query:

        global $wpdb;
        $wpdb->update('usuarios', array( 'my_custom_field_B' => 'blablabla'), array( 'ID' => $post_id ) ); 

    and don't work...

    Do you know what I'm doing wrong?

    Thanks,
    Sergio

    #14267
    Anh TranAnh Tran
    Keymaster

    Hi Sergio,

    Did you try:

    add_action( 'rwmb_field_A_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
        $storage = $field['storage'];
        $storage->update( $object_id, 'field_B', 'new value' );
    }, 10, 5 );

    This code works only when the field A is put after field B in the meta box, which means field B is already saved.

    If field A is before field B, then after we run this code, field B still use value from the submission. In this case, please try this code:

    add_action( 'rwmb_field_A_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
        $_POST['field_B'] = 'new value'; // Modify directly submitted value from $_POST.
    }, 10, 5 );
    #14283
    proyectohappyweb@gmail.com[email protected]
    Participant

    Yes, I try the first method but don't run.

    But with the second:
    $_POST['field_B'] = 'new value';
    It run well!!!

    Thanks once again.

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