Come ottenere l'ID del post corrente nell'azione hook wpcf7_before_send_mail di Contact Form 7

16 dic 2019, 13:17:29
Visualizzazioni: 21.7K
Voti: 6

Sto cercando di gestire i dati di CF7 prima dell'invio e aggiornare il campo personalizzato del post corrente usando la funzione ACF, ma non riesco a ottenere l'ID del post da cui viene inviato il form. Ho anche provato a ottenere l'ID dalla variabile globale $post e da get_queried_object_id() ma non ha funzionato.

Qualche idea su come posso ottenere l'ID del post da cui è stato inviato il form?

function dv_wpcf7_handle_form_data($wpcf7)
{
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $posted_data = $submission->get_posted_data();
    }

    // Verifica l'ID dello specifico form WPCF7
    if ($wpcf7->id() == 128) {
        $number_order = $posted_data['customer-number'];
        $number_current_value = get_field('trip_available_seats', get_the_ID()); // il passaggio dell'ID alla funzione non funziona
        $number_new_value = $number_current_value - $number_order;

        if ($number_new_value >= 0) {
            update_field('trip_available_seats', $number_new_value, get_the_ID());
        } else {
            $error = true;
            $err_msg = 'Messaggio di errore...';
        }
    }

    if (isset($error) && $error === true) {
        $msgs = $wpcf7->prop('messages');
        $msgs['mail_sent_ok'] = $err_msg;
        $wpcf7->set_properties(array('messages' => $msgs));
        add_filter('wpcf7_skip_mail', 'abort_mail_sending');
    }

    return $wpcf7;
}
add_action('wpcf7_before_send_mail', 'dv_wpcf7_handle_form_data');

function abort_mail_sending($contact_form)
{
    return true;
}
0
Tutte le risposte alla domanda 4
2

Puoi ottenere l'ID del post dalla variabile array $posted_data da cui è stato inviato il modulo

  if ($submission) {
       $posted_data = $submission->get_posted_data();
       print_r($posted_data)
  }

Se esegui un print_r su di esso otterrai qualcosa del genere:

Array
(
    [_wpcf7] => 20
    [_wpcf7_version] => 5.1.6
    [_wpcf7_locale] => en_US
    [_wpcf7_unit_tag] => wpcf7-f20-p22-o1
    [_wpcf7_container_post] => 22  **//Questo è quello che ti serve.**
    [your-name] => Jon Doe
    [your-email] => test@test.com
    [your-subject] => subject
    [your-message] => message
)

Modifica:

A partire dalla versione 5.2 di CF7, il modo corretto per ottenere l'ID del "post" a cui è collegato il modulo di contatto è:

if ($submission) {
    print_r($_POST);
}

Questo restituirà qualcosa del genere:

Array
(
    [_wpcf7] => 119 **//Questo è l'ID del "modulo di contatto". Ma si dovrebbe ottenere utilizzando il metodo WPCF7_ContactForm::get_current(); **
    [_wpcf7_version] => 5.2.1
    [_wpcf7_locale] => en_US
    [_wpcf7_unit_tag] => wpcf7-f119-p120-o1
    [_wpcf7_container_post] => 120 **//Questo è l'ID del "post"**
    [_wpcf7_posted_data_hash] => 
    [your-name] => Jon
    [your-email] => Doe
    [your-subject] => Test
    [your-message] => Test
)
16 dic 2019 14:03:11
Commenti

A causa di una modifica di rottura nella versione 5.2, get_posted_data non restituisce più alcun metadato. Vedi sotto per una risposta alternativa.

Pikamander2 Pikamander2
18 lug 2020 10:36:23

Esatto. Dalla versione 5.2 get_posted_data non restituisce più tutti i campi, ho aggiornato la mia risposta.

Awais Awais
16 ago 2020 04:20:54
1

A causa di un cambio incompatibile con le versioni precedenti nella versione 5.2, non è più possibile recuperare i metadati del modulo utilizzando get_posted_data().

In alternativa, puoi utilizzare il valore id nell'array restituito da get_current():

$contact_form = WPCF7_ContactForm::get_current();
$contact_form_id = $contact_form -> id;
18 lug 2020 10:35:12
Commenti

Questo restituirebbe l'ID del modulo, non l'ID del "post" richiesto dall'OP.

Awais Awais
16 ago 2020 04:19:14
1

Scoperto che puoi ottenere l'ID del post contenitore con $_POST['_wpcf7_container_post'])

16 dic 2019 13:55:12
Commenti

funziona perfettamente da functions.php

Iggy Iggy
11 gen 2020 22:51:33
0

Se preferisci recuperare il valore da wpcf7 invece che dall'array $_POST, puoi usare il seguente codice. Questo è simile a come recuperare i dati inviati. Basta usare get_meta invece di get_posted_data:

$submission = WPCF7_Submission :: get_instance();
$submission->get_meta('container_post_id');
2 ott 2020 13:22:00