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

时间:2017-09-28 14:29:18

标签: php wordpress woocommerce

我遇到了将自定义帖子类型的自定义功能分配给自定义角色的问题。

问题是我要删除自定义帖子类型的添加新不使用CSS hack或取消设置菜单项)选项。我已经遇到过提出许多解决方案的答案,但没有一个完美无缺。

最接近我想要的是:

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
));

此代码删除了“添加新链接”,但它为功能分配了默认的帖子 slug。注意上述代码的第2行。如果我将其更改为我的自定义帖子类型slug,它将停止工作,我甚至无法进入帖子页面。

要记住的一件事是我正在使用自定义角色,默认情况下只有读取功能。

如果我还分配了 edit_posts 功能,那么我的目标就实现了,但用户也可以访问我不想要的帖子和评论。

Woocommerce正在这样做。我会深入研究woocommerce代码,并在注册产品和订单的地方添加此行。

'capabilities'        => array( 'create_posts' => 'do_not_allow' ),

一切都按照我的意愿行事。我整天都在探索woocommerce代码,但无法找到它是如何做到这一点的。有人可以用另一双眼睛帮我解决这个问题吗? :)

非常感谢。感谢..

2 个答案:

答案 0 :(得分:2)

我已经向你提过,给我们提供一些关于你想要做的计划的更多细节?

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

如果您想使用map_meta_cap => true,那么您需要更改capability_type以及您自己的类型,请参阅更多https://codex.wordpress.org/Function_Reference/register_post_type#capability_type

但是,如果您设置了自己的capability_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;

    }


  }
}

然后使用class

继承new WPSE64458_CPT_Register();

通过此示例,我禁用编辑器角色以添加新的成员发布功能。修改你喜欢的任何东西,或者你也可以做其他人。但你之前提到你已经尝试过跟随WooCommerce,实际上他们也是这样做的,

希望你有意义。

HappyCodding!

答案 1 :(得分:1)

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

添加自定义角色

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' );

     }
}

我希望这会对您有所帮助,请获取更多帮助,请访问

https://codex.wordpress.org/Function_Reference/register_post_type

https://codex.wordpress.org/Roles_and_Capabilities