問題描述

有什麼辦法可以停用在 WordPress(3.0) 中的自定義帖子型別下新增新帖子的選項嗎?我檢視了標籤和引數,但找不到類似於這樣的功能的任何東西。

最佳解決思路

完全信用 Seamus Leahy

There is a meta capability create_posts that is not documented but is used by WordPress to check before inserting the various ‘Add New’ buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.

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

請問為什麼要這樣做?

我會首先建議更改您的自定義帖子型別的功能,但我不認為有一個限制誰可以新增帖子,但只有誰可以編輯或釋出。

它看起來有點髒,但您可以嘗試取消設定 $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');

次佳解決思路

有一個元功能 create_posts 沒有記錄,但是在插入各種’Add New’ 按鈕和連結之前,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
));

第三種解決思路

上述解決方案的組合可以隱藏連結 (儘管有人可以很容易地直接輸入 URL 。

提到 @PavelChernov 的解決方案依賴於 get_post_type(),只有在列表中已經有一個帖子才能工作。如果沒有帖子,該功能將不會返回任何內容,並且”Add New” 連結將可用。另一種方法:

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

編輯:如果有人自己鍵入 URL,以防止直接訪問:https://wordpress.stackexchange.com/a/58292/6003

第四種思路

WordPress 網路:我發現如果您以網路的超級管理員身份登入,Seamus Leahy’s answer 不起作用,如果使用者沒有能力,對映或其他方式,當 current_user_can($ cap) 為由 CMS 呼叫。透過挖掘核心,我發現你可以做以下工作。

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 隱藏,但頁面仍然可以訪問。

第五種思路

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

第六種思路

在 wordpress 和所有的帖子型別有 create_posts 的能力。此功能用於幾個核心檔案:

  1. wp-admin edit-form-advanced.php

  2. wp-admin edit.php

  3. wp-admin 包括 post.php 中

  4. wp-admin menu.php

  5. wp-admin post-new.php

  6. wp-admin press-this.php

  7. wp-includes admin-bar.php

  8. wp-includes class-wp-xmlrpc,server.php

  9. wp-includes post.php 中

所以如果你真的要停用這個 feautere,你必須按角色和每個帖子型別進行。我使用偉大的外掛 「User Role Editor」 來管理每個角色的功能。

但是 create_posts 的能力呢?那麼這個能力沒有被對映,並且 create_posts 等於 create_posts,所以我們應該修復這個並且對映每個 post 型別的能力。

所以你可以在你的 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 或其他任何因為 init 優先順序為 100,阻止在管理欄,側邊欄等 (在所有 wp 介面中) 顯示”add new” 。

第七種思路

@ Staffan Estberg,

這是在自定義郵件列表中隱藏新增新建或建立新按鈕的最佳方法

'capability_type'    => 'post',

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

        'map_meta_cap'       => true,

它停用在管理選單中以及帖子型別列表之上的自定義帖子型別中建立新帖子。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。