prosource

WordPress: 커스텀 투고 타입에서 "신규 추가"를 무효화

probook 2023. 4. 3. 21:36
반응형

WordPress: 커스텀 투고 타입에서 "신규 추가"를 무효화

WordPress(3.0)의 Custom Post Type(사용자 지정 게시 유형)에서 새 게시물을 추가하는 옵션을 비활성화하는 방법이 있습니까?라벨이나 논거를 조사해 봤지만, 그러한 특징과 비슷한 것은 찾을 수 없었습니다.

메타 기능이 있습니다.create_postsWordPress에서 다양한 '신규 추가' 버튼과 링크를 삽입하기 전에 확인하는 데 사용됩니다.커스텀 투고 타입 선언에,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
));

아마 셋팅을 하고 싶을 겁니다.map_meta_cap로.true뿐만 아니라.이 기능이 없으면 게시물의 편집 페이지에 더 이상 액세스할 수 없습니다.

위의 솔루션 조합은 링크를 숨기는 데 도움이 됩니다(다른 사용자가 URL을 직접 입력할 수도 있지만).

@3pe3에서 언급한 솔루션은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');

편집: 직접 URL을 입력하는 경우 직접 액세스를 방지하려면 https://wordpress.stackexchange.com/a/58292/6003를 방문하십시오.

WordPress 네트워크:네트워크의 슈퍼 관리자로 로그인하면 Seamus Leahy의 답변이 작동하지 않는다는 을 알게 되었습니다.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
));

승인된 답변은 메뉴 항목을 숨기지만 페이지에 액세스할 수 있습니다.

wordpress 및 모든 포스트 유형에 create_posts 기능이 있습니다.이 기능은 여러 코어 파일에서 사용됩니다.

  1. wp-admin\edit-form-advanced.php
  2. wp-admin\edit.php
  3. wp-admin\post.php
  4. wp-admin\메뉴.php
  5. wp-admin\post-new.php
  6. wp-admin\이것을 누릅니다.php
  7. wp-displays\admin-bar.php
  8. wp-class\class-wp-xmlrpc-server.php
  9. wp-post\post.php

따라서 이 기능을 비활성화하려면 역할별 및 게시 유형별로 수행해야 합니다.역할별 기능을 관리하기 위해 위대한 플러그인 "사용자 역할 편집기"를 사용합니다.

create_posts 기능은 어떻습니까?이 기능은 매핑되지 않으며 create_posts는 create_posts와 같기 때문에 이를 수정하고 포스트 유형별로 기능을 매핑해야 합니다.

이 코드를 함수에 추가할 수 있습니다.이 기능은 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 요구 포함)의 기능을 삭제합니다.

priority 100의 init에서는 (모든 wp 인터페이스에서) admin 바, 사이드바 등에 "add new"가 표시되지 않기 때문에 액션은 init 이며 admin_init 등의 액션이 아닙니다.

등록된 포스트 유형에 대해 새 게시물 생성을 사용하지 않도록 설정합니다(예:post ★★★★★★★★★★★★★★★★★」page)

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

@ Staffan Estberg,

이 방법은 사용자 지정 포스트유형에서 새로 추가 또는 새로 만들기 단추를 숨기는 가장 좋은 방법입니다.

'capability_type'    => 'post',

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

        'map_meta_cap'       => true,

관리 메뉴의 측면과 게시물 유형 목록 위 모두에서 사용자 지정 게시물 유형에 새 게시물을 생성할 수 없습니다.

는는이이이이이한한방방방방방방방방방 。만 하면 됩니다.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');

문제는 커스텀 투고 타입의 새로운 버튼을 무효로 하는 방법'이 아니라, 「커스텀 투고 타입의 편집을 제한하는 방법」이기 때문에, 이것을 함수에 추가하는 것으로, CSS로 버튼을 숨기는 것이 답이라고 생각합니다.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();
});

언급URL : https://stackoverflow.com/questions/3235257/wordpress-disable-add-new-on-custom-post-type

반응형