Results 1 to 2 of 2

Thread: Show and update checkbox custom fields register form

  1. #1
    Thread Starter
    Expired Customer carmuosa's Avatar
    Join Date
    Sep 2014
    Location
    Spain
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post

    Show and update checkbox custom fields register form

    Hello,

    I have used the tutorial for creating custom fields in my Vantage Theme in the registration form. Here you can have a look:

    http://zuwyx.es/register/

    It is working prefectly during registration and also text fields at any time.

    But in admin back-end and and front-end I have following problems with my checkbox fields:

    1) All checkboxes are uncheck independent that in data base looks like (yes or on, I have tried both ways)
    2) I am not able to update values in data base. I check it in the backend or frontend and it doesn't change in the data base.

    I would really appreciate your help.

    Here the complete code, it is a bit log, I apologise.

    <?php
    /**
    * Write your own functions or modify functions, actions, and filters using this file.
    * LIST YOUR FUNCTIONS (optional):
    * cp_import_wp_childstyle() [CUSTOM]
    * cp_get_price() [OVERWRITE]
    */

    //Place All Your Custom Function Below This Line

    // display the custom fields on registration form
    add_action( 'register_form', 'at_custom_fields' );

    // display the custom fields
    function at_custom_fields() {
    ?>
    <fieldset class="at_fieldset">
    <legend><?php _e('Información Adicional Requerida', 'appthemes'); ?></legend>
    <p>
    <label for="at_poblacion"><?php _e('Población', 'appthemes'); ?></label><br/>
    <input class="text" type="text" name="at_poblacion" id="at_poblacion" value=<?php if (isset($_POST['at_poblacion'])) echo esc_attr($_POST['at_poblacion']); ?>>
    <label for="at_provincia"><?php _e('Provincia', 'appthemes'); ?></label><br/>
    <input class="text" type="text" name="at_provincia" id="at_provincia" value=<?php if (isset($_POST['at_provincia'])) echo esc_attr($_POST['at_provincia']); ?>>
    </p>
    <p>
    Selecciona una o varias opciones que encajen con tu perfil: <br/>
    <input type="checkbox" name="at_colegio" id="at_colegio" value="yes" <?php echo checked( ! empty( $_POST['at_colegio'] ) )?> />
    <label for="colegio"><?php _e('Representante de Colegio', 'appthemes'); ?></label> <br/>
    <input type="checkbox" name="at_escuela" id="at_escuela" value="yes" <?php echo checked( ! empty( $_POST['at_escuela'] ) )?> />
    <label for="escuela"><?php _e('Representate de Escuela Infantil, Guardería...', 'appthemes'); ?></label> <br/>
    <input type="checkbox" name="at_ampa" id="at_ampa" value="yes" <?php echo checked( ! empty( $_POST['at_ampa'] ) )?> />
    <label for="ampa"><?php _e('Representate de Ampa', 'appthemes'); ?></label><br/>
    <input type="checkbox" name="at_profesor" id="at_profesor" value="yes" <?php echo checked( ! empty( $_POST['at_profesor'] ) )?> />
    <label for="profesor"><?php _e('Profesor', 'appthemes'); ?></label><br/>
    <input type="checkbox" name="at_padre" id="at_padre" value="on" <?php echo checked( ! empty( $_POST['at_padre'] ) )?> />
    <label for="padre"><?php _e('Padre', 'appthemes'); ?></label> <br/>
    <input type="checkbox" name="at_proveedor" id="at_proveedor" value="yes" <?php echo checked( ! empty( $_POST['at_proveedor'] ) )?> />
    <label for="proveedor"><?php _e('Proveedor de Colegios, Escuelas, Guarderías, Ampa y/o Padres', 'appthemes'); ?></label> <br/>
    <input type="checkbox" name="at_ninguno" id="at_ninguno" value="yes" <?php echo checked( ! empty( $_POST['at_ninguno'] ) )?> />
    <label for="ninguno"><?php _e('Ninguno de los anteriores', 'appthemes'); ?></label> <br/>

    </p>
    </fieldset>

    <?php
    }

    // validate custom fields
    add_action( 'register_post', 'at_validate_custom_fields', 10, 3 );

    // validate fields
    function at_validate_custom_fields( $login, $email, $errors ) {

    // fields to be validated
    $fields = array(
    'at_poblacion' => __( 'Población', 'appthemes' ),
    'at_provincia' => __( 'Provincia', 'appthemes' ),
    );

    // check for empty required fields an display notice
    foreach ( $fields as $field => $value ) {
    if ( empty( $_POST[$field] ) ) {
    $errors->add('empty_fields', __('<strong>ERROR</strong>: &ldquo;', 'appthemes').$value.__('Por favor introduce &rdquo; es un campo requerido.', 'appthemes'));
    }
    }

    // check for invalid names (letters and spaces)
    // if ( ! empty( $_POST['at_poblacion'] ) && preg_match("/[^a-zA-Z ]/", $_POST['at_poblacion']) ) {
    // $errors->add('invalid_poblacion', __('<strong>ERROR</strong>: &ldquo;', 'appthemes').$fields['at_poblacion'].__('&rdquo; does not contain a valid name.', 'appthemes'));
    // }

    // additional custom fields validations here

    }


    // save the custom fields to the database as soon as the user is registered on the database
    add_action( 'user_register', 'at_register_custom_fields' );

    // register the extra fields as user metadata
    function at_register_custom_fields( $user_id, $password = "", $meta = array() ) {

    // custom fields
    $fields = array(
    'at_poblacion',
    'at_provincia',
    'at_colegio',
    'at_escuela',
    'at_ampa',
    'at_profesor',
    'at_padre',
    'at_proveedor',
    'at_ninguno',
    );
    // cleans and updates the custom fields
    foreach ( $fields as $field ) {
    $value = stripslashes( trim( $_POST[$field] ) ) ;
    if ( ! empty( $value ) ) {
    update_user_meta( $user_id, $field, $value );
    }
    }

    }

    // display the custom fields on the user profile page (frontend and admin)
    add_action( 'show_user_profile', 'at_custom_fields_display' ); // frontend
    add_action( 'edit_user_profile', 'at_custom_fields_display' ); // backend

    // display custom fields/values on the backend or frontend
    function at_custom_fields_display( $user ) {
    $user_id = $user->ID;
    ?>
    <?php if ( is_admin() ) { ?>
    <!-- // show the backend HTML -->
    <h3><?php _e('Información Adicional', 'appthemes'); ?></h3>

    <table class="form-table">
    <tr>
    <th><label for="at_poblacion"><?php _e('Población', 'appthemes'); ?></label></th>
    <td>
    <input class="text" type="text" name="at_poblacion" id="at_poblacion" value="<?php echo get_user_meta( $user_id, 'at_poblacion', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_provincia"><?php _e('Provincia', 'appthemes'); ?></label></th>
    <td>
    <input class="text" type="text" name="at_provincia" id="at_provincia" value="<?php echo get_user_meta( $user_id, 'at_provincia', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_colegio"><?php _e('Representante de Colegio', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_colegio" id="at_colegio" value="<?php echo get_user_meta( $user_id, 'at_colegio', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_escuela"><?php _e('Representante de Escuela Infantil, Guardería...', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_escuela" id="at_escuela" value="<?php echo get_user_meta( $user_id, 'at_escuela', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_ampa"><?php _e('Representante de AMPA', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_ampa" id="at_ampa" value="<?php echo get_user_meta( $user_id, 'at_ampa', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_profesor"><?php _e('Profesor', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_profesor" id="at_profesor" value="<?php echo get_user_meta( $user_id, 'at_profesor', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_padre"><?php _e('Padre', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_padre" id="at_padre" value="<?php echo get_user_meta( $user_id, 'at_padre', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_proveedor"><?php _e('Proveedor de Colegios, Escuelas, Guarderías, Ampa y/o Padres', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_proveedor" id="at_proveedor" value="<?php echo get_user_meta( $user_id, 'at_proveedor', true ) ; ?>">
    </td>
    </tr>
    <tr>
    <th><label for="at_ninguno"><?php _e('Ninguno de los anteriores', 'appthemes'); ?></label></th>
    <td>
    <input class="checkbox" type="checkbox" name="at_ninguno" id="at_ninguno" value="<?php echo get_user_meta( $user_id, 'at_ninguno', true ) ; ?>">
    </td>
    </tr>
    </table>

    <?php } else { ?>

    <!-- // show the frontend HTML -->
    <fieldset>
    <div class="form-field">
    <legend class="description"><?php _e('Información Adicional', 'appthemes'); ?></legend>
    </div>
    <div class="form-field">
    <p>
    <label for="at_poblacion"><?php _e('Población', 'appthemes'); ?></label>
    <input class="text" type="text" name="at_poblacion" id="at_poblacion" value="<?php echo get_user_meta( $user_id, 'at_poblacion', true ) ; ?>">
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_provincia"><?php _e('Provincia', 'appthemes'); ?></label>
    <input class="text" type="text" name="at_provincia" id="at_provincia" value="<?php echo get_user_meta( $user_id, 'at_provincia', true ) ; ?>">
    </p>
    </div>
    <div class="form-field">
    <legend class="description"><?php _e('Selecciona una o varias opciones que encajen con tu perfil:', 'appthemes'); ?></legend>
    </div>
    <div class="form-field">
    <p>
    <label for="at_colegio">
    <input class="checkbox" type="checkbox" name="at_colegio" id="at_colegio" value="<?php echo get_user_meta( $user_id, 'at_colegio', true ) ; ?>">
    <?php _e('Representante de Colegio', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_escuela">
    <input class="checkbox" type="checkbox" name="at_escuela" id="at_escuela" value="<?php echo get_user_meta( $user_id, 'at_escuela', true ) ; ?>">
    <?php _e('Representante de Escuela Infantil, Guardería...', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_ampa">
    <input class="checkbox" type="checkbox" name="at_ampa" id="at_ampa" value="<?php echo get_user_meta( $user_id, 'at_ampa', true ) ; ?>">
    <?php _e('Representante de AMPA', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_profesor">
    <input class="checkbox" type="checkbox" name="at_profesor" id="at_profesor" value="<?php echo get_user_meta( $user_id, 'at_profesor', true ) ; ?>">
    <?php _e('Profesor', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_padre">
    <input class="checkbox" type="checkbox" name="at_padre" id="at_padre" value="<?php echo get_user_meta( $user_id, 'at_padre', true ) ; ?>">
    <?php _e('Padre', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_proveedor">
    <input class="checkbox" type="checkbox" name="at_proveedor" id="at_proveedor" value="<?php echo get_user_meta( $user_id, 'at_proveedor', true ) ; ?>">
    <?php _e('Proveedor de Colegios, Escuelas, Guarderías, Ampa y/o Padres', 'appthemes'); ?></label>
    </p>
    </div>
    <div class="form-field">
    <p>
    <label for="at_ninguno">
    <input class="checkbox" type="checkbox" name="at_ninguno" id="at_ninguno" value="<?php echo get_user_meta( $user_id, 'at_ninguno', true ) ; ?>">
    <?php _e('Ninguno de los anteriores', 'appthemes'); ?></label>
    </p>
    </div>
    </fieldset>
    <?php } ?>
    <?php
    }



    // additional fields update
    add_action( 'edit_user_profile_update', 'at_custom_fields_update' );
    add_action( 'personal_options_update', 'at_custom_fields_update' );
    add_action( 'show_user_profile_update', 'at_custom_fields_update' );


    // updates custom fields
    function at_custom_fields_update( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

    // updatable fields
    $fields = array(
    'at_poblacion',
    'at_provincia',
    'at_colegio',
    'at_escuela',
    'at_ampa',
    'at_profesor',
    'at_padre',
    'at_proveedor',
    'at_ninguno',
    );

    foreach ( $fields as $field ) {
    $value = stripslashes( trim( $_POST[$field] ) ) ;
    if ( ! empty( $value ) ) {
    update_user_meta( $user_id, $field, $value );
    }
    }
    }

    //Do not place any code below this line.
    ?>

  2. #2
    Veteran andreuerj's Avatar
    Join Date
    Jan 2012
    Location
    Brazil
    Posts
    1,600
    Thanks
    553
    Thanked 17 Times in 16 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Register Form - Add Fields
    By mgusky in forum Help Using Vantage (Legacy)
    Replies: 1
    Last Post: February 12th, 2015, 09:27 AM
  2. Samcy Custom form with checkbox group or a radio field
    By fabien in forum Report HireBee Bugs
    Replies: 5
    Last Post: July 1st, 2014, 10:18 AM
  3. Replies: 4
    Last Post: April 25th, 2013, 05:14 PM
  4. [SOLVED] Fields doesn't show up - Advanced Custom Fields for ClassiPress
    By dragana in forum Advanced Custom Fields
    Replies: 10
    Last Post: April 8th, 2013, 03:21 PM