Redirection formulaire après vérification

Fermé
befastlane Messages postés 1 Date d'inscription mardi 28 janvier 2020 Statut Membre Dernière intervention 28 janvier 2020 - Modifié le 28 janv. 2020 à 23:06
jordane45 Messages postés 38273 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 novembre 2024 - 28 janv. 2020 à 23:03
Bonjour,

J'ai besoin de votre aide car je souhaiterais rajouter une option au bouton d'envoi de mon formulaire une option me permettant de rediriger l’utilisateur après quand il clique sur le bouton. J'aimerais en fait que les informations du formulaire soient valides (adresse e-mail valide, par exemple) et que seulement dans ce cas, l’utilisateur soit redirigé. Comme ça, je suis sûr que le formulaire a bien été soumis avant qu'il passe à l'autre page. Et donc, si les informations entrées ne sont pas valides, le message d'erreur pré-configuré apparaisse.

Voilà le code php principal du formulaire :
<?php
/**


* Vendor Registration Form Template * * @version 2.0.0 */ if ( ! defined( 'ABSPATH' ) ) {    exit; // Exit if accessed directly}?> <h2><?php esc_html_e( 'Vendor Registration Form', 'woocommerce-product-vendors' ); ?></h2> <p><?php esc_html_e( 'Submit the form below to become a vendor on this store.', 'woocommerce-product-vendors' ); ?></p> <form class="wcpv-shortcode-registration-form">     <?php do_action( 'wcpv_registration_form_start' ); ?>     <?php if ( ! is_user_logged_in() ) { ?>        <p class="form-row form-row-first">            <label for="wcpv-firstname"><?php esc_html_e( 'First Name', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>            <input type="text" class="input-text" name="firstname" id="wcpv-firstname" value="<?php if ( ! empty( $_POST['firstname'] ) ) echo esc_attr( trim( $_POST['firstname'] ) ); ?>" tabindex="1" />        </p>         <p class="form-row form-row-last">            <label for="wcpv-lastname"><?php esc_html_e( 'Last Name', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>            <input type="text" class="input-text" name="lastname" id="wcpv-lastname" value="<?php if ( ! empty( $_POST['lastname'] ) ) echo esc_attr( trim( $_POST['lastname'] ) ); ?>" tabindex="2" />        </p>                 <div class="clear"></div>                 <p class="form-row form-row-wide">            <label for="wcpv-username"><?php esc_html_e( 'Login Username', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>            <input type="text" class="input-text" name="username" id="wcpv-username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( trim( $_POST['username'] ) ); ?>" tabindex="3" />        </p>         <p class="form-row form-row-first">            <label for="wcpv-email"><?php esc_html_e( 'Email', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>            <input type="email" class="input-text" name="email" id="wcpv-email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( trim( $_POST['email'] ) ); ?>" tabindex="4" />        </p>         <p class="form-row form-row-last">            <label for="wcpv-confirm-email"><?php esc_html_e( 'Confirm Email', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>            <input type="email" class="input-text" name="confirm_email" id="wcpv-confirm-email" value="<?php if ( ! empty( $_POST['confirm_email'] ) ) echo esc_attr( trim( $_POST['confirm_email'] ) ); ?>" tabindex="5" />        </p>         <?php } ?>         <div class="clear"></div>     <p class="form-row form-row-wide">        <label for="wcpv-vendor-vendor-name"><?php esc_html_e( 'Vendor Name', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>        <input class="input-text" type="text" name="vendor_name" id="wcpv-vendor-name" value="<?php if ( ! empty( $_POST['vendor_name'] ) ) echo esc_attr( trim( $_POST['vendor_name'] ) ); ?>" tabindex="6" />        <em class="wcpv-field-note"><?php esc_html_e( 'Important: This is the name that customers see when purchasing your products.  Please choose carefully.', 'woocommerce-product-vendors' ); ?></em>    </p>     <p class="form-row form-row-wide">        <label for="wcpv-vendor-description"><?php esc_html_e( 'Please describe something about your company and what you sell.', 'woocommerce-product-vendors' ); ?> <span class="required">*</span></label>        <textarea class="input-text" name="vendor_description" id="wcpv-vendor-description" rows="4" tabindex="7"><?php if ( ! empty( $_POST['vendor_description'] ) ) echo trim( $_POST['vendor_description'] ); ?></textarea>    </p>     <?php do_action( 'wcpv_registration_form' ); ?>     <p class="form-row">        <input type="submit" class="button" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce-product-vendors' ); ?>" tabindex="8" />    </p>     <?php do_action( 'wcpv_registration_form_end' ); ?> </form>


Et voilà le code php complémentaire au formulaire (sachant que j'ai déjà essayé de le modifier mais je ne vois pas les modifications apparaître sur le site qui est un site wordpress) :
<?php
if ( ! defined( 'ABSPATH' ) ) {
 exit; // Exit if accessed directly
}

/**


* Vendor Registration. * * Handles the vendor form registration process . * * @category Registration * @package  WooCommerce Product Vendors/Registration * @version  2.0.0 */class WC_Product_Vendors_Registration { /**  * Init  *  * @access public  * @since 2.0.0  * @version 2.0.0  * @return bool  */ public function __construct() {  if ( is_admin() ) {   add_action( 'wp_ajax_wc_product_vendors_registration', array( $this, 'registration_ajax' ) );   add_action( 'wp_ajax_nopriv_wc_product_vendors_registration', array( $this, 'registration_ajax' ) );  } else {   add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );  }  return true; } /**  * Add scripts  *  * @access public  * @since 2.0.0  * @version 2.0.0  * @return bool  */ public function add_scripts() {  wp_enqueue_script( 'wcpv-frontend-scripts' );  $localized_vars = array(   'ajaxurl'               => admin_url( 'admin-ajax.php' ),   'ajaxRegistrationNonce' => wp_create_nonce( '_wc_product_vendors_registration_nonce' ),   'success'               => __( 'Your request has been submitted.  You will be contacted shortly.', 'woocommerce-product-vendors' ),  );  wp_localize_script( 'wcpv-frontend-scripts', 'wcpv_registration_local', $localized_vars );  return true; } /**  * Handles the registration via AJAX  *  * @access public  * @since 2.0.0  * @version 2.0.0  * @return bool  */ public function registration_ajax() {  $this->registration_form_validation( $_POST['form_items'] );  return true; } /**  * Includes the registration form  *  * Also allows for the form template to be overwritten.  *  * @access public  * @since 2.0.0  * @version 2.1.5  */ public function include_form() {  wc_get_template( 'shortcode-registration-form.php', array(), 'woocommerce-product-vendors', WC_PRODUCT_VENDORS_TEMPLATES_PATH ); } /**  * Validates the registration form  *  * @access public  * @since 2.0.0  * @version 2.0.0  * @param array $form_items forms items to validate  * @return bool  */ public function registration_form_validation( $form_items = array() ) {  global $errors;  if ( ! is_array( $form_items ) ) {   parse_str( $_POST['form_items'], $form_items );  }  $form_items = array_map( 'sanitize_text_field', $form_items );  if ( ! isset( $form_items ) ) {   wp_die( __( 'Cheatin’ huh?', 'woocommerce-product-vendors' ) );  }  if ( ! wp_verify_nonce( $_POST['ajaxRegistrationNonce'], '_wc_product_vendors_registration_nonce' ) ) {   wp_die( __( 'Cheatin’ huh?', 'woocommerce-product-vendors' ) );  }  // handle form submission/validation  if ( ! empty( $form_items ) ) {   $errors = array();   if ( ! is_user_logged_in() ) {    if ( empty( $form_items['firstname'] ) ) {     $errors['firstname'] = __( 'First Name is a required field.', 'woocommerce-product-vendors' );    }    if ( empty( $form_items['lastname'] ) ) {     $errors['lastname'] = __( 'Last Name is a required field.', 'woocommerce-product-vendors' );    }    if ( empty( $form_items['username'] ) ) {     $errors['username'] = __( 'Username is a required field.', 'woocommerce-product-vendors' );    }    if ( ! empty( $form_items['username'] ) && username_exists( $form_items['username'] ) ) {     $errors['username'] = __( 'Please choose a different username.', 'woocommerce-product-vendors' );    }    if ( empty( $form_items['email'] ) ) {     $errors['email'] = __( 'Email is a required field.', 'woocommerce-product-vendors' );    }    if ( empty( $form_items['confirm_email'] ) ) {     $errors['confirm_email'] = __( 'Confirm email is a required field.', 'woocommerce-product-vendors' );    }    if ( $form_items['confirm_email'] !== $form_items['email'] ) {     $errors['confirm_email'] = __( 'Emails must match.', 'woocommerce-product-vendors' );    }    if ( ! empty( $form_items['email'] ) && false !== email_exists( $form_items['email'] ) ) {     $errors['email'] = __( 'Email already exists in our system.', 'woocommerce-product-vendors' );    }    if ( ! filter_var( $form_items['email'], FILTER_VALIDATE_EMAIL ) ) {     $errors['email'] = __( 'Email is not valid.', 'woocommerce-product-vendors' );    }   }   if ( empty( $form_items['vendor_name'] ) ) {    $errors['vendor_name'] = __( 'Vendor Name is a required field.', 'woocommerce-product-vendors' );   }   // check that the vendor name is not already taken   // checks against existing terms from "wcpv_product_vendors" taxonomy   if ( ! empty( $form_items['vendor_name'] ) && term_exists( $form_items['vendor_name'], WC_PRODUCT_VENDORS_TAXONOMY ) ) {    $errors['vendor_name'] = __( 'Sorry that vendor name already exists. Please enter a different one.', 'woocommerce-product-vendors' );   }   if ( empty( $form_items['vendor_description'] ) ) {    $errors['vendor_description'] = __( 'Vendor Description is a required field.', 'woocommerce-product-vendors' );   }   do_action( 'wcpv_shortcode_registration_form_validation', $errors, $form_items );   $errors = apply_filters( 'wcpv_shortcode_registration_form_validation_errors', $errors, $form_items );   // no errors, lets process the form   if ( empty( $errors ) ) {    if ( is_user_logged_in() ) {     $this->vendor_registration_form_process( $form_items );    } else {     $this->vendor_user_registration_form_process( $form_items );    }   } else {    wp_send_json( array( 'errors' => $errors ) );   }  } } /**  * Process the registration for a vendor.  *  * @since 2.0.41  * @version 2.0.41  * @param array   $form_items Sanitized form items  * @param WP_User $user       WP User  * @param array   $args  * @return bool  */ protected function register_vendor( $form_items, $user, $args = array() ) {  $term_args = apply_filters( 'wcpv_registration_term_args', $args, $form_items );  // add vendor name to taxonomy  $term = wp_insert_term( $form_items['vendor_name'], WC_PRODUCT_VENDORS_TAXONOMY, $term_args );  // no errors, term added, continue  if ( ! is_wp_error( $term ) && ! empty( $user ) ) {   // add user to term meta   $vendor_data = array();   $vendor_data['admins']               = $user->ID;   $vendor_data['per_product_shipping'] = 'yes';   $vendor_data['commission_type']      = 'percentage';   update_term_meta( $term['term_id'], 'vendor_data', apply_filters( 'wcpv_registration_default_vendor_data', $vendor_data ) );   // change this user's role to pending vendor   wp_update_user( apply_filters( 'wcpv_registration_default_user_data', array(    'ID'   => $user->ID,    'role' => 'wc_product_vendors_pending_vendor',   ) ) );   // Add new pending vendor to list.   WC_Product_Vendors_Utils::set_new_pending_vendor( $user->ID );   $default_args = array(    'user_id'     => $user->ID,    'user_email'  => $user->user_email,    'first_name'  => $user->user_firstname,    'last_name'   => $user->user_lastname,    'user_login'  => __( 'Same as your account login', 'woocommerce-product-vendors' ),    'user_pass'   => __( 'Same as your account password', 'woocommerce-product-vendors' ),    'vendor_name' => $form_items['vendor_name'],    'vendor_desc' => $form_items['vendor_description'],   );   $args = apply_filters( 'wcpv_registration_args', wp_parse_args( $args, $default_args ), $args, $default_args );   do_action( 'wcpv_shortcode_registration_form_process', $args, $form_items );   echo 'success';   exit;  } else {   global $errors;   if ( is_wp_error( $user ) ) {    $errors[] = $user->get_error_message();   }   if ( is_wp_error( $term ) ) {    $errors[] = $term->get_error_message();   }   wp_send_json( array( 'errors' => $errors ) );  }  return true; } /**  * Process the registration form for just vendor.  * As in they already have a user account on the site.  *  * @access public  * @since 2.0.0  * @version 2.0.41  * @param array $form_items sanitized form items  * @return bool  */ public function vendor_registration_form_process( $form_items ) {  return $this->register_vendor( $form_items, wp_get_current_user() ); } /**  * Process the registration form for vendor and user  *  * @access public  * @since 2.0.0  * @version 2.0.41  * @param array $form_items sanitized form items  * @return bool  */ public function vendor_user_registration_form_process( $form_items ) {  $username    = $form_items['username'];  $email       = $form_items['email'];  $firstname   = $form_items['firstname'];  $lastname    = $form_items['lastname'];  $password = wp_generate_password();  $args = apply_filters( 'wcpv_shortcode_register_vendor_args', array(   'user_login'      => $username,   'user_email'      => $email,   'user_pass'       => $password,   'first_name'      => $firstname,   'last_name'       => $lastname,   'display_name'    => $firstname,   'role'            => 'wc_product_vendors_pending_vendor',  ) );  $user_id            = wp_insert_user( $args );  $user               = get_user_by( 'id', $user_id );  $password_reset_key = get_password_reset_key( $user );  $args['password_reset_key'] = $password_reset_key;  return $this->register_vendor( $form_items, $user, $args ); }}


EDIT : Correction des balises de code (ajout du LANGAGE pour avoir la coloration syntaxique )
A voir également:

1 réponse

jordane45 Messages postés 38273 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 novembre 2024 4 696
28 janv. 2020 à 23:03
Bonjour,

Merci de bien vouloir poster ton code correctement en précisant le LANGAGE dans les balises de code.
Explications disponibles ici :
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code

Cela permettra d'avoir la coloration syntaxique, les retours à la ligne et l'indentation.
En l'état, ton code est illisible.

PS : Ta question concernant du wordpress... je l'ai déplacée dans le forum wordpress.

1