删除“添加新功能”,同时保留对自定义角色类型的编辑功能

时间:2019-05-21 14:49:22

标签: wordpress

我试图限制来自低于编辑器的任何人的自定义类型类型(“居民”)的“添加新”功能。但是,我希望我的自定义角色(也称为“居民”)能够编辑现有(和将来)的CPT

我不接受这个建议:https://stackoverflow.com/a/3248103/3239470确实确实有效,但是它为所有人(包括我仍然需要的管理员/编辑)消除了“添加新内容”的需求。

我尝试为管理员/编辑者重新添加create_posts / create_residents功能,但它似乎并没有采用,也不会覆盖已注册的CPT中设置的功能。

我也正在多站点设置中执行此操作,这对您有所帮助。

我不希望通过CSS删除它,但是实际上应该禁用直接链接,如果有人键入URL,但是我开始认为那是我唯一的选择?任何方向或建议,不胜感激。

            /* Register 'Resident' role */
            function add_resident_role() {
             add_role('resident',
                        'Resident',
                        array(
                            'read' => true,
                            'edit_posts' => false,
                            'delete_posts' => false,
                            'publish_posts' => false,
                            'upload_files' => false,
                        )
                    );
               }
               register_activation_hook( __FILE__, 'add_resident_role' );


    function residents_post_type() {

       // Labels
        $labels = array(
            'name' => _x('Residents', 'post type general name'),
            'singular_name' => _x('Resident', 'post type singular name'),
            'menu_name' => 'Residents',
            'add_new' => _x('Add New', 'residents item'),
            'add_new_item' => __('Add New Resident'),
            'edit_item' => __('Edit Resident'),
            'new_item' => __('New Resident'),
            'view_item' => __('View Resident'),
            'search_items' => __('Search Residents'),
            'not_found' =>  __('No Residents Found'),
            'not_found_in_trash' => __('No Residents Found in Trash'),
            'parent_item_colon' => ''
        );

    // Register post type
    register_post_type('residents' , array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_in_nav_menus' => true,
        'menu_icon'   => 'dashicons-id-alt',
        'supports' => array('title', 'thumbnail', 'author'),
        'show_in_rest'  => true,
        'exclude_from_search' => true,
        'publicly_queryable' => false,
        'capability_type'     => array('resident','residents'),
        'map_meta_cap' => true,

    ) );
    }
    add_action( 'init', 'residents_post_type', 0 );

    /* Assign Capabilities back to editor and admin parties */

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

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

        // 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_residents');
                 $role->add_cap( 'read_private_residents' );
                 $role->add_cap( 'edit_resident' );
                 $role->add_cap( 'edit_residents' );
                 $role->add_cap( 'edit_others_residents' );
                 $role->add_cap( 'edit_published_residents' );
                 $role->add_cap( 'publish_residents' );
                 $role->add_cap( 'delete_others_residents' );
                 $role->add_cap( 'delete_private_residents' );
                 $role->add_cap( 'delete_published_residents' );        
        }
    }


    /* Assign Capabilities to resident */

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

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

        // 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_residents');
                 $role->add_cap( 'read_resident' );
                 $role->add_cap( 'edit_resident' );
                 $role->add_cap( 'edit_residents' );
                 $role->add_cap( 'publish_residents' );
                 $role->remove_cap( 'create_residents' );
        }
    }

0 个答案:

没有答案