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”
- Style Radio Buttons inside Edit Page (Custom Fields)
- Need to over ride a !important from the index file
- How to display all post from the same current usermeta
- how to hard link to the posts page?
- Creating WordPress staging environment software
- How to add H1 Tag on Homepage?
- Overwrite user role
- Pull A Single Post Prior To Running wp_query
- Why the capability ‘activate_plugins’ is needed to allow a role to see all posts/pages/comments?
- Trouble getting started in wordpress – my experience isn’t in wordpress.
- Responsive menu not expanding in footer
- issue with saving custom metaboxes fields
- Accountless password generation
- Disable `create_post` for built-in post type
- Getting a jQuery library to work in WordPress & Avada
- Custom user roles
- Creating a Treeview Listing of a Directory
- Changing ‘Add to cart’ button text and relink for some specific products [closed]
- adding a custom time class to in theme twenty sixteen
- Adding separate CSS files for separate pages with functions.php
- after checked the checkbox in backend, dont show social link on frontend
- Best way to sort posts with different types by distance
- WooCommerce – Adding custom HTML to specific product pages
- Best way to move a site to a subfolder for testing purposes?
- Page Permalinks: Custom URLs
- How do I move my sites to a child theme without loosing customisation?
- Wonderflux Framework Static Homepage
- How to send email with wp_mail() with from email taken from contact form instead of the host?
- Add commentmeta custom field to every published comment with SQL
- Query 1 custom post type, each day, alphabetically
- Highlight specific user with custom field value
- Displaying all Custom fields on post except some
- Change the default WordPress image on the dashboard login to a custom image [duplicate]
- Can I check who updated the static front page on WordPress and the time it was done
- Removing comma from ‘entry-date’ in Twenty-Sixteen theme
- Admin menu disable new/delete page
- WordPress Install – “end of script output before headers” when running simple php script
- Show different pages on site depending on user choice
- Is there a way to limit multi upload in media upload box?
- Ordering a specific post type
- Having a forum in a subfolder
- call get_header() function from different theme
- What is more secure checking capabilities of user or checking role of user in WordPress plugin development
- Use another theme template in my theme
- How to echo custom styles in css format?
- How to add a menu page for options in wordpress for user that has the role of vendor?
- Error in establishing a database connection in wordpress?
- Force “submit for review” on update?
- Run only on plug-in activation instead of wp_head
- Accept HTML in custom function
- How to access my WP site locally and publicly? [closed]
- A delete/trash all function?
- DOM reference to TinyMCE editor element (button)
- Build custom pdf from users choices
- Show Header When Not In Iframe
- How do I display entries in the top menu based on conditions selected in the sidebar menu?
- Background setting isn’t applied to entire sidebar height
- Show specific posts as static html/php
- custom loop issue
- ‘str_replace’ and ‘strtr’ not working inside plugin
- How to create new page can get custom field other post in wordpress
- How to customize theme?
- Buddypress: New Tab and Page (Members Profile) [closed]
- How come there is no wp-content directory in wp theme
- Creating Action Hooks For Automatic File Inclusion
- SAving PHP in custom wordpress option field
- Does WordPress support template resources/custom fields
- Change default “Apply Changes To” radio option when editing images
- How to define a static Home page
- Weird capabilities / roles behavior
- How to appoint different users to different pages?
- Change post template the proper way
- let users add multiple divs in static page
- Custom search results
- How do I change text of a product template in a WooCommerce theme? [closed]