Created
April 9, 2019 10:09
-
-
Save sazzadh/b08634304b427cd8d289708a9067ab54 to your computer and use it in GitHub Desktop.
WP Custom user role with custom post type edit capability. Using this code we will be able to control a user to edit a post type only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Registering new Role | |
===============================================*/ | |
function TallyFunctions_add_roles_on_plugin_activation() { | |
add_role( 'shop_helper', 'Shop Helper', array( 'read' => true, 'level_1' => true ) ); | |
} | |
register_activation_hook( __FILE__, 'TallyFunctions_add_roles_on_plugin_activation' ); | |
/* | |
Edit post type for added our new capabilities | |
===============================================*/ | |
add_filter( 'register_post_type_args', 'TallyFunctions_change_capabilities_of_shop_helper' , 10, 2 ); | |
function TallyFunctions_change_capabilities_of_shop_helper( $args, $post_type ){ | |
$custom_types = array('tallykit_doc', 'preview', 'tally_showcase', 'product'); | |
foreach ($custom_types as $custom_type){ | |
if ( $custom_type === $post_type ) { | |
$args['capabilities'] = array( | |
'edit_post' => 'edit_shophelper_post', | |
'edit_posts' => 'edit_shophelper_posts', | |
'edit_others_posts' => 'edit_other_shophelper_posts', | |
'publish_posts' => 'publish_shophelper_posts', | |
'read_post' => 'read_shophelper_post', | |
'read_private_posts' => 'read_private_shophelper_posts', | |
'delete_post' => 'delete_shophelper_post' | |
); | |
} | |
} | |
return $args; | |
} | |
/* | |
Add the role capability | |
===============================================*/ | |
add_action('admin_init','rpt_add_role_caps',999); | |
function rpt_add_role_caps() { | |
$role = get_role('shop_helper'); | |
$role->add_cap( 'edit_shophelper_post'); | |
$role->add_cap( 'edit_shophelper_posts' ); | |
$role->add_cap( 'edit_other_shophelper_posts' ); | |
$role->add_cap( 'publish_shophelper_posts' ); | |
$role->add_cap( 'read_shophelper_post' ); | |
$role->add_cap( 'publish_course_documents' ); | |
$role->add_cap( 'read_private_shophelper_posts' ); | |
$role->add_cap( 'delete_shophelper_post' ); | |
} | |
/* | |
Allow this role to access wp-admin when using WooCommerce | |
===============================================*/ | |
function tally_theme_enable_wp_admin_user(){ | |
if( is_user_logged_in() ){ | |
$tally_theme_enable_wp_admin_user = new WP_User(get_current_user_id()); | |
if($tally_theme_enable_wp_admin_user->roles[0] == "shop_helper"){ | |
add_filter( 'woocommerce_prevent_admin_access', '__return_false' ); | |
add_filter( 'woocommerce_disable_admin_bar', '__return_false' ); | |
} | |
} | |
} | |
add_action('init', 'tally_theme_enable_wp_admin_user'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment