钩子文档

register_{$post_type}_post_type_args

💡 云策文档标注

概述

register_{$post_type}_post_type_args 是一个 WordPress 过滤器,用于在注册特定文章类型时修改其参数。它允许开发者在文章类型注册过程中动态调整设置,如标签、权限或 REST API 控制器类。

关键要点

  • 这是一个动态过滤器,名称中的 $post_type 占位符对应文章类型键,例如 register_post_post_type_args 用于文章类型 'post'。
  • 参数 $args 是一个数组,包含注册文章类型时传递给 register_post_type() 的参数,可基于文档进行修改。
  • 从 WordPress 6.4.0 起,新增了 late_route_registration、autosave_rest_controller_class 和 revisions_rest_controller_class 参数,增强了 REST API 集成功能。

代码示例

add_filter( 'register_post_post_type_args', 'modify_post_type_args', 10, 2 );
function modify_post_type_args( $args, $post_type ) {
    if ( 'post' === $post_type ) {
        $args['labels']['name'] = '自定义文章';
        $args['supports'][] = 'custom-fields';
    }
    return $args;
}

注意事项

  • 使用此过滤器时,需确保 $post_type 参数正确匹配目标文章类型键,以避免影响其他文章类型。
  • 修改参数应参考 register_post_type() 函数的文档,确保传递有效的参数值,如数组结构或布尔值。
  • 在 WordPress 6.0.0 中引入,后续版本如 6.4.0 添加了新参数,开发时需注意版本兼容性。

📄 原文内容

Filters the arguments for registering a specific post type.

Description

The dynamic portion of the filter name, $post_type, refers to the post type key.

Possible hook names include:

  • register_post_post_type_args
  • register_page_post_type_args

Parameters

$argsarray
Array of arguments for registering a post type.
See the register_post_type() function for accepted arguments.

More Arguments from register_post_type( … $args )

Post type registration arguments.

$post_typestring
Post type key.

Source

$args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name );

Changelog

Version Description
6.4.0 Added late_route_registration, autosave_rest_controller_class and revisions_rest_controller_class arguments.
6.0.0 Introduced.