tools/post-meta-generator
WordPress Post Meta Generator
Register post meta keys with register_post_meta(): a declared type, a default, a sanitizer every writer goes through, an auth rule, and the REST schema the block editor needs to see the value.
snippet.php
/**
* Registers a post meta key on the 'post' post type.
*
* Registering a key is what gives it a declared type, a sanitizer that every
* writer goes through — including update_post_meta() calls from elsewhere in
* your code — and a rule for who may change it.
*
* A key only reaches the REST API if its post type supports 'custom-fields'.
* That is not in register_post_type()'s default supports list, so a custom post
* type usually has to add it before any of this is visible:
*
* add_post_type_support( 'post', 'custom-fields' );
*/
function wps_register_post_meta() {
register_post_meta(
'post',
'reading_time',
array(
'type' => 'integer',
'single' => true,
'description' => 'Estimated reading time in minutes.',
'default' => 0,
'show_in_rest' => true,
'sanitize_callback' => static function ( $value ) {
return (int) $value;
},
)
);
}
add_action( 'init', 'wps_register_post_meta' );