2

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

2

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

12
  • Hello, I have tried the new code. Using that code I succeeded disabling any other users other than the role "author". But the main problem still remain intact. A person with role "author" is still able to access that "edit" button on all the posts rather than just his posts. I stuck there from the beginning. Can you suggest me something for this?
    – Netizen
    Commented Dec 18, 2013 at 14:33
  • You need to be clear in your original question... it says "...based on their roles." and now you say "the main problem" is that all authors are able to edit all posts... Commented Dec 20, 2013 at 18:17
  • Hello Paul Geisler,sorry that I didn't made myself clear. I meant to say that the plugin specified "Edit" button should appear based on roles particularly to 'authors',the authors should have access to their own published posts and not others. Say, you and I are the authors. I shouldn't have access to that "edit" button on your posts and you shouldn't have access to mine. Admin will have access to all the posts.
    – Netizen
    Commented Dec 21, 2013 at 5:28
  • So... a post should be editable for writer of the post and all higher roles than 'author', for example 'editor' and 'admin'? Commented Dec 21, 2013 at 10:15
  • 1
    Here is the final code- pastebin.com/GEUCebBi. I should say the problem is resolved. I request you to check if what I used it fine and there is nothing wrong in my code(my knowledge in php is limited and don't know if what I used is correct).
    – Netizen
    Commented Jan 14, 2014 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.