Advertisement
wp-coding

Caps on Roles (also on CPT)

May 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Set capabilities to a role (no need for members plugin!)
  4.  *
  5.  * This function can/will handle multiple custom post types and roles at once!
  6.  *
  7.  * Disable/Remove the function after once using it.
  8.  * (settings will stay in the DB and there is NO need to call it over and over again!
  9.  *
  10.  * Read more {@link https://codex.wordpress.org/Function_Reference/add_cap}
  11.  *
  12.  * Works with WP @version 5.0.3 and below
  13.  */
  14. add_action( 'init', 'cpt_caps_to_roles' );
  15. function cpt_caps_to_roles()
  16. {
  17.     $args  = array( 'posts', 'pages', 'cpt-name', 'cpt-name', 'cpt-name', );
  18.     $roles = array( get_role( 'administrator' ), get_role( 'editor' )  );
  19.  
  20.     foreach( $args as $arg )
  21.     {
  22.         $caps = array(
  23.             'read',
  24.             'read_' . $arg,
  25.             'edit_' . $arg,
  26.             'read_' . $arg,
  27.             'delete_'   . $arg,
  28.             'edit_'     . $arg,
  29.             'edit_others_'  . $arg,
  30.             'publish_'      . $arg,
  31.             'read_private_' . $arg,
  32.             'delete_'       . $arg,
  33.             'delete_private_'   . $arg,
  34.             'delete_published_' . $arg,
  35.             'delete_others_'    . $arg,
  36.             'edit_private_'     . $arg,
  37.             'edit_published_'   . $arg,
  38.             );
  39.  
  40.         foreach( $roles as $role ) {
  41.             foreach( $caps as $cap ) {
  42.                 $role->add_cap( $cap );
  43.             }
  44.         }
  45.  
  46.     } // end first foreach
  47. } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement