Skip to content

Instantly share code, notes, and snippets.

@nextend

nextend/test.php Secret

Created March 28, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextend/8b1a4928a8a99540c1155d73a5a59e2a to your computer and use it in GitHub Desktop.
Save nextend/8b1a4928a8a99540c1155d73a5a59e2a to your computer and use it in GitHub Desktop.
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