ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` add_action( 'show_user_profile', 'restrict_user_form' ); add_action( 'edit_user_profile', 'restrict_user_form' ); function restrict_user_form( $user ) { if ( ! current_user_can('add_users')) return false; $args = array( 'show_option_all' => '', 'show_option_none' => '未选择', 'orderby' => 'ID', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 0, 'child_of' => 0, 'exclude' => '', 'echo' => 1, 'selected' => get_user_meta( $user->ID, '_access', true), 'hierarchical' => 0, 'name' => 'allow', 'id' => '', 'class' => 'postform', 'depth' => 0, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false, 'walker' => '' );?> <h3>限制该用户只能投稿到分类</h3> <table class="form-table"> <tr> <th><label for="access">选择分类:</label></th> <td> <?php wp_dropdown_categories($args);?> <br /> <span class="description">用于限制投稿者的分类目录</span> </td> </tr> </table> <?php } add_action( 'personal_options_update', 'restrict_save_data' ); add_action( 'edit_user_profile_update', 'restrict_save_data' ); function restrict_save_data( $user_id ) { if ( ! current_user_can( 'add_users' ) ) return false; update_user_meta( $user_id, '_access', $_POST['allow'] ); } // check if the user loggin in is author and be restricted function is_restrict() { if ( get_user_meta(get_current_user_id(), '_access', true) > 0 ) return true; else return false; } add_action( 'save_post', 'save_restrict_post' ); function save_restrict_post( $post_id ) { if ( ! wp_is_post_revision( $post_id ) && is_restrict() ) { remove_action('save_post', 'save_restrict_post'); wp_set_post_categories( $post_id, get_user_meta( get_current_user_id() , '_access', true) ); add_action('save_post', 'save_restrict_post'); } } add_action( 'edit_form_after_title', 'restrict_warning' ); function restrict_warning( $post_data = false ) { if (is_restrict()) { $c = get_user_meta( get_current_user_id() , '_access', true); $data = get_category($c); echo 'You are allowing to post to category: <strong>'. $data->name .'</strong><br /><br />'; } } function restrict_remove_meta_boxes() { if (is_restrict() ) remove_meta_box('categorydiv', 'post', 'normal'); } add_action( 'admin_menu', 'restrict_remove_meta_boxes' ); ```