add_filter( 'user_has_cap', $this, 'grant_course_page_edit', 10, 4 ); } public function grant_course_page_edit( array $allcaps, array $caps, array $args, \WP_User $user ): array { // $caps holds the primitive caps the meta cap was mapped to, so the // requested meta cap itself is in $args[0]. if ( ! in_array( $args[0] ?? '', [ 'edit_post', 'edit_page' ], true ) ) { return $allcaps; } $post_id = $args[2] ?? null; if ( ! $post_id || 'page' !== get_post_type( $post_id ) ) { return $allcaps; } // Is this a academiccrew course page? $course_id = get_post_meta( $post_id, '_academiccrew_course_id', true ); if ( ! $course_id ) { return $allcaps; } // Is the current user among the course instructors? $instructor_ids = get_post_meta( $post_id, '_academiccrew_instructor_user_ids', true ); if ( is_array( $instructor_ids ) && in_array( $user->ID, $instructor_ids, true ) ) { // Grant the primitive caps this meta cap maps to (edit_pages, // edit_published_pages, …), for this one page only. foreach ( $caps as $cap ) { $allcaps[ $cap ] = true; } } return $allcaps; } }