下面是一个示例
//create the user category fields
add_action( \'show_user_profile\', \'add_user_categories\' );
add_action( \'edit_user_profile\', \'add_user_categories\' );
function add_user_categories($user ){
?>
<table class="form-table">
<tr>
<th><label for="user_categories"><?php _e("User categories"); ?></label></th>
<td>
<?php
$data = get_the_author_meta( \'user_categories\', $user->ID );
$args = array( \'hide_empty\' =>0, \'taxonomy\'=> \'category\');
$categories= get_categories($args);
if ($categories){
foreach ( $categories as $category ){
if(in_array($category->term_id,(array)$data)) {
$selected = \'checked="checked""\';
} else {
$selected = \'\';
}
echo \'<input name="user_categories[]" value="\'.$category->term_id.\'" \'.$selected.\' type="checkbox"/>\'.$category->name.\'<br/>\';
}
}
?>
</td>
</tr>
</table>
<?php
}
//save the user category fields
add_action( \'personal_options_update\', \'save_user_categories\' );
add_action( \'edit_user_profile_update\', \'save_user_categories\' );
function save_user_categories( $user_id ){
if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }
update_usermeta( $user_id, \'user_categories\', $_POST[\'user_categories\'] );
}
并获取存储的数据,只需使用:
$data = get_the_author_meta( \'user_categories\', $user->ID );
以及
$data
将是一个包含用户选择的所有类别的数组。