0

Help with new user wordpress capabilities and roles custom. The media not work upload and display images in galery.

image error: https://i.sstatic.net/VE59k.png

code:

if ( get_role('user_products') ) {
    remove_role('user_products');
}

add_action('init','add_my_custom_role');
function add_my_custom_role() {

    add_role( 
      'user_products', 
      __( 'User Products', 'editproducts' ), 
      array(
               'read'            => true,
               'create_posts'      => true,
               'edit_posts'        => true,
               'edit_others_posts' => true,
               'delete_posts' => true,
               'upload_files' => true,
               'upload_medias' => true,                   
               'edit_files' => true,
               'publish_posts' => true,
               'manage_categories' => true,
               'edit_comment' => true,
               'edit_products' => true,
               'edit_others_products' => true,
               'publish_products' => true,
               'delete_products' => true,
               'read_private_products' => true,
               'delete_private_products' => true,
               'edit_private_products' => true,
               'edit_published_products' => true,
               'delete_others_pages' => true,
               'delete_others_posts' => true,
               'delete_pages' => true,
               'delete_private_pages' => true,
               'delete_private_posts' => true,
               'delete_published_pages' => true,
               'delete_published_posts' => true,
               'edit_others_pages' => true,
               'edit_pages' => true,
               'edit_private_pages' => true,
               'edit_private_posts' => true,
               'edit_published_pages' => true,
               'edit_published_posts' => true,
               'manage_links' => true,
               'moderate_comments' => true,
               'publish_pages' => true,
               'read_private_pages' => true,
               'read_private_posts' => true,
               'unfiltered_html' => false,
               'level_3' => true,                   
              // 'edit_attachments' => true,
              // 'delete_attachments' => true,
              // 'read_others_attachments' => true,
              // 'edit_others_attachments' => true,
              // 'delete_others_attachments' => true,

        // Various Capabilities
    ));
}

Code update, i have the same error:

add_action('init','add_my_custom_role');
function add_my_custom_role() {

    add_role( 
      'user_products', 
      __( 'User Products', 'editproducts' ), 
      array(
               'read'            => true,
               'create_posts'      => true,
               'edit_posts'        => true,
               'edit_others_posts' => true,
               'delete_posts' => true,
               'upload_files' => true,
               'upload_medias' => true,                   
               'edit_files' => true,
               'publish_posts' => true,
               'manage_categories' => true,
               'edit_comment' => true,
               'edit_products' => true,
               'edit_others_products' => true,
               'publish_products' => true,
               'delete_products' => true,
               'read_private_products' => true,
               'delete_private_products' => true,
               'edit_private_products' => true,
               'edit_published_products' => true,
               'delete_others_pages' => true,
               'delete_others_posts' => true,
               'delete_pages' => true,
               'delete_private_pages' => true,
               'delete_private_posts' => true,
               'delete_published_pages' => true,
               'delete_published_posts' => true,
               'edit_others_pages' => true,
               'edit_pages' => true,
               'edit_private_pages' => true,
               'edit_private_posts' => true,
               'edit_published_pages' => true,
               'edit_published_posts' => true,
               'manage_links' => true,
               'moderate_comments' => true,
               'publish_pages' => true,
               'read_private_pages' => true,
               'read_private_posts' => true,
               'unfiltered_html' => false,
               'level_3' => true,                   
              // 'edit_attachments' => true,
              // 'delete_attachments' => true,
              // 'read_others_attachments' => true,
              // 'edit_others_attachments' => true,
              // 'delete_others_attachments' => true,

        // Various Capabilities
    ));
}
3
  • If i'm reading that right, on every page load, you delete and remove that role from the database then recreate it again from scratch? Roles aren't like post type registrations, they're saved to the database
    – Tom J Nowell
    Commented May 20, 2019 at 23:10
  • Thanks for information! I am delete the code for remove role. Commented May 21, 2019 at 1:29
  • and i have the same error, image> i.imgur.com/c48k0bB.png Commented May 21, 2019 at 1:30

1 Answer 1

0

This happens because on every page load you call remove_role

As the docs note:

When a role is removed, the users who have this role lose all rights on the site.

Roles are not like post types, they exist in the database, and removing/adding a role updates the database. For that reason you shouldn't add them on every page load, it's better to do it on theme activation or plugin activation

3
  • Thanks for help! i am delete code that remove. i need desable and enable theme for update the role? I have the same error, image> i.imgur.com/c48k0bB.png Commented May 21, 2019 at 1:31
  • I just have this code with the same error: Commented May 21, 2019 at 16:50
  • I Solved! Thanks! i delete one code that display text with id of user, he is cause of not work the media right. thanks! Commented May 21, 2019 at 18:57

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.