Skip to content

Instantly share code, notes, and snippets.

@nextend

nextend/test.php Secret

Created Mar 28, 2018
Embed
What would you like to do?
test.php register extra field
<?php
add_action('nsl_before_register', function ($provider) {
/** @var $provider NextendSocialProvider */
/**
* You must return true if you want to add custom fields
*/
add_filter('nsl_registration_require_extra_input', function ($askExtraData) {
return true;
});
add_filter('nsl_registration_validate_extra_input', function ($userData, $errors) {
/** @var $errors WP_Error */
$isPost = isset($_POST['submit']);
if ($isPost) {
/**
* You must add an error if your fields are not filled or does not fulfill your validation.
* If no errors added, that means that the register form is fine.
*/
if (!empty($_POST['favorite_color']) && is_string($_POST['favorite_color'])) {
$userData['favorite_color'] = $_POST['favorite_color'];
} else {
$userData['favorite_color'] = '';
$errors->add('favorite_color_missing', '<strong>' . __('ERROR') . '</strong>: Favorite Color can not be empty.', array('form-field' => 'favorite_color'));
}
} else {
/**
* Fill up user data with default values to prevent the notice in the form
*/
$userData['favorite_color'] = '';
}
return $userData;
}, 10, 2);
/** You can use nsl_registration_form_start and nsl_registration_form_end action. */
add_action('nsl_registration_form_start', function ($userData) {
?>
<p>
<label for="favorite_color">Favorite Color<br/>
<input type="text" name="favorite_color" id="favorite_color" class="input"
value="<?php echo esc_attr(wp_unslash($userData['favorite_color'])); ?>" size="20"/></label>
</p>
<?php
});
/**
* $user_id contains the created user's id
* $userData contains the previously validated input
*/
add_action('nsl_registration_store_extra_input', function ($user_id, $userData) {
add_user_meta($user_id, 'favorite_color', $userData['favorite_color']);
}, 10, 2);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment