How can I let users to access plugin functions based on roles?

You can check if current user is an Author (WP-Codex)

$current_user_is_allowed_to_edit = false;
if ( current_user_can('delete_posts') ) {
        $current_user_is_allowed_to_edit = true;
}

And use it in the if-statement at the point where you implement the Live Edit function.

Here is the whole code (for user roles): http://pastebin.com/fY7UPcB1

EDIT:

Here is the new code (check for user role and if it’s his own post):

// Check if User can Edit
$current_user_is_allowed_to_edit = false;
$user_ID = get_current_user_id();
$post_author_ID = the_author_meta('ID');

if ( current_user_can('delete_posts') && $post_author_ID == $user_ID ) {
        $current_user_is_allowed_to_edit = true;
}

http://pastebin.com/NFtNVAGE

EDIT #2:

Now the button will appear only for users who written the post (Authors) and every user with higher user role (Editor, Admin).

// Check if User can Edit
// Set permission to false="not allowed"
$current_user_is_allowed_to_edit = false;
// Get the ID of the user who watches this post
$user_ID = get_current_user_id();
// Get the ID of the Post-Writer
$post_author_ID = the_author_meta('ID');

//   Check if User has role 'Editor' and higher  OR is the Post-Writer
if ( current_user_can('delete_posts') || $post_author_ID == $user_ID ) {
    // Set permission to true="allowed"
    $current_user_is_allowed_to_edit = true;
}

Code: http://pastebin.com/8u6bV018

Leave a Comment