0

i need to insert wp_users -> user_login field to wp_terms name and slugs field,when a new user registering.

my requirement is admin needs to assign posts to specific users, that is admin need to add a post to user1 ,but user2 should not see that.for this i created a custom post and add taxonomy for that.And the terms are users Usernames .so i need to list the usernames of the users as terms in the taxonomy.when a new user is registered his username should updated in the wp_terms table also,so i will get the usernames as terms.

i stucked here .please suggest some solution for this

1

i got the solution for this

add_action('user_register', function ($user_id) {
    $user_info = get_userdata($user_id);
    $user_name = $user_info->user_login;
    wp_insert_term($user_name, 'user1', array());
}, 10, 1);
| improve this answer | |
0

There is a simple way to to add username as the term of specific taxonomie. You need to use "user_register" hook. So the sample code is bellow

add_action( 'user_register', 'add_term_to_taxonomie', 10, 1 );
function myplugin_registration_save( $user_id ) {
    if ( isset( $_POST['user_login'] ) ){
        wp_insert_term(
          $_POST['user_login'], // the term 
          'product', // the taxonomy
        );
    }

}

Try the code , and replace "product" with your taxonomie name. Then let me know the result. Thanks

| improve this answer | |
  • hi i replaced the product with my taxonomy user1 and copied the code in function.php but it not working the code i replaced is add_action( 'user_register', 'add_term_to_taxonomie', 10, 1 ); function myplugin_registration_save( $user_id ) { if ( isset( $_POST['user_login'] ) ){ wp_insert_term( $_POST['user_login'], // the term 'user1', // the taxonomy ); } } – Adarsh Jul 31 '18 at 4:46

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.