0

I have some meta fields for the users on my website, but I can't access to the $_POST variables after de update. The curious thing is that after the update, if I comment the conditional, the meta is updated properly with the $_POST content, but I am not able to use conditionals using $_POST because I have an error,

Undefined index: gdpr_data_consent_field

I can see all the $_POST data in user-edit.php, but as the form is in the url /wp-admin/user-edit.php?user_id=13&wp_http_referer=%2Fwp%2Fwp-admin%2Fusers.php I am not able to have access to it.

Here is the code:

<?php
function privacity_user_profile_fields() {
    global $user_id;

    if ($_POST['gdpr_data_consent_field'] == 'true' ){
        update_user_meta( $user_id, 'gdpr_user_register_consent', 'true' );
        echo 'IS TRUE';
    } else {
        update_user_meta( $user_id, 'gdpr_user_register_consent', 'false' );
        echo 'IS FALSE';
    }

    $data_consent = get_user_meta($user_id, 'gdpr_user_register_consent', true);
    $data_consent_text = 'He leído y entendido el cómo se tratarán los datos introducidos durante el registro en esta web.';
?>

<h2 id="privacity">Opciones de privacidad</h2>
<p>
    <input type="checkbox" name="gdpr_data_consent_field" id="data-consent-field" value="true" <?php echo ($data_consent == 'true') ? 'checked' : '' ?> />
    <label for="data-consent-field"><?php echo $data_consent_text; ?></label>
</p>
<?php 
}
add_action('edit_user_profile', 'privacity_user_profile_fields');
add_action( 'edit_user_profile_update', 'privacity_user_profile_fields' );
?>

I can't sleep!! What I'm doing wrong? A lot of thanks

0

I've found my mistake. I was hooking twice the same code, one in the 'edit_user_profile' and other in 'edit_user_profile_update'. I have separated the display code from de update code and now it works like have to:

<?php
function privacity_user_profile_fields() {
    global $user_id;
    $data_consent = get_user_meta($user_id, 'gdpr_user_register_consent', true);
    $data_consent_text = 'He leído y entendido el cómo se tratarán los datos 
      introducidos durante el registro en esta web.';
    ?>

    <h2 id="privacity">Opciones de privacidad</h2>
    <p>
        <input type="checkbox" name="gdpr_data_consent_field" id="data-consent-field" value="true" <?php echo ($data_consent == 'true') ? 'checked' : '' ?> />
        <label for="data-consent-field"><?php echo $data_consent_text; ?></label>
    </p>
    <?php 
}
add_action('edit_user_profile', 'privacity_user_profile_fields');

function privacity_user_profile_update() {
    global $user_id;
    if ($_POST['gdpr_data_consent_field'] == 'true' ){
        update_user_meta( $user_id, 'gdpr_user_register_consent', 'true' );
    } else {
        update_user_meta( $user_id, 'gdpr_user_register_consent', 'false' );
    }
}
add_action( 'edit_user_profile_update', 'privacity_user_profile_update' );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.