Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [Wordpress] Controllare duplicati in contact form 7

    ciao!

    scusate se ho sbagliato sezione ma ero indeciso, visto che cmq è tutto codice php.

    in sostanza devo controllare se una email è già stata inviata attraverso un form.
    ho aggiunto questo codice nel functions.php:
    codice:
    add_action( 'wpcf7_before_send_mail', 'check_email' );
    
    
    function email_already_in_db ( $result, $tags ) {
        // Retrieve the posted form
        $form  = WPCF7_Submission::get_instance();
        $form_posted_data = $form->get_posted_data();
    
    
        // Get the field name that we want to check for duplicates.
        // I added 'unique' to the beginning of the field name in CF7
        // Checking for that with preg_grep
        $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));
    
    
        // $unique_field_name comes back as array so the next three lines give us the key as a string
        reset($unique_field_name);
        $first_key = key($unique_field_name);
        $unique_field_name = $unique_field_name[$first_key];
    
    
        // Check the form submission unique field vs what is already in the database
        $email = $form->get_posted_data($unique_field_name);
        global $wpdb;
        $entry = $wpdb->get_results( "SELECT * FROM zzx_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );
    
    
        // If already in database, invalidate
        if (!empty($entry)) {
          $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
          }
        // return the filtered value
      return $result;
    }
    il mio problema è che ottengo un errore in console, ma nulla sulla pagina:
    codice:
    Response {type: 'basic', url: 'https://sito.it/wp-json/contact-form-7/v1/contact-forms/11/feedback', redirected: false, status: 500, ok: false, …}
    qualche idea??

  2. #2
    ho fatto questa modifica:
    codice:
    function email_already_in_db ( $contact_form ) {
        $form_id = $contact_form->id();
        $submission = WPCF7_Submission::get_instance(); 
        $form_posted_data = $submission->get_posted_data();
        $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));
        reset($unique_field_name);
        $first_key = key($unique_field_name);
        $unique_field_name = $unique_field_name[$first_key];
        global $wpdb;
        $entry = $wpdb->get_results( "SELECT * FROM zzx_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );
        if (!empty($entry)) {
            $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
        }
    }
    
    
    add_action( 'wpcf7_before_send_mail', 'email_already_in_db', 10 , 1 );
    non ottengo più l'errore in console, ma sostanzialmente non funziona.
    nel senso che mi valida sempre il form anche se metto sempre la stessa email.

  3. #3
    risolto:
    codice:
    add_filter('wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2);
    function custom_email_confirmation_validation_filter($result, $tag) {
      if ('your-email' == $tag->name) {
        $your_email = isset($_POST['your-email']) ? trim($_POST['your-email']) : '';
        global $wpdb;
        $entry = $wpdb->get_results("SELECT * FROM xte_cf7_vdata_entry WHERE name LIKE 'your-email' AND value = '$your_email'");
    
    
        if (!empty($entry)) {
          $result->invalidate($tag, "Hai già inviato una richiesta!");
        }
      }
    l'unica cosa che non funziona è il messaggio custom.
    ma per ora va bene così.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.