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

I use a plugin called “Live Edit” to let authors and editors edit the content front-end. But the issue is that the plugin lets any logged in users to edit the content. I want the edit(specified by plugin. Not the WP’s default edit button) button be accessed based on their roles. I don’t know how to make it accessible based on roles. So trying to make it appear only for the authors and higher roles to that.

I want to do something like this in loop.php:

<?php
global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "author" ) ){ 
// Show Role
// Show Subscriber Image
} 

Here is the code:

 <?php post_class( 'post clearfix ' . $themify->get_categories_as_classes(get_the_ID()) . ' ' . $themify->get_post_color_class(get_the_ID()) ); if(function_exists("live_edit")){ live_edit('post_title, field1'); } ?>

and

<div <?php if(function_exists("live_edit")){ live_edit('post_content, myfield2'); }?>>
        <?php if( function_exists('the_field') ) the_field('myfield1'); ?> </div>
        <?php if( function_exists('the_field') ) the_field('myfield2');  ?>
</div>

I am unsure how to do it in the template or through functions.php. Could any one help me or guide through that to achieve what I want?

Here is the loop.php at pastebin – http://pastebin.com/9MVY64LJ

The live edit class is used in line 8 and line 57.

enter image description here

1 Answer
1

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