Created
March 13, 2020 14:31
-
-
Save ooksanen/2d9f30cd7ec68a889ffa6d6146a4e8ae to your computer and use it in GitHub Desktop.
WP Extra User Fields Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Extra user data | |
* Description: Show extra user fields in admin | |
* Version: 1.0 | |
* Author: Oskari Oksanen | |
* Author URI: http://oskarioksanen.fi/ | |
*/ | |
/* Show when viewing, editing or creating a user profile */ | |
add_action( 'show_user_profile', 'extra_profile_fields' ); | |
add_action( 'edit_user_profile', 'extra_profile_fields' ); | |
add_action( 'user_new_form', 'extra_profile_fields' ); | |
function oo_extra_profile_fields( $user ) { | |
?> | |
<h3>Heading</h3> | |
<table class="form-table"> | |
<tr> | |
<th> | |
<label>Text field</label> | |
</th> | |
<td> | |
<input type="text" name="text_val" value="<?php echo get_user_meta( $user->ID, 'text_val', true ); ?>" placeholder="" /> | |
</td> | |
</tr> | |
<tr> | |
<th> | |
<label>Select</label> | |
</th> | |
<td> | |
<?php | |
$options = array( 'option-1', 'option-2', 'option-3' ); | |
$selected = get_user_meta( $user->ID, 'option', true ); | |
?> | |
<select name="select_val"> | |
<?php | |
foreach( $options as $o ){ | |
if($selected == $o) | |
echo '<option value="'.$o.'" selected>'.$o.'</option>'; | |
else | |
echo '<option value="'.$o.'">'.$o.'</option>'; | |
} | |
?> | |
</select> | |
</td> | |
</tr> | |
<tr> | |
<th> | |
<label>Radio buttons</label> | |
</th> | |
<td> | |
<?php $radio_val = get_user_meta( $user->ID, 'radio_val', true ); ?> | |
<label for="radio_val_yes"><input type="radio" id="radio_val_yes" name="radio_val" value="yes" <?php echo $radio_val == 'yes' ? 'checked' : ''; ?>> Yes</label><br> | |
<label for="radio_val_no"><input type="radio" id="radio_val_no" name="radio_val" value="no" <?php echo $radio_val == 'no' ? 'checked' : ''; ?>> No</label> | |
</td> | |
</tr> | |
<tr> | |
<th> | |
<label>Checkboxes</label> | |
</th> | |
<td> | |
<?php $checkbox_val = get_user_meta( $user->ID, 'checkbox_val', true ); ?> | |
<label for="checkbox_val_yes"><input type="radio" id="checkbox_val_yes" name="checkbox_val" value="yes" <?php echo $checkbox_val == 'yes' ? 'checked' : ''; ?>> Yes</label><br> | |
<label for="checkbox_val_no"><input type="radio" id="checkbox_val_no" name="checkbox_val" value="no" <?php echo $checkbox_val == 'no' ? 'checked' : ''; ?>> No</label> | |
</td> | |
</tr> | |
</table> | |
<? | |
} | |
add_action( 'personal_options_update', 'oo_save_extra_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'oo_save_extra_profile_fields' ); | |
function oo_save_extra_profile_fields( $user_id ) { | |
if ( !current_user_can( 'edit_user', $user_id ) ) | |
return false; | |
update_usermeta( $user_id, 'text_val', $_POST['tour_leader'] ); | |
update_usermeta( $user_id, 'select_val', $_POST['select_val'] ); | |
update_usermeta( $user_id, 'radio_val', $_POST['radio_val'] ); | |
update_usermeta( $user_id, 'checkbox_val', $_POST['checkbox_val'] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment