Php WordPress-自定义帖子类型、自定义角色、自定义功能

Php WordPress-自定义帖子类型、自定义角色、自定义功能,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在为自定义角色分配具有自定义帖子类型的自定义功能时遇到了一个问题 问题是我想删除我的自定义帖子类型的添加新(不是通过CSS破解或取消设置菜单项)选项。我已经找到了一些答案,提出了许多解决方案,但没有一个是完美的 最接近我想要的是: register_post_type( 'custom_post_type_name', array( 'capability_type' => 'post', 'capabilities' => array( 'create_posts

我在为自定义角色分配具有自定义帖子类型的自定义功能时遇到了一个问题

问题是我想删除我的自定义帖子类型的添加新不是通过CSS破解或取消设置菜单项)选项。我已经找到了一些答案,提出了许多解决方案,但没有一个是完美的

最接近我想要的是:

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // false < WP 4.5, credit @Ewout
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
一切都按照我的要求进行。我整天都在探索woocommerce代码,但找不到它是如何做到这一点的。其他人能用另一双眼睛帮我解决这个问题吗?:)


非常感谢。谢谢。

我已经告诉过你,你想做的计划给了我们更多的细节

以下是我认为对您有帮助的解决方案

如果要使用
map\u meta\u cap=>true
,则需要更改
capability\u类型以及您自己的类型,请参阅更多

但是,如果您设置自己的
capability\u type
值,则需要向您想要添加的用户角色添加功能

这是一个完整的例子

if ( !class_exists( 'WPSE64458_CPT_Register' ) ) {
  class WPSE64458_CPT_Register {

    function __construct() {
      add_action( 'init', array( $this, 'wpse64458_register_post_types' ), 5 );
      add_action( 'admin_init', array( $this, 'wpse64458_add_caps' ), 5 );
      add_filter( 'register_post_type_args', array( $this, 'wpse64458_modify_cpt_registry' ) , 10, 2 );

    }

    /**
     * Register core post types.
     */
    public function wpse64458_register_post_types() {

      if ( ! is_blog_installed() || post_type_exists( 'member' ) ) {
        return;
      }

      register_post_type( 'member',
        apply_filters( 'wpse64458_callb_post_type_member',
          array(
            'labels'              => array(
              'name'                  => __( 'Members', 'domaintext' ),
              'singular_name'         => __( 'Member', 'domaintext' ),
              'all_items'             => __( 'All Members', 'domaintext' ),
              'menu_name'             => _x( 'Members', 'Admin menu name', 'domaintext' ),
              'add_new'               => __( 'Add New', 'domaintext' ),
              'add_new_item'          => __( 'Add new member', 'domaintext' ),
              'edit'                  => __( 'Edit', 'domaintext' ),
              'edit_item'             => __( 'Edit member', 'domaintext' ),
              'new_item'              => __( 'New member', 'domaintext' ),
              'view'                  => __( 'View member', 'domaintext' ),
              'view_item'             => __( 'View member', 'domaintext' ),
              'search_items'          => __( 'Search members', 'domaintext' ),
              'not_found'             => __( 'No members found', 'domaintext' ),
              'not_found_in_trash'    => __( 'No members found in trash', 'domaintext' ),
              'parent'                => __( 'Parent member', 'domaintext' ),
              'featured_image'        => __( 'Member image', 'domaintext' ),
              'set_featured_image'    => __( 'Set member image', 'domaintext' ),
              'remove_featured_image' => __( 'Remove member image', 'domaintext' ),
              'use_featured_image'    => __( 'Use as member image', 'domaintext' ),
              'insert_into_item'      => __( 'Insert into member', 'domaintext' ),
              'uploaded_to_this_item' => __( 'Uploaded to this member', 'domaintext' ),
              'filter_items_list'     => __( 'Filter members', 'domaintext' ),
              'items_list_navigation' => __( 'Members navigation', 'domaintext' ),
              'items_list'            => __( 'Members list', 'domaintext' ),
            ),
            'public'              => true,
            'show_ui'             => true,
            'capability_type'     => 'member',
            'map_meta_cap'        => true,
            'menu_icon'          => 'dashicons-groups',
            'publicly_queryable'  => true,
            'exclude_from_search' => false,
            'hierarchical'        => false, // Hierarchical causes memory issues - WP loads all records!
            'rewrite'            => array( 'slug' => 'member' ),
            'query_var'           => true,
            'supports'            => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
            'has_archive'         => 'members',
            'show_in_nav_menus'   => true,
            'show_in_rest'        => true,
          )
        )
      );

    } // end of wpse64458_register_post_types


    /**
     * Get capabilities.
     *
     * @return array
     */
     private function wpse64458_set_caps() {
      $capabilities = array();

      $capability_types = array( 'member' );

      foreach ( $capability_types as $capability_type ) {

        $capabilities[ $capability_type ] = array(
          // Post type
          "edit_{$capability_type}",
          "read_{$capability_type}",
          "delete_{$capability_type}",
          "edit_{$capability_type}s",
          "edit_others_{$capability_type}s",
          "publish_{$capability_type}s",
          "read_private_{$capability_type}s",
          "delete_{$capability_type}s",
          "delete_private_{$capability_type}s",
          "delete_published_{$capability_type}s",
          "delete_others_{$capability_type}s",
          "edit_private_{$capability_type}s",
          "edit_published_{$capability_type}s",

          // Terms
          // "manage_{$capability_type}_terms",
          // "edit_{$capability_type}_terms",
          // "delete_{$capability_type}_terms",
          // "assign_{$capability_type}_terms",

        );
      }

      return $capabilities;
    }

    /*
      Add Capability
     */
    public function wpse64458_add_caps(){

      global $wp_roles;

      if ( ! class_exists( 'WP_Roles' ) ) {
        return;
      }

      if ( ! isset( $wp_roles ) ) {
        $wp_roles = new WP_Roles();
      }

      $capabilities = $this->wpse64458_set_caps();

      foreach ( $capabilities as $cap_group ) {
        foreach ( $cap_group as $cap ) {
          $wp_roles->add_cap( 'editor', $cap );
          $wp_roles->add_cap( 'administrator', $cap );
        }
      }
    }

    public function wpse64458_modify_cpt_registry( $args, $post_type ){

      // Do not filter any other post type
      if ( 'member' !== $post_type ) {

        // Give other post_types their original arguments
        return $args;

      }

      if( current_user_can('editor') ) {
        $args['capabilities']['create_posts'] = false;
      }

      // Give the custom-css-js post type it's arguments
      return $args;

    }


  }
}
然后用
新的WPSE64458_CPT_Register()继承

在这个示例中,我禁用了编辑器角色以添加新的成员帖子功能。修改任何你喜欢的,或者你也可以这样做其他人。但你之前提到你已经尝试过追随WooCommerce,实际上他们也是这样做的

希望这对你有意义


祝你快乐

您可以通过以下脚本将特定用户角色限制为自定义帖子:

添加自定义角色

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

     add_role('my_custom_role',
                'Custom Role',
                array(
                    'read' => true,
                    'edit_posts' => false,
                    'delete_posts' => false,
                    'publish_posts' => false,
                    'upload_files' => false,
                    'publish_posts' => false,
                    'create_posts' => false, 
                )
            );
       }
