函数文档

remove_post_type_support()

💡 云策文档标注

概述

remove_post_type_support() 函数用于从指定文章类型中移除特定功能支持,如编辑器、缩略图等。它通常通过 'init' 钩子调用,以修改文章类型的编辑界面行为。

关键要点

  • 函数接受两个必需参数:$post_type(文章类型)和 $feature(要移除的功能)。
  • 功能($feature)包括 'title'、'editor'、'author'、'thumbnail'、'excerpt'、'trackbacks'、'custom-fields'、'comments'、'revisions'、'page-attributes' 和 'post-formats'。
  • 移除功能会影响编辑屏幕的相关区域,例如 'comments' 功能控制评论计数显示,'revisions' 控制是否存储修订。
  • 建议在 'init' 动作钩子中调用此函数,以确保在 WordPress 初始化时生效。

代码示例

// 移除页面编辑器的示例
add_action( 'init', 'remove_editor_init' );
function remove_editor_init() {
    if ( ! is_admin() ) {
        return;
    }
    $current_post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
    $update_post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
    if ( isset( $current_post_id ) ) {
        $post_id = absint( $current_post_id );
    } else if ( isset( $update_post_id ) ) {
        $post_id = absint( $update_post_id );
    } else {
        return;
    }
    if ( isset( $post_id ) ) {
        $template_file = get_post_meta( $post_id, '_wp_page_template', true );
        if ( 'page-your-template.php' === $template_file ) {
            remove_post_type_support( 'page', 'editor' );
        }
    }
}

注意事项

  • 移除 'thumbnail' 功能需要当前主题支持 Post Thumbnails。
  • 移除 'page-attributes' 功能要求文章类型是层级式的(hierarchical 必须为 true)。
  • 函数通过全局变量 $_wp_post_type_features 操作,直接取消设置指定功能。

📄 原文内容

Removes support for a feature from a post type.

Parameters

$post_typestringrequired
The post type for which to remove the feature.
$featurestringrequired
The feature being removed.

More Information

All features are directly associated with a functional area of the edit screen, such as the editor or a meta box. Additionally, the ‘revisions’ feature dictates whether the post type will store revisions, and the ‘comments’ feature dictates whether the comments count will show on the edit screen.

Typically remove_post_type_support() should be attached to the ‘init’ action hook.

Possible values of parameter $feature

  • ‘title’
  • ‘editor’ (content)
  • ‘author’
  • ‘thumbnail’ (featured image) (current theme must also support Post Thumbnails)
  • ‘excerpt’
  • ‘trackbacks’
  • ‘custom-fields’
  • ‘comments’ (also will see comment count balloon on edit screen)
  • ‘revisions’ (will store revisions)
  • ‘page-attributes’ (template and menu order) (hierarchical must be true)
  • ‘post-formats’ removes post formats, see Post Formats

Source

function remove_post_type_support( $post_type, $feature ) {
	global $_wp_post_type_features;

	unset( $_wp_post_type_features[ $post_type ][ $feature ] );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Hide page visual editor if certain template is selected:

    add_action( 'init', 'remove_editor_init' );
    
    function remove_editor_init() {
        // If not in the admin, return.
        if ( ! is_admin() ) {
           return;
        }
    
        // Get the post ID on edit post with filter_input super global inspection.
        $current_post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
        // Get the post ID on update post with filter_input super global inspection.
        $update_post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
    
        // Check to see if the post ID is set, else return.
        if ( isset( $current_post_id ) ) {
           $post_id = absint( $current_post_id );
        } else if ( isset( $update_post_id ) ) {
           $post_id = absint( $update_post_id );
        } else {
           return;
        }
    
        // Don't do anything unless there is a post_id.
        if ( isset( $post_id ) ) {
           // Get the template of the current post.
           $template_file = get_post_meta( $post_id, '_wp_page_template', true );
    
           // Example of removing page editor for page-your-template.php template.
           if (  'page-your-template.php' === $template_file ) {
               remove_post_type_support( 'page', 'editor' );
               // Other features can also be removed in addition to the editor. See: <a href="https://codex.wordpress.org/Function_Reference/remove_post_type_support" rel="nofollow ugc">https://codex.wordpress.org/Function_Reference/remove_post_type_support</a>.
           }
        }
    }

    Credit: https://wordpress.stackexchange.com/a/91644/138483

  2. Skip to note 7 content

    Remove support for post formats
    This example removes support for post formats in posts:

    /**
     * Remove post-formats support from posts.
     */
    function wpdocs_remove_post_type_support() {
    	remove_post_type_support( 'post', 'post-formats' );
    }
    add_action( 'init', 'wpdocs_remove_post_type_support', 10 );