具有供应商角色的自定义帖子类型

时间:2016-11-08 作者:chudasamachirag

我正在尝试创建添加WC供应商,其中我必须仅为供应商用户创建自定义帖子类型。

下面是我使用的代码片段

 register_post_type( \'acme_product\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Products\' ),
        \'singular_name\' => __( \'Product\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
    )
  );

above code works well with admin user.

2 个回复
SO网友:Syed Fakhar Abbas

首先,必须向register\\u post\\u type()函数传递两个附加参数。

然后,您可以使用add_cap(); 作用


register_post_type( \'acme_product\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Products\' ),
        \'singular_name\' => __( \'Product\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
      \'capability_type\'  => array(\'acme_product\',\'acme_products\'),
      \'map_meta_cap\'        => true,
    )
  );

    add_action(\'admin_init\',\'vendor_role_caps\');

    function vendor_role_caps() {

      $role = get_role(\'vendor_user\');
      $role->add_cap( \'read\' );
      $role->add_cap( \'read_acme_product\');
      $role->add_cap( \'read_private_acme_products\' );
      $role->add_cap( \'edit_acme_product\' );
      $role->add_cap( \'edit_acme_products\' );
      $role->add_cap( \'edit_others_acme_products\' );
      $role->add_cap( \'edit_published_acme_products\' );
      $role->add_cap( \'publish_acme_products\' );
      $role->add_cap( \'delete_others_acme_products\' );
      $role->add_cap( \'delete_private_acme_products\' );
      $role->add_cap( \'delete_published_acme_products\' );

 }

SO网友:Benoti

您需要添加capability 和/或map_meta_cap 的参数数组register_post_type()

codex说:附加功能仅在map\\u meta\\u cap()中使用。因此,只有在将post类型注册为“map\\u meta\\u cap”参数设置为true(默认值为false)的情况下,才会默认分配它们。

以下是供应商的add\\u角色

add_role( \'vendor\', __(\'Vendor\', \'wcvendors\') , array(
         \'assign_product_terms\'     => true,
         \'edit_products\'            => true,
         \'edit_product\'             => true,
         \'edit_published_products\'  => false,
         \'manage_product\'           => true,
         \'publish_products\'         => false,
         \'read\'                     => true,
         \'upload_files\'             => true,
         \'view_woocommerce_reports\' => true,
         ) 
);
您将在codex页面上获得所有详细信息Here