This happens because on every page load you call remove_role
As the docs note:
When a role is removed, the users who have this role lose all rights on the site.
Roles are not like post types, they exist in the database, and removing/adding a role updates the database. For that reason you shouldn’t add them on every page load, it’s better to do it on theme activation or plugin activation
Related Posts:
- Capability to edit own posts and not others
- Ability to only view (not edit) other user’s personal profile information
- WordPress SEO by Yoast: Hide Meta Boxes in Posts for Non-admins
- Modify custom Users Manage page
- How to not allow custom roles to edit published custom post types?
- How to change in customizer the “site identity” tab required capabilities
- User Roles Not Sticking
- Creating user relationships
- Define new user capability for custom post types?
- I want to submit multiple users if checkbox is checked
- How to add an extra button on woocommerce shop page and use it to send the product info with image in a mail? [closed]
- Custom Role with Own Page Edit Capability
- How to stop displaying the Id without losing the functionality
- how to make a custom field readonly or disabled by user role?
- Allow multisite admins only to create users with one specific user role?
- Hide content and functions from specific user roles
- How to determine which capability to use?
- Best practices to handle multilpe roles and capabilities?
- Show comments based on user role in front end
- Disallow “contributors” to edit their own posts once they’ve been set to “Pending Review”
- Users can only save their draft once before saving for revision
- Change the functionality of wordpress users module in admin
- What capability is required to view posts, scheduled in the future, by someone else?
- Security Issues with custom user role or changing core role capabilities?
- How to disable plugin capability : “create new category”
- Is there a flowchart for WordPress loading sequence?
- How do register_sidebar() and get_sidebar() work together?
- Groups of capabilities: users with multiple roles?
- How to customize the default HTML for WordPress Attachments
- Is it possible to change the attributes of a registered style or script before it fires?
- how to change “published date” format on edit.php (Posts page)?
- How to change submit button value in comment from “Post Comment” to “Send”
- Creating a searchable A-Z listing of hundreds of companies/services
- Show attribute description when filtering by that attribute
- Customize in category page
- How to get WordPress to accept the tag in articles (or other alternatives)
- Add custom column to Users admin panel with Types user custom fields?
- Let new user role to ‘edit_others_posts’ of other user role, not of its own type
- How do I change the default admin color scheme in MP6
- allow edit of custom post type but not regular posts?
- Users with custom roles can’t read each other’s comments
- woocommerce Add custom field set like billing fields
- How can a single line in a blog post take down my server?
- Calling a php file from a javascript file in wordpress
- Why is my Custom Post Type not showing up after adding capabilities?
- Adding a Table to the wordpress database
- how can I change the background color of all posts on my server?
- Auto redirect after session expire, but only for one user
- map_meta_cap woes
- Custom title, Bones theme
- Roles for Custom Post Types
- How to create a completely private wordpress with multiple membership levels
- Why do posts disappear from admin after upgrading from WP 2.7.1 to 3.0.1?
- How to change the category url jusy show /category/%category_id% in wordpress?
- custom meta box for page default template
- How can I display a PHP foreach loop’s answers by AJAX
- Add a role and give admin priviledges
- Child Theme with multiple css files
- “An automated WordPress update has failed to complete – please attempt the update again now”– But there is no .maintenance file
- How do I redirect a post url to a custom url?
- How to customize
- Why the css in twenteenfourteen-child theme take no effect?
- Determine if action was executed in functions
- Extending core class in plugin
- Restrict access to non-wordpress section of site with user roles?
- Is it possible to make wordpress LMS
- Saving contact form 7 data into custom Table
- User Capabilities are not available in WP REST permission callback?
- Hiding Short Description in Shop Page, WooCommerce
- theme customizer – can a single option pass multiple values?
- Separate header for static home page and posts page
- Media Upload not working after hiding some elements from functions.php
- Pagination with custom field
- Capabilities don’t add
- Customizing the gallery of the post correctly
- Will WordPress updates remove Gutenberg Additional Classes?
- WP_Query – sort the results by meta value custom attribute wocoomerce
- Allow a user or role to view drafts and previews, but not other admin privileges?
- Multiple header style under meta box
- Remove plugin settings from post creation page for a user role
- How can I create a Custom Page? (not a page template!!)
- Allowing custom role user to edit post assigned to them but don’t let them create new custom type post
- Add a custom intro page
- How to have differents sites share the same tables of DB?
- How to add particular attribute of product in shop as well as home page
- How to change custom link of subpages from of localhost to website
- Disabling user capability to edit_posts or delete_posts in the front-end
- Duplicate Header and Footer
- Custom login doesn’t stay
- Custom dropdown search form that allows users to select option B based from Option 1
- Smarter Document Management links between three WordPress sites sought
- data retrieval presents last set of data
- How to add a menu page for options in wordpress for user that has the role of vendor?
- How to get name and email value from a custom form and add it to campaign monitor subscriber list?
- Create a google friendly header with text and logo
- Custom Link In WordPress Media “Attachment Details” Upload Screen
- “Static” pages within a theme
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Sync roles across several plugins
- How can I make a search term redirect to a page