Site-specific Login plugin

Based on

Adding a custom field to the user („logincustomizing“ Plugin):

Front end registration of a custom field „vtadnr“ (defined via pods)
add_action( 'register_form', 'logincustomizing_registration_form' );
function logincustomizing_registration_form() {

	 if (!empty( $_POST['vtadnr'] )) { $mitgliedsnr = sanitize_text_field($_POST['vtadnr']);} else { $mitgliedsnr =  '';}

	?>
	<p>
		<label for="vtadnr"><?php esc_html_e( 'Mitglieds-Nr.', 'logincustomizing' ) ?><br/>
			<input type="text"		    
			       id="vtadnr"
			       name="vtadnr"
				   maxlength="14"
			       value="<?php echo esc_attr( $mitgliedsnr ); ?>"
			       class="input"
			/>
		</label>
	</p>
	<?php
}
Validate the vtanr field
add_filter( 'registration_errors', 'logincustomizing_registration_errors', 10, 3 );
function logincustomizing_registration_errors( $errors, $sanitized_user_login, $user_email ) {

	if ( empty( $_POST['vtadnr'] ) ) {
		$errors->add( 'vtadnr_error', __( '<strong>Fehler</strong>: Bitte die VTADNR eingeben', 'logincustomizing' ) );
	}
    $pattern = '/*/'; // <----------- customize !!!!
	if ( ! empty( $_POST['vtadnr'] ) && 
	( preg_match($pattern,$_POST['vtadnr']) == 0)) {
		$errors->add( 'vtadnr_error', __( '<strong>Fehler</strong>: Wrong VTADNR', 'logincustomizing' ) );
	}

	return $errors;
}
Save the field
/**
 * Sanitize and save the field, also for backend : edit_user_created_user.
**/
add_action( 'edit_user_created_user', 'logincustomizing_user_register' );
add_action( 'user_register', 'logincustomizing_user_register' );
function logincustomizing_user_register( $user_id ) {
	if ( ! empty( $_POST['vtadnr'] ) ) {
		update_user_meta( $user_id, 'vtadnr', sanitize_text_field($_POST['vtadnr']) );
	}
}
Backend: Display the VTADNR field (users)
add_action( 'user_new_form', 'logincustomizing_admin_registration_form' );
function logincustomizing_admin_registration_form( $operation ) {
	if ( 'add-new-user' !== $operation ) {
		// $operation may also be 'add-existing-user'
		return;
	}

	 if (!empty( $_POST['vtadnr'] )) { $mitgliedsnr = sanitize_text_field($_POST['vtadnr']);} else { $mitgliedsnr =  '';}

	?>
	<h3><?php esc_html_e( 'Personal Information', 'logincustomizing' ); ?></h3>

	<table class="form-table">
		<tr>
			<th><label for="vtadnr"><?php esc_html_e( 'Mitglieds-Nr.', 'logincustomizing' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'logincustomizing' ); ?></span></th>
			<td>
				<input type="text"
			       id="vtadnr"
			       name="vtadnr"
				   maxlength="14"
			       value="<?php echo esc_attr( $mitgliedsnr ); ?>"
			       class="regular-text"
				/>
			</td>
		</tr>
	</table>
	<?php
}
Backend: Validate the vtadnr field
add_action( 'user_profile_update_errors', 'logincustomizing_user_profile_update_errors', 10, 3 );
function logincustomizing_user_profile_update_errors( $errors, $update, $user ) {
	if ( empty( $_POST['vtadnr'] ) ) {
		$errors->add( 'vtadnr_error', __( '<strong>Fehler</strong>: Enter VTADNR. ', 'logincustomizing' ) );
	}
    $pattern = '/*/'; // <----------- customize !!!!
	if ( ! empty( $_POST['vtadnr'] ) && ( (preg_match($pattern,$_POST['vtadnr']) == 0) )) {
		$errors->add( 'vtadnr_error', __( '<strong>Fehler</strong>: Wrong VTADNR', 'logincustomizing' ) );
	}
}
Backend: Edit
add_action( 'personal_options_update', 'logincustomizing_update_profile_fields' );
add_action( 'edit_user_profile_update', 'logincustomizing_update_profile_fields' );

function logincustomizing_update_profile_fields( $user_id ) {
	if ( ! current_user_can( 'edit_user', $user_id ) ) {
		return false;
	} 

	if ( ! empty( $_POST['vtadnr'] ) ) { 
		update_user_meta( $user_id, 'vtadnr', sanitize_text_field($_POST['vtadnr']) );
	}
}
Backend: Displaying
add_action( 'show_user_profile', 'logincustomizing_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'logincustomizing_show_extra_profile_fields' );

function logincustomizing_show_extra_profile_fields( $user ) {
	$mitgliedsnr = get_the_author_meta( 'vtadnr', $user->ID )
	?>
	<h3><?php esc_html_e( 'Personal Information', 'logincustomizing' ); ?></h3>

	<table class="form-table">
		<tr>
			<th><label for="vtadnr"><?php esc_html_e( 'Mitglieds-Nr.', 'logincustomizing' ); ?></label></th>
			<td>
			<input type="text"
			       id="vtadnr"
			       name="vtadnr"
				    maxlength="14"
			       value="<?php echo esc_attr( $mitgliedsnr ); ?>"
			       class="regular-text"
				/>
			</td>
		</tr>
	</table>
	<?php
}
/**
 * add Mitgliedsnummer (vtadnr) column to user table
 **/
function modify_user_table( $column ) {
    $column['Mitgliedsnummer'] = 'Mitgliedsnummer';
return $column;
}
add_filter( 'manage_users_columns', 'modify_user_table' );

function modify_user_table_row( $val, $column_name, $user_id ) { 
  if ($column_name == 'Mitgliedsnummer') {
    return get_user_meta( $user_id, 'vtadnr', true );
  }
return $val;
}
add_filter( 'manage_users_custom_column', 'modify_user_table_row', 10, 3 );

/**
 * make the Mitglieds column sortable
**/
function prefix_sortable_columns( $columns ) {
     $columns['Mitgliedsnummer'] = 'Mitgliedsnummer';
     return $columns;
}
add_filter( 'manage_users_sortable_columns', 'prefix_sortable_columns' );
function prefix_sort_by_vtadnr( $query ) {
if ( 'Mitgliedsnummer' == $query->get( 'orderby' ) ) {
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', 'vtadnr' );
   }
}
add_action( 'pre_get_users', 'prefix_sort_by_vtadnr' );