我跟着this excellent tutorial 贾斯汀·塔洛克制作。
我无法更新配置文件,它不会保存到用户配置文件:
add_action( \'init\', \'create_profession_tax\' );
$post_type = get_post_type();
function create_profession_tax() {
register_taxonomy(
\'profession\',
\'$post_type\',
array(
\'label\' => __( \'Profession\' ),
\'rewrite\' => array( \'slug\' => \'profession\' ),
\'hierarchical\' => true,
)
);
}
function my_update_profession_count( $terms, $taxonomy ) {
global $wpdb;
foreach ( (array) $terms as $term ) {
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
do_action( \'edit_term_taxonomy\', $term, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( \'count\' ), array( \'term_taxonomy_id\' => $term ) );
do_action( \'edited_term_taxonomy\', $term, $taxonomy );
}
}
/* Adds the taxonomy page in the admin. */
add_action( \'admin_menu\', \'my_add_profession_admin_page\' );
function my_add_profession_admin_page() {
$tax = get_taxonomy( \'profession\' );
add_users_page(
esc_attr( $tax->labels->menu_name ),
esc_attr( $tax->labels->menu_name ),
$tax->cap->manage_terms,
\'edit-tags.php?taxonomy=\' . $tax->name
);
}
/* Create custom columns for the manage profession page. */
add_filter( \'manage_edit-profession_columns\', \'my_manage_profession_user_column\' );
function my_manage_profession_user_column( $columns ) {
unset( $columns[\'posts\'] );
$columns[\'users\'] = __( \'Users\' );
return $columns;
}
/* Customize the output of the custom column on the manage professions page. */
add_action( \'manage_profession_custom_column\', \'my_manage_profession_column\', 10, 3 );
function my_manage_profession_column( $display, $column, $term_id ) {
if ( \'users\' === $column ) {
$term = get_term( $term_id, \'profession\' );
echo $term->count;
}
}
function my_save_user_profession_terms( $user_id ) {
$tax = get_taxonomy( \'profession\' );
/* Make sure the current user can edit the user and assign terms before proceeding. */
if ( !current_user_can( \'edit_user\', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$term = esc_attr( $_POST[\'profession\'] );
/* Sets the terms (we\'re just using a single term) for the user. */
wp_set_object_terms( $user_id, array( $term ), \'profession\', false);
clean_object_term_cache( $user_id, \'profession\' );
update_user_meta( $user_id, \'profession\', $_POST[\'profession\'] );
}
/* Add section to the edit user page in the admin to select profession. */
add_action( \'show_user_profile\', \'my_edit_user_profession_section\' );
add_action( \'edit_user_profile\', \'my_edit_user_profession_section\' );
function my_edit_user_profession_section( $user ) {
$tax = get_taxonomy( \'profession\' );
/* Make sure the user can assign terms of the profession taxonomy before proceeding. */
if ( !current_user_can( $tax->cap->assign_terms ) )
return;
/* Get the terms of the \'profession\' taxonomy. */
$terms = get_terms( \'profession\', array( \'hide_empty\' => false ) ); ?>
<h3><?php _e( \'Profession\' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="profession"><?php _e( \'Select Profession\' ); ?></label></th>
<td><?php
/* If there are any profession terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="radio" name="profession" id="profession-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, \'profession\', $term ) ); ?> /> <label for="profession-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
/* If there are no profession terms, display a message. */
else {
_e( \'There are no professions available.\' );
}
?></td>
</tr>
<tr><?php echo $term->name; ?>
</tr>
</table>
<?php }
/* Update the profession terms when the edit user page is updated. */
add_action( \'personal_options_update\', \'my_save_user_profession_terms\' );
add_action( \'edit_user_profile_update\', \'my_save_user_profession_terms\' );