• Hello, the project I’m doing must be able to create a category with the userlogin or username at registration. The code below works, but only for one category. Can I change it to have what I need; (username = user_categories) ?

    // Autogenerate category—
    function example_insert_category() {
    wp_insert_term(
    ‘Example Category’,
    ‘category’,
    array(
    ‘description’ => ‘This is category created with wp_insert_term.’,
    ‘slug’ => ‘example-category’
    )
    );
    }
    add_action( ‘after_setup_theme’, ‘example_insert_category’ );

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Why does each user need their own category?

    Thread Starter rodesro

    (@rodesro)

    Because they are 50 finance professionals with their own clients and confidential information. We are intranet private blog. Only authors will have the opportunity to create their categories. I think they will have about 50 authors. Can you help me?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    I was asking because author links and feeds already exist with things like /author/name so, why do they need categories specifically? Seems like you have a problem that you’re trying to solve with the wrong answer.

    Thread Starter rodesro

    (@rodesro)

    Yes that’s right, but in this case the contributor uses Postie’s plugin to publish a post by email. With the Postie category option in the topic, this will allow the contributor to publish directly into the author category, if I can find the code to do that. Currently I have the code for the role condition, it misses me the category code by autheur (like the example above). I have this problem because contributors use Postie plugin, it’s the easiest way for them to publish a post.

    Thread Starter rodesro

    (@rodesro)

    Use case is: 1-New author registers on the site. 2-The new suscriber publishes in the category of the author name, into the title (Postie Plugin)

    (Author Category Plugin do manualy the category association in the user profil.)

    The code below works well for category creation in a post.

    add_action(‘save_post’, ‘add_title_as_category’);
    function add_title_as_category( $postid ) {
    if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) return;
    $post = get_post($postid);
    if ( $post->post_type == ‘post’) { // change ‘post’ to any cpt you want to target
    $term = get_term_by(‘slug’, $post->post_name, ‘category’);
    if ( empty($term) ) {
    $add = wp_insert_term( $post->post_title, ‘category’, array(‘slug’=> $post->post_name) );
    if ( is_array($add) && isset($add[‘term_id’]) ) {
    wp_set_object_terms($postid, $add[‘term_id’], ‘category’, true );
    }
    }
    }
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create a category with the userlogin or username at registration’ is closed to new replies.