0

What i'm trying to do - i have meta field in post with array of user emails, i'd like to add new user email(if it's not there already) with each submission of Contact form 7.

Here's what i managed to do by now:

add_action('wpcf7_mail_sent',
    function ($contact_form) {
        if ($contact_form->id() === 32009) {
            $submission = WPCF7_Submission::get_instance();
            //$currentdata = get_post_meta(32127, 'customer_email');
            $mail = $submission->get_posted_data('email-002');
            update_post_meta(32127, 'customer_email',$mail);
        }
    }
);

Thing is that above code is replacing whole array with one new string and i just want to add it, not replace whole thing.

add_post_meta create duplicates and with unique="true" it doesn't add anything.

I would appreciate if anyone could explain to me how can i achieve what i need and maybe a little about updating meta fields, since reading wordpress documentation doesn't really answer my questions.

1
  • post meta values are just strings, even if you try to store an array it gets flattened to a string. update_post_meta replaces the entire value it can't edit/append as it has no concept of "structured data"
    – Tom J Nowell
    Nov 7, 2023 at 16:48

1 Answer 1

1

I think you're close. In my code I've assumed that the desired customer_email meta looks like this:

array(
    0 => '[email protected]',
    1 => '[email protected]',
    2 => '[email protected]',
);

What you need to do is add the new email address to the array of existing email addresses, then save the updated array back to the meta value. update_post_meta() simply overwrites whatever's already in the meta value, so you can't use it to simply merge a new value into an existing array.

Something like this should do it:

add_action('wpcf7_mail_sent',
    function ($contact_form) {
        if ($contact_form->id() === 32009) {
            $submission = WPCF7_Submission::get_instance();
            $currentdata = get_post_meta(32127, 'customer_email', true);
            if ( empty( $currentdata ) ) {
                $currentdata = array();
            }
            // Bail out if $currentdata *isn't* an array.
            if ( ! is_array( $currentdata ) ) {
                return;
            }
            $mail = $submission->get_posted_data('email-002');
            // Add the new email address to $currentdata.
            $currentdata[] = $mail;
            update_post_meta(32127, 'customer_email',$currentdata);
        }
    }
);

Edited: I've changed get_post_meta( 32127, 'customer_email' ); to get_post_meta( 32127, 'customer_email', true ); to ensure that we only get a single post_meta value. Otherwise it will grab all the post_meta values with the key customer_email.

I'm not sure that's the cause of the error you're seeing in the comments, but it should make the code more precise.

4
  • 1
    can you expand a bit on what you did? The solution is in your code but it's buried a bit
    – Tom J Nowell
    Nov 7, 2023 at 16:49
  • with above code when trying to add duplicate i'm getting PHP error: PHP Fatal error: Uncaught TypeError: strtolower(): Argument #1 ($string) must be of type string, array given - is it that something is not sanitized properly?
    – Nikson
    Nov 7, 2023 at 17:46
  • I've updated the code; I'm not sure that's the fix, but it'll improve the precision.
    – Pat J
    Nov 7, 2023 at 18:42
  • 1
    @PatJ thanks! now it works properly. At the end of the script i have added a little check for duplicates: if (in_array($mail, $currentdata)) { return; } else { $currentdata[] = $mail; update_post_meta(32127, 'customer_email',$currentdata); } and now it's perfect, thanks again!
    – Nikson
    Nov 7, 2023 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.