Use the rwmb_{$field_type}_value filter with sub-fields

Support MB Group Use the rwmb_{$field_type}_value filter with sub-fields

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38800
    Alaan TVAlaan TV
    Participant

    Hi,

    I'm trying to encrypt ALL the password fields before saving them in DB using something like this:

    function my_encrypt_function ( $new ) {
        $secret_key    = 'XXXXXXXXXXX';
        $secret_vector = 'YYYYYYYYYYY';
        $key           = hash( 'sha256', $secret_key );
        $vector        = substr( hash( 'sha256', $secret_vector ), 0, 16 );
    
        return base64_encode( openssl_encrypt( $new, 'AES-256-CBC', $key, 0, $vector ) );
    };
    add_filter( 'rwmb_password_value', 'my_encrypt_function' );
    

    But this is not working when the password field is a sub-field inside a group field:

    $meta_boxes[] = [
        'title'  => 'My fields',
        'id'     => 'my_fields',
        'fields' => [
            [
                'id'     => 'my_group',
                'type'   => 'group',
                'fields' => [
                    [
                        'name' => 'My Password',
                        'id'   => 'my_password',
                        'type' => 'password',
                    ],
                ],
            ],
        ]
    ];
    

    Is there any other filter or way to do the same?

    #38812
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The field type of the filter hook rwmb_{$field_type}_value or field ID of the filter hook rwmb_{$field_id}_value in this case is the group field, not the subfield. Then you can access the subfield in the group array. For example:

    function my_encrypt_function ( $new ) {
        $new['my_password'] = base64_encode( ... );
        return $new;
    };
    add_filter( 'rwmb_group_value', 'my_encrypt_function' );
    // or add_filter( 'rwmb_my_group_value', 'my_encrypt_function' );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.