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've added an action to my functions.php file that forces a plugin (Content Scheduler) to act on posts, even when it wasn't explicitly enabled by the person writing the post.

I've got that working, but I want it to work only when the author's role is one that I've specified, and better yet, work differently for different roles. The role-checking code isn't working. I'm attempting to call the "user_has_role" function (which I am calling successfully elsewhere in the same file, so I know that part should work), but either it is not working in this context, or it is not getting called at all.

function user_has_role( $roles_to_check=array() ) {
  if( ! $roles_to_check ) return FALSE;
  global $current_user;
  get_currentuserinfo();
  $user_id = intval( $current_user->ID );
  if( ! $user_id ) { return FALSE; }
  $user = new WP_User( $user_id ); 
  return in_array( $roles_to_check, $user->roles, FALSE );
}

function my_function($post_id, $post) {
    $is_role1 = user_has_role( array('role1') );
    $is_role2 = user_has_role( array('role2') );
    if ($is_role1) { $flag = 1; }
    if ($is_role2) { $flag = 2; }
    if ($flag != null) {        
    // do Content Scheduler stuff
    }
}

add_action( 'save_post', 'my_function', 10, 2);

Thanks in advance.

share|improve this question
1  
Any reason you're not using current_user_can()? –  m0r7if3r Mar 6 '12 at 16:53
    
The "user_has_role" function is one that I found elsewhere. I'm dealing with custom roles and post types, and haven't defined names for every capability I'd want to trap for. This is also not a capability per se: what I'm trying to do is set a post expiration date based on the user role. –  Adam Rice Mar 6 '12 at 18:17
1  
You can use current_user_can( 'administrator' ), etc for the names of user roles. –  m0r7if3r Mar 7 '12 at 1:42
    
Is this solved, abandoned or still open? Please update your question with the progress you made. –  toscho Mar 6 '13 at 23:40
    
I wound up abandoning it—ran out of time on the project, had to make do without. –  Adam Rice Mar 22 '13 at 15:37
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.