programing

특정 투고 유형에 대해 Gutenberg/블록 에디터를 비활성화하는 방법

newnotes 2023. 3. 16. 21:50
반응형

특정 투고 유형에 대해 Gutenberg/블록 에디터를 비활성화하는 방법

WordPress는 5번째 버전에서 Gutenberg / 블록 에디터를 추가했으며 Post 및 Page 투고 유형에 대해 기본적으로 활성화되어 있습니다.

가까운 장래에 모든 커스텀 투고 타입에 대해서 디폴트로 유효하게 되어 있을 가능성이 있기 때문에, WordPress 개발자로서 커스텀 투고 타입에 대해서 이 에디터를 무효로 하는 방법을 알고 싶습니다.플러그인이나 테마에서 등록한 투고 타입의 클래식 에디터를 유지하고 싶다.

WordPress 필터를 사용하여 편집기를 비활성화할 수 있습니다.

WordPress 5 이상

자신의 투고 타입에 대해서만 블록 에디터를 무효로 하려면 , 다음의 코드를 플러그 인에 추가할 수 있습니다.functions.php테마의 파일입니다.

add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2);
function prefix_disable_gutenberg($current_status, $post_type)
{
    // Use your post type key instead of 'product'
    if ($post_type === 'product') return false;
    return $current_status;
}

블록 편집기를 완전히 비활성화하려면(권장하지 않음) 다음 코드를 사용할 수 있습니다.

add_filter('use_block_editor_for_post_type', '__return_false');

Gutenberg 플러그인(WordPress 5 이전)

자신의 투고 타입에 대해서만 Gutenberg 에디터를 무효로 하는 경우는, 다음의 코드를 플러그 인에 추가할 수 있습니다.functions.php테마의 파일입니다.

add_filter('gutenberg_can_edit_post_type', 'prefix_disable_gutenberg', 10, 2);
function prefix_disable_gutenberg($current_status, $post_type)
{
    // Use your post type key instead of 'product'
    if ($post_type === 'product') return false;
    return $current_status;
}

구텐베르크 편집기를 완전히 비활성화하려면(권장하지 않음) 다음 코드를 사용할 수 있습니다.

add_filter('gutenberg_can_edit_post_type', '__return_false');

위의 다른 사용자도 가능하기 때문에 가능합니다.또, 이하를 알리고 싶습니다.

최신 Wordpress 또는 Wordpress 5+ - (Gutenberg 포함)두 가지 방법은 구텐베르크 제거에 동일한 효과가 있지만 제거할 때 다른 옵션이 있습니다.

(둘 다 함수에 삽입합니다.php 또는 커스텀 플러그인 함수)

투고 유형에서 Gutenberg를 삭제하려면:

add_filter('use_block_editor_for_post_type', 'prefix_disable_gutenberg', 10, 2);

 function prefix_disable_gutenberg($gutenberg_filter, $post_type)
  {
   if ($post_type === 'your_post_type') return false;
   return $gutenberg_filter;
  }

이 경우, 커스텀 투고 타입에서 구텐베르크 에디터가 완전히 삭제되지만, 다른 메타 박스나 기타 메타 박스도 그대로 사용할 수 있습니다.

단, 텍스트 에디터/텍스트 영역 자체를 삭제하는 경우 또는 기타 기본 옵션인 WordPress도 이를 Gutenberg로 간주하므로 다음 항목을 추가하여 이 항목을 삭제하고 동시에 Gutenberg를 삭제할 수 있습니다.

add_action('init', 'init_remove_editor',100);

 function init_remove_editor(){
  $post_type = 'your_post_type';
  remove_post_type_support( $post_type, 'editor');
 }

위는 구텐베르크와 워드프레스의 '편집자'를 무력화시킬 것이다.이것은 다른 메타박스/데이터 옵션으로 대체될 수 있습니다.(작성자/썸네일/개정판 등)

커스텀 투고 타입을 사용하는 경우는, 다른 방법이 있습니다.

cpt add를 등록할 때add_post_type_support( 'news', 'excerpt' );

완전한 예:

function create_news() {
    $args = [
        'labels' => [
            'name' => __( 'News', 'lang' ),
            'singular_name' => __( 'News', 'lang' ),
            'add_new_item'       => __( 'Add a news', 'lang' ),
    ],
        'public' => true,
        'has_archive' => true,
        'menu_icon' => 'dashicons-admin-post',
        'show_in_rest' => false,
        'rewrite' => ['slug' => 'news'],
        'show_in_nav_menus' => true,
    ];

    register_post_type( 'news',
        $args
    );
}
add_action( 'init', 'create_news' );
add_post_type_support( 'news', 'excerpt' );

, , 논증할 수 있습니다.'show_in_rest' => false,에는 클래식 .

register_post_type('test', [
    'label'  => null,
    'labels' => [
        'name'              => 'Test',
        'singular_name'     => 'Test',
        'add_new'           => 'Add test',
        'add_new_item'      => 'Add test',
        'edit_item'         => 'Edit test',
        'new_item'          => 'New test',
        'view_item'         => 'Watch test',
        'search_items'      => 'Search test',
        'not_found'         => 'Not found',
    ],
    'description'       => 'Post for Test',
    'public'            => true,
    'has_archive'       => false,
    'hierarchical'      => true,
    'menu_icon'         => 'dashicons-megaphone',
    'show_in_rest'      => false,
    'supports'          => ['title', 'thumbnail', 'editor'],
]);

이것처럼.

플러그인에 커스텀 투고 유형을 등록하기 때문에 블록에디터를 디세블로 하는 가장 빠른 방법은 에서 옵션을 false로 설정하는 것입니다.

<?php
$args = array(
  'label'        => 'Custom Posts',
  'show_ui'      => true,
  'show_in_rest' => false,          // ← Disables the block editor.
);
register_post_type( 'my-cpt-slug', $args );

커스텀 투고유형에서 에디터나 다른 필드를 삭제하려면 이 코드를 함수로 사용합니다.php 파일

add_action( 'init', function() {
    remove_post_type_support( 'custom post name', 'filed name' );
}, 99);

언급URL : https://stackoverflow.com/questions/52199629/how-to-disable-gutenberg-block-editor-for-certain-post-types

반응형