What’s the Best Function to Hook remove_cap To?

[ad_1]

Thread Starter
Samuel

(@samuelbacay)

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' );
  }

Thread Starter
Samuel

(@samuelbacay)

ChatGPT solved it! I should’ve been using __FILE__ instead of pointing the hooks to the class’s file. I didn’t know require_once() appends the file’s content into the plugin’s main file.

  • This reply was modified 8 minutes ago by Samuel. Reason: Correct typographical error

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer