I'm trying to conditionally display some content in a theme using custom roles and capabilities. First, I define a custom role:

add_role('free_vendor', 'Free Vendor Listing', array('read', 'edit_posts', 'delete_posts', 'display_map'));

This is run directly from functions.php (do I need to add it to an action somewhere?)

Then I use the display_map capability I defined to conditionally display the map:

if (author_can($post, 'display_map')) echo '<li><a href="#map">Map</a></li>';

The only problem is, it doesn't work! Is there some issue in using author_can() with custom post types? Am I not calling add_role() properly? I can't really find any decent documentation on using author_can() with anything other than vanilla posts. Is this just not possible?

  • These roles are basically permissions that advertisers on the site will be able to pay for, ie: The free account can display a map, but not a link to their website. The deluxe account can display a map, website, and other stuff. I'm trying to use roles so I have flexibility to change the ad packages easily later. – user2579 Jan 18 '11 at 22:53

first about the add_role

you only need to run this once, so after you paste it in your functions.php and saved you can remove it and save again and the role will be there always.

then about the author_can function, i really never use it so i can tell whats wrong but you can use current_user_can() function like this:


if ( is_user_logged_in() ) {
     //first you get the curent user info
     //then you can check capabilities like this
      echo '<li><a href="#map">Map</a></li>';

hope this helps

| improve this answer | |
  • Doesn't current_user_can() use the ID of the currently logged in user? – user2579 Jan 18 '11 at 22:18
  • Damn. Apparently not! That function name doesn't make it readily apparent, but that worked. Thanks a million! – user2579 Jan 18 '11 at 22:23
  • glad i could help! – Bainternet Jan 18 '11 at 22:28
  • Nope, spoke too soon. I didn't test it as a logged out user. Here's the error: Warning: Illegal offset type in isset or empty in C:\xampp\htdocs\clients\blushingbride\wp-includes\capabilities.php on line 741 – user2579 Jan 18 '11 at 22:46
  • i added to the code is_user_logged_in() so this will only run if the user is logged in. – Bainternet Jan 18 '11 at 23:11

Try using current_user_can() instead.

| improve this answer | |

Okay, so here's what seems to be happening. Adding capabilities directly to the role when it's created doesn't seem to work (at least for me). So, instead of this:

add_role('free_vendor', 'Free Vendor Listing', array('read', 'edit_posts', 'delete_posts', 'display_map'));

It needs to be something like this:

$free_vendor_caps = array('read', 'edit_posts', 'delete_posts', 'display_map');
add_role('free_vendor', 'Free Vendor Listing');
$free_vendor = get_role('free_vendor');
foreach ($free_vendor_caps as $cap) {

Then I can do what I need to do with author_can():

<?php if (author_can($post->ID, 'display_map')) : ?>
<div id="map">
    <?php the_map(); ?>
<?php endif; ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy