상현에 하루하루
개발자의 하루

특정 템플릿을 사용할때 구텐베르그 비활성화하기

( 업데이트: )

간단한 콘텐츠 페이지는 Gutenberg로 제작한다 하지만 복잡한 페이지 Gutenberg로는 한계가있다.

ACF나 커스텀 필드를 이용해서 워드프레스에서 텍스트, 이미지, 문단을 컨트롤하고 php로 템플릿을 구성해서 유연하게 페이지를 제작한다.

그러면 결국 해당 템플릿이 적용된 페이지는 Gutenberg 에디터가 활성화 되어있을 필요가 없다 거추장스럽고 불편하기만 할 뿐이다.

필터로 특정 템플릿 Gutenbreg 비활성화

<?php
/**
 * Templates and Page IDs without editor
 *
 */
function ea_disable_editor( $id = false ) {

	$excluded_templates = array(
		'templates/modules.php',
		'templates/contact.php'
	);

	$excluded_ids = array(
		// get_option( 'page_on_front' )
	);

	if( empty( $id ) )
		return false;

	$id = intval( $id );
	$template = get_page_template_slug( $id );

	return in_array( $id, $excluded_ids ) || in_array( $template, $excluded_templates );
}

/**
 * Disable Gutenberg by template
 *
 */
function ea_disable_gutenberg( $can_edit, $post_type ) {

	if( ! ( is_admin() && !empty( $_GET['post'] ) ) )
		return $can_edit;

	if( ea_disable_editor( $_GET['post'] ) )
		$can_edit = false;

	return $can_edit;

}
add_filter( 'gutenberg_can_edit_post_type', 'ea_disable_gutenberg', 10, 2 );
add_filter( 'use_block_editor_for_post_type', 'ea_disable_gutenberg', 10, 2 );

/**
 * Disable Classic Editor by template
 *
 */
function ea_disable_classic_editor() {

	$screen = get_current_screen();
	if( 'page' !== $screen->id || ! isset( $_GET['post']) )
		return;

	if( ea_disable_editor( $_GET['post'] ) ) {
		remove_post_type_support( 'page', 'editor' );
	}

}
add_action( 'admin_head', 'ea_disable_classic_editor' );
Code language: HTML, XML (xml)

$_GET['post'] URL에서 현재 페이지 ID를 가져와 해당 페이지에서 특정 템플릿을 사용하는 경우 Gutenberg 편집기를 비활성화. 또한 비슷한 기능을 사용하는 클래식 편집기또한 비활성화합니다.

functions.php에 적용한 disable-editor.php를 내 테마에 파일로 생성하고 비활성화할 목록을 $excluded_templates$excluded_ids 배열을 업데이트하여 적용한다.

이렇게 구성하면 Gutenberg와 클래식 편집기가 모두 비활성화되어 메타 박스를 편집할 수 있는 옵션들로만 환경으로 구성할 수 있습니다.


참고, 아이디어

감사합니다 🙏