Created
October 26, 2011 19:51
-
-
Save derpixler/1317587 to your computer and use it in GitHub Desktop.
creat new custom extra user profiel fields
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
function my_userfields(){ | |
$output = '<h3>Meine Felder</h3>'; | |
$output .= '<table class="form-table">'; | |
if(isset($_GET['user_id'])){ | |
$user_id = $_GET['user_id']; | |
}else{ | |
$user_id = $_POST['user_id']; | |
} | |
$my_userfields_value = get_user_meta($user_id, 'my_userfields'); | |
$my_userfields = array( | |
array( | |
'label' => 'Ein tolles Feld', | |
'value' => $my_userfields_value[0]['feld_name'], | |
'slug' => 'feld_name', | |
), | |
array( | |
'label' => 'Zahl', | |
'value' => $my_userfields_value[0]['zahl'], | |
'slug' => 'zahl', | |
) | |
); | |
foreach($my_userfields as $my_userfield){ | |
$output .= '<tr>'; | |
$output .= '<th><label for="'.$my_userfield['slug'].'">'.$my_userfield['label'].'</label></th>'; | |
$output .= '<td><input type="text" name="my_userfields['.$my_userfield['slug'].']" id="'.$my_userfield['slug'].'" value="'.$my_userfield['value'].'" class="regular-text code" /></td>'; | |
$output .= '</tr>'; | |
} | |
$output .= '</table>'; | |
echo $output; | |
} | |
function save_my_userfields( $user_id ){ | |
global $wpdb; | |
#print_r($_POST); | |
update_usermeta( $user_id, 'my_userfields', $_POST['my_userfields'] ); | |
} | |
add_action( 'show_user_profile', 'my_userfields' ); | |
add_action( 'edit_user_profile', 'my_userfields' ); | |
add_action( 'edit_user_profile_update', 'save_my_userfields' ); |
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: ADD Custom User Fields | |
* Description: Dieses Plugin fügt dem Benutzerprofil neue Benutzerfelder hinzu. | |
* Version: 1 | |
* Author: René Reimann | |
* Author URI: mailto:rene@die-pixler.de | |
* | |
* @Toscho Thanks for the safety tip! | |
* https://gist.github.com/toscho | |
* | |
*/ | |
add_action( 'show_user_profile', 'RR_custom_userfields' ); | |
add_action( 'edit_user_profile', 'RR_custom_userfields' ); | |
add_action( 'edit_user_profile_update', 'save_RR_custom_userfields' ); | |
function RR_custom_userfields( $user ){ | |
$user_id = $user->ID; | |
$RR_custom_userfields_value = get_user_meta($user_id, 'RR_custom_userfields'); | |
$RR_custom_userfields = array( | |
array( | |
'label' => 'Ein tolles Feld', | |
'value' => $RR_custom_userfields_value[0]['feld_name'], | |
'slug' => 'feld_name', | |
), | |
array( | |
'label' => 'Zahl', | |
'value' => $RR_custom_userfields_value[0]['zahl'], | |
'slug' => 'zahl', | |
) | |
); | |
$output = '<h3>Meine Felder</h3>'; | |
$output .= '<table class="form-table">'; | |
foreach($RR_custom_userfields as $RR_custom_userfield){ | |
$output .= '<tr>'; | |
$output .= '<th><label for="'.$RR_custom_userfield['slug'].'">'.$RR_custom_userfield['label'].'</label></th>'; | |
$output .= '<td><input type="text" name="RR_custom_userfields['.$RR_custom_userfield['slug'].']" id="'.$RR_custom_userfield['slug'].'" value="'.$RR_custom_userfield['value'].'" class="regular-text code" /></td>'; | |
$output .= '</tr>'; | |
} | |
$output .= '</table>'; | |
echo $output; | |
} | |
function save_RR_custom_userfields( $user_id ){ | |
global $wpdb; | |
if ( current_user_can( 'edit_user', $user_id ) ){ | |
foreach($_POST['RR_custom_userfields'] as $RR_custom_field_key => $RR_custom_field_data){ | |
$data[$RR_custom_field_key] = htmlspecialchars ( strip_tags ( $RR_custom_field_data ), ENT_QUOTES, 'utf-8', FALSE ); | |
} | |
update_usermeta( $user_id, 'RR_custom_userfields', $data ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment