daniele daniele - 1 month ago 7
MySQL Question

wordpress how I can compare custom field entry with values in a database table?

I have this piece of code that give me the possibility to add and validate a custom field during registration process.



//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {

$esp_n_1 = ( ! empty( $_POST['esp_n_1'] ) ) ? trim( $_POST['esp_n_1'] ) : '';

?>
<p>
<label for="esp_n_1"><?php _e( 'Termostato n. 1', 'mydomain' ) ?><br />
<input type="text" name="esp_n_1" id="esp_n_1" class="input" value="<?php echo esc_attr( wp_unslash( $esp_n_1 ) ); ?>" size="25" /></label>
</p>
<?php
}

//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

if ( empty( $_POST['esp_n_1'] ) || ! empty( $_POST['esp_n_1'] ) && trim( $_POST['esp_n_1'] ) == '' ) {
$errors->add( 'esp_n_1_error', __( '<strong>ERROR</strong>: You must include a Serial number.', 'mydomain' ) );
}

return $errors;
}

//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );

function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['esp_n_1'] ) ) {
update_user_meta( $user_id, 'esp_n_1', trim( $_POST['esp_n_1'] ) );
}
}





But I need one more mandatory validation:
When the user submit the form I ned wordpress to check if the custom field data is already present in a column of a table ( I think iterating trough it ), If found the registration process go ahead, if not it raise error message.

I hope someone can help this question.

Best regards

Answer

ok, I found the solution and place for those who had had the same problem.

The complete code is :

add_action( 'register_form', 'myplugin_register_form' ); function myplugin_register_form() {

$esp_n_1 = ( ! empty( $_POST['esp_n_1'] ) ) ? trim( $_POST['esp_n_1'] ) : '';

    ?>
    <p>
        <label for="esp_n_1"><?php _e( 'Termostato n. 1', 'mydomain' ) ?><br />
            <input type="text" name="esp_n_1" id="esp_n_1" class="input" value="<?php echo esc_attr( wp_unslash( $esp_n_1 ) ); ?>" size="25" /></label>
    </p>
    <?php
}

//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if ( empty( $_POST['esp_n_1'] ) || ! empty( $_POST['esp_n_1'] ) && trim( $_POST['esp_n_1'] ) == '' ) {
        $errors->add( 'esp_n_1_error', __( '<strong>ERROR</strong>: You must include a Serial number.', 'mydomain' ) );
    }

    global $wpdb;
    $found = false;
    $serialnumbers = $wpdb->get_col($wpdb->prepare("SELECT serialnumber FROM ".$wpdb->prefix."espserial", 0));
    foreach ($serialnumbers as $serialnumber) {
        if ($serialnumber == $_POST['esp_n_1']) {
            $found = true;
            break;
        } 
    }
    if (! $found) {
        $errors->add( 'esp_n_1_error', __( '<strong>ERROR</strong>: You  Serial .', 'mydomain' ) );
    }
    $found = false;
    return $errors;
}

//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );

function myplugin_user_register( $user_id ) {       
    if ( ! empty( $_POST['esp_n_1'] ) ) {
        update_user_meta( $user_id, 'esp_n_1', trim( $_POST['esp_n_1'] ) );
    }
}`
Comments