Support

Account

Home Forums General Issues ACF and Custom Roles Reply To: ACF and Custom Roles

  • 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 );
      }
      
    ?>