1

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' );
0

I 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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.