database - WordPress - Custom user role - grant guest access to edit.php without insert/update/delete

416

I am working on a site where I want to show customers a demo-version of a product. To do this they can login with a guest-account, I have created this role as follows

add_role('guest', __('Guest'), array('read'=> true));

When I login as a guest I get redirected to and get to see theoverview-page /admin.php?page=my_overview, so this does work, but when the guest clicks on any item in this overview I get a message saying: You don't have permission to access this page.

These items refer to pages like:/profile.php /edit.php /users.php /admin.php Since using these pages is a basic part of my products functionality, I want to grant the guest account access to these pages, but don't let them insert/update/delete any data which is shown on those pages.

I have tried adding multiple different privilegesi.e. edit_posts, edit_private_posts, edit_private_pages, read_private_posts, read_private_pages but I can't get it to work right.

I would like to achieve this guest account with specific permissions without the use of any plugin. Basically I want to create a user that can do the same as an Author, but without anyINSERTs/UPDATEs/DELETEs on my demo-site to the database

I don't know if it is even possible, but any help would be appreciated.

911

Answer

Solution:

I have figured it out, I was only creating a role, but I didn't add any capabilities to it. The code I was missing will be like and its working now:

function add_capability() {
    // gets the author role
    $role = get_role( 'guest' );
    // This only works, because it accesses the class instance.
    $role->add_cap( 'edit_others_posts' ); 
    $role->add_cap( 'read_post' ); 
    $role->add_cap( 'edit_posts' ); 
}
add_action( 'admin_init', 'add_capability');

People are also looking for solutions to the problem: php - If return value is scalar, push as one row; if iterable, push as multiple rows

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.