如何让自定义Student角色仅访问自定义插件的Assignments页面?
Let's break down why your current setup isn't working and fix it step by step:
1. Fix the Custom Role Creation Code
First, your existing role code has a small bug: you're using an undefined $role variable instead of the $student variable you created. Also, the admin_init hook isn't the best choice for adding roles—use init instead, as it fires earlier in the WordPress lifecycle to ensure roles are set up before user sessions load.
Here's the corrected role creation code:
add_action( 'init', 'add_custom_roles' ); function add_custom_roles() { // Check if the role already exists to avoid redundant database calls if ( ! get_role( 'student' ) ) { add_role( 'student', 'Student', array( 'read' => false, // Prevent students from accessing regular posts/pages 'assignments' => true // Custom capability for your page ) ); } }
Note: Adding the existing role check stops WordPress from re-registering the role on every page load, which is more efficient.
2. Tie Your Custom Menu Page to the Student Role's Capability
When you create your menu page with add_menu_page, you need to set the capability parameter directly to your custom assignments capability. This tells WordPress only users with that capability can see and access the page.
Here's how to register your menu page correctly:
add_action( 'admin_menu', 'register_assignments_menu_page' ); function register_assignments_menu_page() { add_menu_page( 'Assignments', // Page title shown in the browser tab 'Assignments', // Menu title in the admin sidebar 'assignments', // Required capability to access this page 'assignments', // Unique menu slug 'render_assignments_page', // Callback function to display page content 'dashicons-welcome-write-blog', // Optional: Custom menu icon 6 // Optional: Position in the admin menu ); } // Callback to render your Assignments page content (add your UI here) function render_assignments_page() { // Extra security layer: Double-check the user has permission if ( ! current_user_can( 'assignments' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); } // Your page content goes below echo '<h1>Student Assignments Dashboard</h1>'; echo '<p>Welcome to your assignments portal!</p>'; }
3. Refresh WordPress Role Permissions
WordPress caches role capabilities, so after making these changes, you need to flush the cache to apply the updates:
- Log out of your WordPress admin account and log back in.
- Alternatively, temporarily deactivate and reactivate your plugin (or theme, if you're adding this code to
functions.php).
Why Your Original Setup Failed
- The undefined
$rolevariable meant your customassignmentscapability wasn't being applied correctly (even though you set it inadd_role, the brokenadd_capcall caused inconsistencies). - You didn't explicitly link your menu page to the
assignmentscapability—WordPress defaults to using themanage_optionscapability for menu pages, which your Student role doesn't have.
内容的提问来源于stack exchange,提问作者Shawn

