admin管理员组文章数量:1415420
I followed this excellent tutorial made by Justin Tadlock .
I can't update profile, it doesn't save to user profile :
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' );
I followed this excellent tutorial made by Justin Tadlock .
I can't update profile, it doesn't save to user profile :
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' );
Share
Improve this question
asked Aug 30, 2019 at 16:14
DrKDrK
234 bronze badges
1 Answer
Reset to default 0I figured it out :
- bad order in function make a mess between variables my_save_user_profession_terms is before my_edit_user_profession_section and should be the oppposite
- I'm calling $term outside the foreach
Now it works !
本文标签: Custom taxonomy forms for user profile
版权声明:本文标题:Custom taxonomy forms for user profile 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745197841a2647226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论