• Resolved Samuel

    (@samuelbacay)


    I’m planning to grant permission and remove permission in a plugin. It says on the remove_cap() documentation that it should only be ran once. I guess same goes for add_cap(). Which function should I hook the function that contains these two to?

    • This topic was modified 1 year, 4 months ago by Samuel. Reason: Remove typo
Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 1 year, 4 months ago by Samuel. Reason: Correct typographical error
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘What’s the Best Function to Hook remove_cap To?’ is closed to new replies.