Skip to content

Instantly share code, notes, and snippets.

@NickMkrtchyan
Last active May 28, 2022 13:08
Show Gist options
  • Save NickMkrtchyan/93deed6274226acd6e66df9243c49d6e to your computer and use it in GitHub Desktop.
Save NickMkrtchyan/93deed6274226acd6e66df9243c49d6e to your computer and use it in GitHub Desktop.
Adding role selection during registration
/**
* @snippet WooCommerce Role Select During Registration
* @how-to https://woohacks.github.io/
* @author Nick Mkrtchyan
* @compatible WooCommerce 5.0
*/
function register_role_select_option() {
global $wp_roles; ?>
<p class="form-row form-row-wide">
<label for="reg_billing_phone"><?php echo __( 'Student or Instructor', 'woocommerce' ) ?></label>
<select name="role" class="input">
<?php
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude roles like Admin, Author and etc...
if ( ! in_array( $value['name'], [ 'Administrator', 'Author', 'Editor', 'Contributor'] ) ){
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
?>
</select>
</p>
<?php
}
add_action( 'woocommerce_register_form', 'register_role_select_option' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment