Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Set capabilities to a role (no need for members plugin!)
- *
- * This function can/will handle multiple custom post types and roles at once!
- *
- * Disable/Remove the function after once using it.
- * (settings will stay in the DB and there is NO need to call it over and over again!
- *
- * Read more {@link https://codex.wordpress.org/Function_Reference/add_cap}
- *
- * Works with WP @version 5.0.3 and below
- */
- add_action( 'init', 'cpt_caps_to_roles' );
- function cpt_caps_to_roles()
- {
- $args = array( 'posts', 'pages', 'cpt-name', 'cpt-name', 'cpt-name', );
- $roles = array( get_role( 'administrator' ), get_role( 'editor' ) );
- foreach( $args as $arg )
- {
- $caps = array(
- 'read',
- 'read_' . $arg,
- 'edit_' . $arg,
- 'read_' . $arg,
- 'delete_' . $arg,
- 'edit_' . $arg,
- 'edit_others_' . $arg,
- 'publish_' . $arg,
- 'read_private_' . $arg,
- 'delete_' . $arg,
- 'delete_private_' . $arg,
- 'delete_published_' . $arg,
- 'delete_others_' . $arg,
- 'edit_private_' . $arg,
- 'edit_published_' . $arg,
- );
- foreach( $roles as $role ) {
- foreach( $caps as $cap ) {
- $role->add_cap( $cap );
- }
- }
- } // end first foreach
- } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement