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

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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

share|improve this answer
    
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? –  IamSJ Dec 18 '13 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... –  Paul Geisler Dec 20 '13 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. –  IamSJ Dec 21 '13 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'? –  Paul Geisler Dec 21 '13 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). –  IamSJ Jan 14 at 16:56
show 6 more comments

Your Answer

 
discard

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

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