注册自定义帖子

add_action( 'init', 'my_custom_post_type');
function my_custom_post_type() {
     $args = array(
 'label'               => __( 'Custom post', 'custom-text-domain' ),
 'description'         => __( 'Custom post', 'custom-text-domain' ),
 'labels'              => $labels,
 'supports'            => array( 'title', 'comments', 'revisions', ),
 'hierarchical'        => false,
 'public'              => true,
 'show_ui'             => true,
 'rewrite'             => $rewrite,
                        'capability_type'     => array('custom_post','custom_post'),
                        'map_meta_cap'        => true, // Set to `false`, if users are not allowed to edit/delete existing posts 
 );
 register_post_type( 'custom_post', $args );
}
允许用户根据角色发布帖子

    add_action('admin_init','custom_post_add_role_caps',999);
        function custom_post_add_role_caps() {

     // Add the roles you'd like to administer the custom post types
     $roles = array('my_custom_role','editor');

     // Loop through each role and assign capabilities
     foreach($roles as $the_role) { 

          $role = get_role($the_role);

                  $role->add_cap( 'read' );
                  $role->add_cap( 'read_custom_post');
                  $role->add_cap( 'read_private_custom_post' );
                  $role->add_cap( 'edit_custom_post' );
                  $role->add_cap( 'edit_custom_post' );
                  $role->add_cap( 'edit_others_custom_post' );
                  $role->add_cap( 'edit_published_custom_post' );
                  $role->add_cap( 'publish_custom_post' );
                  $role->add_cap( 'delete_others_custom_post' );
                  $role->add_cap( 'delete_private_custom_post' );
                  $role->add_cap( 'delete_published_custom_post' );

     }
}
我希望这将帮助你,获得更多的帮助请访问


