I found this page in the documentation, but the deactivation hook isn’t being fired, when I deactivate the plugin. The permissions aren’t being restored.
Here’s all related codes:
register_activation_hook( GUIDE_POSTS__PLUGIN_DIR . 'class.guides.php', array( 'Guides', 'activate_plugin' ) );
register_deactivation_hook( GUIDE_POSTS__PLUGIN_DIR . 'class.guides.php', array( 'Guides', 'deactivate_plugin' ) );
public static function activate_plugin() {
self::register_guides_and_grant_permissions();
}
public static function deactivate_plugin() {
// Unregister Guides
unregister_post_type( 'guides' );
flush_rewrite_rules;
// Grant role permissions
$editor = get_role( 'editor' );
$editor->remove_cap( 'manage_categories' );
$contributor = get_role( 'contributor' );
$contributor->remove_cap( 'upload_files' );
}
public static function register_guides_and_grant_permissions() {
$labels = array(
'name' => 'Guides',
'singular_name' => 'Guide',
'menu_name' => 'Guides',
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'guide' ),
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'taxonomies' => array( 'category' ),
'menu_icon' => 'dashicons-location',
'show_in_rest' => true,
);
// Register the custom post type
register_post_type( 'guide', $args );
// Grant role permissions
$editor = get_role( 'editor' );
$editor->add_cap( 'manage_categories' );
$contributor = get_role( 'contributor' );
$contributor->add_cap( 'upload_files' );
}