$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How can I restrict the categories users can post in, based on user roles?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I restrict the categories users can post in, based on user roles?

matteradmin9PV0评论

So far I've only found plugins that are either obsolete or only affect which categories are displayed.

EDIT: The result should be that users can only select certain categories when they publish their post.

So far I've only found plugins that are either obsolete or only affect which categories are displayed.

EDIT: The result should be that users can only select certain categories when they publish their post.

Share Improve this question edited Dec 15, 2018 at 19:29 Malory asked Dec 15, 2018 at 18:54 MaloryMalory 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Try using these codes:

function themename_check_user_role( $role, $user_id = null ) {

    if ( is_numeric( $user_id ) )
    $user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
    return false;

    return in_array( $role, (array) $user->roles );
}

// example use for the current user
if ( themename_check_user_role( 'customer' )
    _e( "You've got access!", 'themename' );
else
    _e( "Sorry, you don't have access!", 'themename' );

Here, you are checking whether a particular user has a role or not. If a match is found, true will be returned.

Post a comment

comment list (0)

  1. No comments so far