WordPress:在自定义帖子类型上禁用“添加新”

时间:2010-07-13 08:12:27

标签: wordpress

有没有办法禁用在WordPress(3.0)的自定义帖子类型下添加新帖子的选项?我查看了标签和参数,但找不到任何类似这样的功能。

11 个答案:

答案 0 :(得分:105)

有一个元功能create_posts没有记录,但在插入各种“添加新”按钮和链接之前,WordPress会使用它来检查。在自定义帖子类型声明中,添加capabilities(不要与cap混淆),然后将其设置为false,如下所示。

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));

答案 1 :(得分:64)

完全归功于Seamus Leahy

  

有一个元功能create_posts没有记录,但在插入各种“添加新”按钮和链接之前,WordPress会使用它来检查。在自定义帖子类型声明中,添加capabilities(不要与cap混淆),然后将其设置为false,如下所示。

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

请问您为什么要这样做?

我首先建议更改自定义帖子类型的功能,但我认为没有一个限制谁可以添加帖子,但只有谁可以编辑或< em>发布他们。

看起来有点脏,但您可以尝试在$submenu全局中取消设置项目;

<击>

<击>
function hide_add_new_custom_type()
{
    global $submenu;
    // replace my_type with the name of your post type
    unset($submenu['edit.php?post_type=my_type'][10]);
}
add_action('admin_menu', 'hide_add_new_custom_type');

<击>

答案 2 :(得分:10)

上述解决方案的组合可以隐藏链接(尽管有人可以很容易地直接输入网址。

@PavelChernov提到的解决方案依赖于get_post_type(),只有在列表中已有帖子时才会有效。如果没有帖子,该功能将不会返回任何内容,并且“添加新”链接将可用。另一种方法:

function disable_new_posts() {
    // Hide sidebar link
    global $submenu;
    unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);

    // Hide link on listing page
    if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
        echo '<style type="text/css">
        #favorite-actions, .add-new-h2, .tablenav { display:none; }
        </style>';
    }
}
add_action('admin_menu', 'disable_new_posts');

编辑:如果有人在其中键入网址,则阻止直接访问:https://wordpress.stackexchange.com/a/58292/6003

答案 3 :(得分:7)

在wordpress和所有帖子类型中都有功能create_posts。此功能用于多个核心文件:

  1. 可湿性粉剂管理员\编辑外形advanced.php
  2. 可湿性粉剂管理员\ edit.php
  3. 可湿性粉剂管理员\包括\ post.php中
  4. 可湿性粉剂管理员\ menu.php
  5. 可湿性粉剂管理员\后new.php
  6. 可湿性粉剂管理员\压this.php
  7. WP-包括\管理员-bar.php
  8. WP-包括\类-WP-XMLRPC-server.php
  9. WP-包括\ post.php中
  10. 因此,如果您真的想禁用此feautere,则必须按角色和每个帖子类型执行此操作。 我使用了很棒的插件&#34; User Role Editor&#34;管理每个角色的能力。

    但是功能create_posts怎么样?那么这个功能没有被映射,并且create_posts也等于create_posts所以我们应该修复它并映射每个帖子类型的能力。

    所以你可以在你的functions.php中添加这段代码,你可以管理这个功能。

    function fix_capability_create(){
        $post_types = get_post_types( array(),'objects' );
        foreach ( $post_types as $post_type ) {
            $cap = "create_".$post_type->name;
            $post_type->cap->create_posts = $cap;
            map_meta_cap( $cap, 1); 
        }
    }
    add_action( 'init', 'fix_capability_create',100);
    

    所以这里我们没有隐藏或删除菜单元素......这里我们正在删除用户的功能(包括xmlrpc请求)。

    操作是init而不是admin_init或其他任何操作,因为优先级为100的init会阻止显示&#34;添加新的&#34;在管理栏,侧栏等(在所有wp界面中)。

答案 4 :(得分:5)

add_action("load-post-new.php", 'block_post');

function block_post()
{
    if($_GET["post_type"] == "custom_type") 
        wp_redirect("edit.php?post_type=custom_type");
}

答案 5 :(得分:4)

WordPress网络:如果您以网络的超级管理员身份登录,我发现Seamus Leahy's answer无法正常工作,如果用户不重要当CMS调用current_user_can($ cap)时,没有能力,映射或其他方式。通过挖掘核心,我发现你可以做到以下几点。

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
  ),
  'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));

accepted answer隐藏了菜单项,但页面仍可访问。

答案 6 :(得分:3)

@ Staffan Estberg,

这是隐藏自定义postypes中的Add New或Create New按钮的最佳方法

'capability_type'    => 'post',

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

        'map_meta_cap'       => true,

禁止在管理菜单中和帖子类型列表上方的自定义帖子类型中创建新帖子。

答案 7 :(得分:3)

禁止为已注册的帖子类型创建新帖子:postpage的示例)

function disable_create_newpost() {
    global $wp_post_types;
    $wp_post_types['post']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['page']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');

答案 8 :(得分:1)

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => false, // Removes support for the "Add New" function 
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));

我希望它将对您有所帮助。非常感谢!

答案 9 :(得分:1)

由于问题是“如何在自定义帖子类型上禁用添加按钮”,而不是“如何限制用户编辑自定义帖子类型”,我认为答案应该是通过添加纯隐藏CSS按钮这到functions.php文件:

add_action( 'admin_head', function(){
    ob_start(); ?>
    <style>
        #wp-admin-bar-new-content{
            display: none;
        }
        a.page-title-action{
            display: none !important;
        }
        #menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
            display:none;
        }
    </style>
<?php ob_end_flush();
});

答案 10 :(得分:0)

我找到了这种最简单的方法。只需将此代码添加到主题function.php

function hd_add_buttons() {
    global $pagenow;
    if (is_admin()) {
        if ($_GET['post_type'] == 'custom_post_type_name') {
            echo '<style>.add-new-h2{display: none !important;}</style>';
        }
    }
}
add_action('admin_head', 'hd_add_buttons');