Support » Plugin: Advanced Contact form 7 DB » Merge 2 fields before storing to DB

  • Resolved fstuurman

    (@fstuurman)


    I want to merge 2 fields (address and housenumber) before it is stored in the table.
    I am using following code in my functions.php:
    add_filter( ‘cfdb7_before_save_data’, function ( array $form_data ): array {

    $form_data[‘Adres’] = $form_data[‘Adres’] + ‘ ‘ + $form_data[‘Huisnummer’] ;

    return $form_data;
    } );
    Fields ‘Adres’ and ‘Huisnummer’ do exist in the form and are individually stored in the CFDB table.
    What I am missing, looks like the function is not fired.
    Thanks and kind regards Fred Stuurman

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Vsourz Digital

    (@vsourz1td)

    Hello Fred Stuurman,

    The way that you are using the filter is not the proper way, just to let you know, merging two values into one field is possible but for this you need to know which field is bee retrieved first in the order, then need to take the second value and merge it to the first value and then in next for the second value you need it to make it null, so that the same value is not been populated in the database.

    Below is the example.

    add_filter( ‘cfdb7_before_save_data’, function ( array $form_data ): array {
    $form_data[‘Adres’] = $form_data[‘Adres’].”-“.$form_data[‘Huisnummer’] ;
    $form_data[‘Huisnummer’] = “”;
    return $form_data;
    } );

    Also to let you know the format for merging that you were using is not the perfect way. You need to use the PHP standard for merging the values.

    If you still have any query, then let us know.

    Regards,
    Vsourz

    Thread Starter fstuurman

    (@fstuurman)

    Vsourz,
    Thanks for the reply, I have tried both ways , filling huisnummer with both values and filling Adres with both.
    Also clearing is not done when coded, so:
    add_filter( ‘cfdb7_before_save_data’, function ( array $form_data ): array {
    $form_data[‘Huisnummer’] = ‘ ‘;
    return $form_data;
    } );
    should empty the huisnummer field, but it is not, what I am missing.

    Kind regards
    Fred Stuurman

    Thread Starter fstuurman

    (@fstuurman)

    Searched the plugin code and found out that the name of the filter is wrong,
    should be vsz_cf7_posted_data
    so code will be:

    add_filter( ‘vsz_cf7_posted_data’, function ( array $form_data ): array {
    $form_data[‘Adres’] = $form_data[‘Adres’] . ‘ ‘ . $form_data[‘Huisnummer’] ;
    $form_data[‘Huisnummer’] = ‘ ‘;
    return $form_data;
    } );This works like a charm.

    Kind regards
    Fred Stuurman

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Merge 2 fields before storing to DB’ is closed to new replies.