Support » Plugins » Allow Subscriber to Add Content

  • I have started working on a plugin that creates a custom post type, and allows users to upload files with the post. The problem I’m finding is that the only way that the user can add the content, is to give them “edit_esthemes” permissions. This not only allows them to create their own posts, this also allows them to delete published posts, and upload files to posts. They can’t edit the posts themselves, but they can upload files to a post.

    add_action('init', 'estheme_custom_init');
    function estheme_custom_init() {
      register_post_type('estheme',array(
        'labels' => $labels,
        'public' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'capabilities' => array(
          'edit_posts' => 'edit_esthemes',
          'edit_post' => 'edit_estheme',
          'delete_post' => 'delete_estheme',
        ),
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields'),
        'rewrite' => true,
        'menu_position' => 5,
      ));
    }
    
    add_action('admin_init', 'estheme_caps');
    function estheme_caps() {
      // Allow subscribers to post
      global $wp_roles;
      foreach($wp_roles->get_names() as $role_key => $role_name) {
        $role =& get_role($role_key);
        if($role->has_cap('read')) {
          $role->add_cap('edit_esthemes');
          # The instructions say to leave the last 's' off in a few places, but subscribers can't see "esthemes" in the admin menu.
          //$role->add_cap('edit_estheme');
          //$role->add_cap('upload_files');
          # Subscribers can still delete without this...
          //$role->add_cap('delete_esthemes');
        }
      }
    }
  • The topic ‘Allow Subscriber to Add Content’ is closed to new replies.