• Aleksandar

    (@jashimili)


    Everywhere on the net i was searching, no one is mentioning which one goes first and is it possible to use one hook theme activation for both roles and capabilities. They both are saved in database and as i see although they are linked with each other, they are not linked in database as foreign keys, they are even in same option saved.

    So my question is as both codex guides references that roles and capabilities should be added or removed on theme/plugin activation, they use different approach.

    In add capabilities codex page suggest to run on theme page and check if it’s activated, otherwise remove capability.

    add_action( 'load-themes.php', 'add_theme_caps' );

    And in roles codex page they suggest register activation hook, which is basically used for plugin activations

    register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );

    So which one would be correct? can i use one hook on theme activation to add both new roles and new capabilities. My point is that i want to add a few roles

    Confirmed poster
    Unconfirmed poster
    Blocked poster

    And a few capabilities hooked to those roles like

    create_new_listing
    edit_own_listing
    delete_own_listing
    send_private_message
    edit_own_profile

    On top of that “listings” are custom post type so should i also add capabilities to that custom post type too?

    register_post_type(
    'listings',
    array(
       'public' => true,
       'capability_type' => 'listings',
       'capabilities' => array(
       'publish_posts' => 'create_new_listing',
       'edit_posts' => 'edit_own_listings',
       'edit_others_posts' => 'edit_others_listing',
       'read_private_posts' => 'read_private_listing',
       'edit_post' => 'edit_listing',
       'delete_post' => 'delete_listing',
       'read_post' => 'read_listing',
    ),
    )
    );

    So which order goes on activation and adding custom roles and capabilities?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can change both roles and capabilities from the same hook. As you’ve observed, they affect the same DB entries. Which hook you use depends on where your code lies. Plugins use one hook, themes a different one. There is no overlap. The important point is because the roles and capabilities are saved in the DB, you do not want to use any hook that runs on every page load.

    Typically, you would specify the capabilities as an argument array when you add the role, so the addition roles and capabilities are simultaneous. If you add capabilities after the fact, the role would need to exist first in order to add to it. You can also add roles to individual users regardless of role, in which case it does not matter if the role exists or not.

    And yes, you need to add the capabilities when you register a CPT if you want them to be considered in post management. Otherwise the regular post capabilities are used. When the CPT is registered, the capabilities are just stored in an array. It doesn’t matter which users have capabilities at this point, nor if they exist at all. It’s just data at this point.

    Only when someone actually tries to do something with the CPT does their assigned capabilities mean anything. This is well after the post type registration and the current user is established, along with their assigned roles and capabilities.

    vince0190

    (@vince0190)

    you may try custom capabilities first then check it out what will be the out put

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Which goes first custom roles or custom capabilities?’ is closed to new replies.