自定义帖子类型-没有文档选项卡的布局部分,也没有作者选择

时间:2020-09-28 作者:Jim Worrall

我网站的一部分被另一个非营利组织使用。为了限制他们随意编辑/删除,我为他们创建了一个自定义帖子类型,并添加了一个用户角色,使用excellent model 由@WebElaine提交

它基本上是有效的:他们可以在后端进入自己的自定义帖子类型并创建/编辑页面,他们是作者,他们不能进入我的页面,基本上都很好。

一些奇怪的事情。

虽然他们是自己创建的页面的作者,但没有;“作者”;编辑器中文档选项卡的一部分,以防我想将页面交给他们,反之亦然。当我列出所有特殊类型的页面并进行快速编辑时,会有一个“作者”下拉列表,但它们不在其中,即使对于它们是作者的页面也是如此。

在编辑器的“文档”选项卡中,没有;“布局”;允许或不允许使用侧边栏的部分(对于它们;对于我)。可能是因为这是由主题(GeneratePress)控制的,而主题不知道这种帖子类型?

这是一个插件,用于自定义帖子类型和用户角色。

/* Plugin Name: fp custom post types
*/

/********************************************************/
/*   Function to create the custom post type \'ncpage\'   */
/********************************************************/

function fp_create_post_type() {
    // Capabilities of custom post type
    // First three are meta capabilities
    $capabilities = array(
        \'edit_post\'                 => \'edit_ncpage\', 
        \'publish_post\'              => \'publish_ncpage\', 
        \'delete_post\'               => \'delete_ncpage\', 
        \'publish_pages\'             => \'publish_ncpages\',
        \'edit_pages\'                => \'edit_ncpages\',
        \'edit_published_pages\'      => \'edit_published_ncpages\',
        \'edit_others_pages\'         => \'edit_others_ncpages\',
        \'edit_private_pages\'        => \'edit_private_ncpages\',
        \'delete_pages\'              => \'delete_ncpages\',
        \'delete_private_pages\'      => \'delete_private_ncpages\',
        \'delete_published_pages\'    => \'delete_published_ncpages\',
        \'delete_others_pages\'       => \'delete_others_ncpages\',
        \'read_private_pages\'        => \'read_private_ncpages\'
    );
    
    // Set backend UI labels for Custom Post Type
    $labels = array(
        \'name\'                => ( \'NCpages\' ),
        \'singular_name\'       => ( \'NCpage\' ),
        \'menu_name\'           => ( \'NCFPW Pages\' ),
        \'parent_item_colon\'   => ( \'Parent NCpage\' ),
        \'all_items\'           => ( \'All NCpages\' ),
        \'view_item\'           => ( \'View NCpage\' ),
        \'add_new_item\'        => ( \'Add New NCpage\' ),
        \'add_new\'             => ( \'Add New\' ),
        \'edit_item\'           => ( \'Edit NCpage\' ),
        \'update_item\'         => ( \'Update NCpage\' ),
        \'search_items\'        => ( \'Search NCpage\' ),
        \'not_found\'           => ( \'Not Found\' ),
        \'not_found_in_trash\'  => ( \'Not found in Trash\' )
    );

    // Add other CPT arguments
    $args = array(
        \'labels\'                => $labels,
        \'capabilities\'          => $capabilities,
        // Features this CPT supports in Post Editor
        \'supports\'              => array ( 
                  \'title\', \'editor\', \'comments\', \'revisions\', 
                  \'author\', \'page-attributes\', 
                  \'thumbnail\', \'custom-fields\', \'post-formats\' ),
        \'description\'           => \'NCFPW pages for corresponding user role\',
        \'map_meta_cap\'          => true,
        \'capability_type\'       => \'ncpage\',
        // Set rewrite to desired URL, otherwise
        // it defaults to true and uses $post-type as slug (ncpage)
        \'rewrite\'               => array(\'slug\' => \'ncfpw\'),
        // Other arguments
        // show_ui, show_in_nav_menus, and publicly queryable default to $public, 
        // show_in_menu defaults to $show_ui
        // show_in_admin_bar defaults to $show_in_menu
        \'public\'                => true,
        \'menu_position\'         => 4,
        \'hierarchical\'          => true,  // can have parent and child, like pages
        \'show_in_rest\'          => true,  // block editor support
        \'can_export\'            => true,
        \'exclude_from_search\'   => false
    );
    
    // Actually create the post type.  First is the post-type name ($post_type)
    register_post_type(\'ncpage\', $args);
}

/************************************/
/*        Call the function         */
/************************************/

add_action(\'init\', \'fp_create_post_type\');

/*********************************************************/
/* Add user role, access to ONLY ncpage and own images   */
/*********************************************************/
function fp_add_ncfpw_role() {
    add_role(\'ncfpw_author\', \'NCFPW Author\', array(
        // Custom capabilities
        \'publish_ncpages\'          => true,
        \'edit_ncpages\'             => true,
        \'edit_others_ncpages\'      => true,
        \'edit_private_ncpages\'     => true,
        \'edit_published_ncpages\'   => true,
        \'delete_ncpages\'           => true,
        \'delete_published_ncpages\' => true,
        \'delete_others_ncpages\'    => true,
        \'delete_private_ncpages\'   => true,
        \'read_private_ncpages\'     => true,

        // Allow to read and upload files
        \'read\' => true,  // access to Dashboard and user profile
        \'upload_files\' => true,  // see and add Media files
        \'delete_posts\' => true  // allows to delete media files they uploaded
    ));
}
// This makes it run only once at plugin activation
// __FILE__ is the full path to this php file.
register_activation_hook( __FILE__, \'fp_add_ncfpw_role\' );

/********************************************************/
/*            Admin access to ncpage                    */
/********************************************************/

function fp_add_admin_caps() {
    $role = get_role( \'administrator\' );
    $role -> add_cap( \'publish_ncpages\' );
    $role -> add_cap( \'edit_ncpages\' );
    $role -> add_cap( \'edit_others_ncpages\' );
    $role -> add_cap( \'edit_private_ncpages\' );
    $role -> add_cap( \'edit_published_ncpages\' );
    $role -> add_cap( \'delete_ncpages\' );
    $role -> add_cap( \'delete_published_ncpages\' );
    $role -> add_cap( \'delete_others_ncpages\' );
    $role -> add_cap( \'delete_private_ncpages\' );
    $role -> add_cap( \'read_private_ncpages\' );
}

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

1 个回复
SO网友:Jim Worrall

继TomJ Nowell提出的GeneratePress特性的伟大提示之后,对编辑器中缺少的布局控件进行了解答。GeneratePress的汤姆用过滤器帮我解决了这个问题。默认情况下,布局框仅针对具有“edit\\u theme\\u options”功能的用户显示。过滤器将其更改为自定义功能“edit\\ncpages”。

    // Allow Layout metabox to be generated in editor.  
    // Otherwise only users with \'edit-theme-options\' capability see it.
    add_filter( \'generate_metabox_capability\', function() {
        return \'edit_ncpages\';
    } );
或者它可能只是“读取”功能,所以任何进入编辑器的人都拥有它。

还没有解决另一个问题-没有人会在“作者”下拉列表中显示自定义角色,尽管他们可以编写和编辑自定义帖子类型。