1

I installed a plugin called "Symbiosis" which creates categories automatically copying a user's username upon them signing up.

Whenever any user creates a post on the site, I am trying to figure out how to assign that category that was already created with the new post.

I want to set WP Default Category to the authors username so that when anyone creates a post it automatically posts under their name. (my users do not have access to default wp-panels)

I know about the dropdown in the settings > writing panel but that's not what I'm looking for as I cannot customize the slug to a users username.

Is this possible via plugin or programatically somewhere else?

The categories are all already created. I can pull the loggedin users username and echo it but I'm not sure where to put it:

<?php global $current_user; if ( isset($current_user) ) {echo $current_user->user_login;}?>

2 Answers 2

0

The default category is stored in the options table under option named default_category , so you can do something like this:

add_action('admin_init','set_user_default_cat_wpa89057');
function set_user_default_cat_wpa89057(){
    global $current_user; 
    if ( isset($current_user) ) {
        update_option( 'default_category',  $current_user->user_login);
    }
}
0
function setcategory_2_username($id){
   global $current_user;  
   $category_id = get_cat_ID( $current_user->user_login );
   wp_set_post_categories( $post_id, array($category_id) );
}
add_action('save_post', 'setcategory_2_username');
2
  • 1
    There's a WYSIWYG editor when writing an answer that helps you with formatting. And please explain why this is the solution.
    – kaiser
    Mar 2, 2013 at 15:00
  • This code cannot work as written. Your callback parameter is $id but you are using post_id with wp_set_post_categories()
    – s_ha_dum
    May 6, 2014 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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