您是否尝试过将“创建帖子”设置为null或空字符串?是的,我尝试过,但结果相同。我相信您设置的位置不对。您应该为该功能指定一个唯一的名称,然后将其添加到各个角色的功能数组中,或者在定义自定义角色时将相应的功能设置为false。你能用更多的信息来更新你的问题吗,比如,你到底想完成什么?目前我在这里猜测的是,您试图隐藏CPT,向特定用户角色添加新功能?但您也给了该用户角色编辑和修改、重新编辑功能/权限?@Gaurav don-allow是正确的做法,而不是像您建议的那样设置false。我已经尝试了所有这些方法。谢谢你给我这么好的详细回答。但我已经这样做了。正如我在问题中提到的。我需要一个自定义角色。它将只具有读取功能。它与任何默认角色或自定义角色一起工作(如果它具有“编辑”和“读取”功能)。但是我不想给自定义角色提供编辑文章的功能。就是这样。其他一切都和你编码和解释的一模一样。希望这能澄清你的要求。很高兴听到你已经这样做了。无论如何,如果你真的不想给其他功能,然后只读功能,那么我真的很困惑,剩下什么来做这个控制?如果我没有给用户角色来编辑文章或编辑cpt,那么我在这里找不到任何问题?然而,快乐编码!:)这就是问题所在。我只是不知道这里缺少什么。我想也许其他人会发现。除非我授予编辑权限,否则它不会工作。如果我这样做,一切都会很好。我想给,我已经给了edit_cpt,但我不想给edit_Post,我将把这笔奖金奖励给你,因为你做出了诚实的尝试,你的回答是有效的。但既然它不能解决我的问题,我就不能接受它。干杯
    add_action('admin_init','custom_post_add_role_caps',999);
        function custom_post_add_role_caps() {

     // Add the roles you'd like to administer the custom post types
     $roles = array('my_custom_role','editor');

     // Loop through each role and assign capabilities
     foreach($roles as $the_role) { 

          $role = get_role($the_role);

                  $role->add_cap( 'read' );
                  $role->add_cap( 'read_custom_post');
                  $role->add_cap( 'read_private_custom_post' );
                  $role->add_cap( 'edit_custom_post' );
                  $role->add_cap( 'edit_custom_post' );
                  $role->add_cap( 'edit_others_custom_post' );
                  $role->add_cap( 'edit_published_custom_post' );
                  $role->add_cap( 'publish_custom_post' );
                  $role->add_cap( 'delete_others_custom_post' );
                  $role->add_cap( 'delete_private_custom_post' );
                  $role->add_cap( 'delete_published_custom_post' );

     }
}