問題描述
有什麼辦法可以禁用在 WordPress(3.0) 中的自定義帖子類型下添加新帖子的選項嗎?我查看了標籤和參數,但找不到類似於這樣的功能的任何東西。
最佳解決思路
完全信用 Seamus Leahy
There is a meta capability
create_poststhat 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, addcapabilities(not to be confused withcap) and then set it tofalseas 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 的能力。此功能用於幾個核心文件:
-
wp-admin edit-form-advanced.php
-
wp-admin edit.php
-
wp-admin 包括 post.php 中
-
wp-admin menu.php
-
wp-admin post-new.php
-
wp-admin press-this.php
-
wp-includes admin-bar.php
-
wp-includes class-wp-xmlrpc,server.php
-
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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。