Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // add a select field to user profile
- add_action( 'show_user_profile', 'my_user_extra_profile_fields' );
- add_action( 'edit_user_profile', 'my_user_extra_profile_fields' );
- function my_user_extra_profile_fields( $user )
- {
- $args = array(
- 'taxonomy' => 'luogolastminute',
- 'show_option_none' => '- Select -',
- 'name' => '_mycategory',
- 'id' => '_mycategory',
- 'selected' => esc_attr( get_the_author_meta('_mycategory', $user->ID) ),
- 'hide_empty' => 0
- );
- ?>
- <h3>Assigned category</h3>
- <table class="form-table">
- <tr>
- <th><label for="_mycategory">Category</label></th>
- <td>
- <?php wp_dropdown_categories( $args ) ?>
- <br />
- <span class="description">Select a category.</span>
- </td>
- </tr>
- </table>
- <?php
- }
- // }}}
- // {{{
- add_action( 'personal_options_update', 'my_save_extra_user_profile_fields' );
- add_action( 'edit_user_profile_update', 'my_save_extra_user_profile_fields' );
- function my_save_extra_user_profile_fields( $user_id )
- {
- if ( !current_user_can( 'edit_user', $user_id ) ) {
- return false;
- }
- if ( current_user_can('manage_options') ) {
- $item = '';
- if ( isset($_POST['_mycategory']) && !empty($_POST['_mycategory']) ) {
- $item = intval($_POST['_mycategory']);
- }
- update_user_meta( $user_id, '_mycategory', $item );
- }
- }
- /*
- * On save post
- *
- */
- add_action('save_post', 'my_save_postdata', 10, 2);
- function my_save_postdata( $post_id )
- {
- // get the post from the post ID, to get the author ID
- $post = get_post( $post_id );
- // get the ID from the user meta
- $term_id = get_the_author_meta('_mycategory', $post->post_author );
- // get the term object
- $term = get_term( $term_id, 'luogolastminute');
- // assign the term to the post
- wp_set_object_terms( $post_id, $term->slug, $term->taxonomy );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement