Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

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:

update

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

hope this helps

share|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.

share|improve this answer
    
Oh, and thank you too! –  user2579 Jan 18 '11 at 22:24

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) {
    $free_vendor->add_cap($cap);
}

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.