Support

Account

Home Forums General Issues ACF and Custom Roles

Solving

ACF and Custom Roles

  • On one of my sites I created custom Roles that have to be able to edit their own custom posts and NOT the ones of other people.

    But to my surprise ACF does not work with these custom Roles.., After some investigation it seems to be necesairy that userroles have the capability ‘edit_others_posts’ to be abled to use ACF, but of course this gives them the posability to edit others posts…!

    How can I work around this issue?

    Thanks

  • I’d like to test this out but I need some information. What did you use to create the custom post type and the user role that only lets the role edit posts of the custom post type and only their own posts.

  • This works for me, the custom role can create and modify his ‘presentacion’ but not the ones of other users…
    But ACF needs a capability ‘edit_others_posts’ or in this case ‘edit_others_presentaciones’ to work, if the role doesn’t have this capability the ACF fields don’t work. ( in backend).

    But meanwhile I think I found a solution in making frontend forms to edit the ‘presentaciones’, I’ve seen that in the frontend the ACF fields works fine without these capabilities.

    
    <?php 
      
      // Register Presentacion Post Type
      function presentacion_post_type() {
        $labels = array(
          'name' => _x( 'Presentaciones', 'Post Type General Name', 'text_domain' ),
          'singular_name' => _x( 'Presentacion', 'Post Type Singular Name', 'text_domain' ),
          'menu_name' => __( 'Presentacion', 'text_domain' ),
          'name_admin_bar' => __( 'Presentaciones', 'text_domain' ),
          'parent_item_colon' => __( 'Presentacion padre:', 'text_domain' ),
          'all_items' => __( 'Todos los Presentaciones', 'text_domain' ),
          'add_new_item' => __( 'Añadir nueva Presentacion', 'text_domain' ),
          'add_new' => __( 'Añadir nueva', 'text_domain' ),
          'new_item' => __( 'Nueva Presentacion', 'text_domain' ),
          'edit_item' => __( 'Editar Presentacion', 'text_domain' ),
          'update_item' => __( 'Actualizar Presentacion', 'text_domain' ),
          'view_item' => __( 'Ver Presentacion', 'text_domain' ),
          'search_items' => __( 'Buscar Presentacione', 'text_domain' ),
          'not_found' => __( 'Presentacion no encontrada', 'text_domain' ),
          'not_found_in_trash' => __( 'Presentacion no en papelera', 'text_domain' ),
        );
        $rewrite = array(
          'slug' => 'transportista',
          'with_front' => true,
          'pages' => true,
          'feeds' => true,
        );
        $capabilities = array(
          'edit_post' => 'edit_presentacion',
          'edit_posts' => 'edit_presentaciones',
          'read_post' => 'read_presentacion',
          'delete_post' => 'delete_presentacion',
          'edit_others_posts' => 'edit_others_presentaciones',
          'publish_posts' => 'publish_presentaciones',
          'read_private_posts' => 'read_private_presentaciones',
        );
        $args = array(
          'label' => __( 'Presentacion', 'text_domain' ),
          'description' => __( 'Presentacion de transportista', 'text_domain' ),
          'labels' => $labels,
          'supports' => array(
            'title',
            'editor',
            'excerpt',
            'author',
            'thumbnail',
            'comments',
            'custom-fields'
          ),
          'taxonomies' => array( 'provincia' ),
          'hierarchical' => false,
          'public' => true,
          'show_ui' => true,
          'show_in_menu' => true,
          'menu_position' => 5,
          'menu_icon' => 'dashicons-id',
          'show_in_admin_bar' => true,
          'show_in_nav_menus' => true,
          'can_export' => true,
          'has_archive' => true,
          'exclude_from_search' => false,
          'publicly_queryable' => true,
          'rewrite' => $rewrite,
          'capabilities' => $capabilities,
        );
        register_post_type( 'presentacion', $args );
      }
      add_action( 'init', 'presentacion_post_type', 0 );
      
      // Add a custom user role
      $result = add_role( 'transporter', __('Transporter' ), array( 'read' => true, ) );
      $result = add_role( 'cliente', __('Cliente' ), array( 'read' => true, ) );
      
      // add capabilities to roles editor and administrator
      global $wp_roles;
      $capabilities = array(
        'edit_presentacion',
        'edit_presentaciones',
        'read_presentacion',
        'delete_presentacion',
        'edit_others_presentaciones',
        'publish_presentaciones',
        'read_private_presentaciones',
        'edit_transporte',
        'edit_transportes',
        'read_transporte',
        'delete_transporte',
        'edit_others_transportes',
        'publish_transportes',
        'read_private_transportes',
        'manage_transportype',
        'edit_transportype',
        'delete_transportype',
        'assign_transportype',
        'manage_provincia',
        'edit_provincia',
        'delete_provincia',
        'assign_provincia'
      );
      $role = get_role( 'administrator' );
      foreach( $capabilities as $cap ) {
        $role->add_cap( $cap );
      }
      $role = get_role( 'editor' );
      foreach( $capabilities as $cap ) {
        $role->add_cap( $cap );
      }
      $transcapabilities = array( 
        'edit_presentacion',
        'edit_presentaciones',
        'read_presentacion',
        'upload_files',
        'assign_provincia',
        'assign_transportype',
        'edit_others_posts'
      );
      $role = get_role( 'transporter');
      foreach( $transcapabilities as $cap ) {
        $role->add_cap( $cap );
      }
      $notranscapabilities = array(
        'edit_posts',
        'edit_pages',
        'create_posts',
        'manage_categories',
        'publish_posts',
        'edit_themes',
        'install_plugins',
        'update_plugin',
        'update_core',
        'delete_presentacion',
        'edit_others_presentaciones',
        'publish_presentaciones',
        'read_private_presentaciones',
        'delete_transporte',
        'edit_others_transportes',
        'publish_transportes',
        'read_private_transportes'
      );
      foreach( $notranscapabilities as $cap ) {
        $role->remove_cap( $cap );
      }
      $clientcapabilities = array(
        'edit_transporte',
        'edit_transportes',
        'read_transporte',
        'assign_provincia',
        'assign_transportype'
      );
      $role = get_role( 'cliente' );
      foreach( $clientcapabilities as $cap ) {
        $role->add_cap( $cap );
      }
      $noclientcapabilities = array(
        'edit_posts',
        'edit_pages',
        'edit_others_posts',
        'create_posts',
        'manage_categories',
        'publish_posts',
        'edit_themes',
        'install_plugins',
        'update_plugin',
        'update_core',
        'edit_presentacion',
        'edit_presentaciones',
        'read_presentacion',
        'delete_presentacion',
        'edit_others_presentaciones',
        'publish_presentaciones',
        'read_private_presentaciones',
        'delete_transporte',
        'edit_others_transportes',
        'publish_transportes',
        'read_private_transportes'
      );
      foreach( $noclientcapabilities as $cap ) {
        $role->remove_cap( $cap );
      }
      
    ?>
    
  • I’ve tried adding your code to my functions.php file, added a field group to the post type and logged in as a user of ‘Transporter’ and I could see and edit the fields in that group. I did not set any location rules on the field group.

    You’ll need to dig deeper, my test was on a site with a default WP theme and only ACF installed. Have you tried disabling other plugins and using a default theme?

  • The point is that yes you can edit, but not having the acf fields…

    When I test in another domain, without any plugins only acf and my function file it happens the same…

    Of course I need to export the field group too to be able to see it well

    But as I said before I think my solution is in taking the editing to the frontend where I see it works well..

    Dave

  • I don’t understand how it’s not working. Maybe I’m confused. I added your code which added a post type and 2 user types. I went in as admin and creates some field groups and added them to your post type. Then I logged into one of the users to edit a post and the acf fields were working on that fields.

    Are you talking about allowing these users to edit the ACF field groups?

  • If you want to see it work I would have to send you the exported fieldgroup otherwise you can’t see the ACF fields.

    To use and see the ACF fields, a user Role needs to have the ‘edit_other_posts’ capability active. (accoring to posts I read in your forum)

    And thats exactly a capability that I don’t want to give them.

    Dave

  • Sorry if you misunderstood, but I’m not the developer and this isn’t my forum, I’m just a guy that uses ACF a lot and helps out here on the user forum.

    I don’t really need to see your field group, but I need to understand where the disconnect we’re having is.

    Do you want these users to be able to edit the content of a post or other object that has ACF groups attached to them? Example, click on “Presentaciones” and add new or edit and existing post and change the content.

    OR

    Do you want the users to be able to click on “Custom Fields” and be able to update and modify their own field groups?

  • Hello John,

    Yes the point is that I want a user (transporter) to be capable of only editing his presentation, but of course not the ones of other users.

    Thats why I created separate user Roles and have to deny them other capabilities.
    But to my surprise every thing worked well when I’m connected as an administrator, but when I connect as a Transporter, suddenly the ACF fields are gone and I’m left with only the basic post fields.

    Searching in forum and googling a bit I found out that any user has to have capability: edit_others_xxxx activated (where xxxx in this case would be ‘presentaciones’) to be able to use ACF fields when editing post.

    But of course this gives them the posability to edit the presentations of other users too..

    Dave

  • I’ve set up a similar situation on a test site and I’m not seeing the behavior you’re seeing.

    With your post type set up and your user roles
    When I log in as one of the user types I cannot edit the post type at all, it does not appear in the admin menu.
    When I log in as the other user type I can edit the post type and any ACF field groups that I add to that post type are visible and I can edit them
    Sorry, I don’t remember which role is which

    What are the location rules on the field groups you have on this post type?

  • The transporter can edit ‘presentacion’, the other one is for future use….

    DAve

  • I think you may have missed my other question. It’s the only other thing I can think of that could be causing your problem.

    What are the location rules on the field groups you have on this post type?

  • I added a standard location rule only show ACF when post_type is equal to ‘presentation’.

    I think that is the way it should work no ?

    Dave

  • Like I said, I can’t recreate the problem. When I add the post type and the users, etc, using your code and then I add field groups to the post type, when I log in as the user type I can see and edit the fields. I can only guess that there is something else going on with your site that is causing a compatibility issue. If you haven’t done so already you should open a support ticket. They may be able to help you better http://support.advancedcustomfields.com/new-ticket/

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘ACF and Custom Roles’ is closed to new replies.