Skip to content
wp-skills
Register
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.

The post type slug these keys belong to.

Passes an empty string to register_post_meta(), which WordPress reads as every post type — including ones added later by other plugins.

The function that runs on init. Naming it after register_post_meta() would redeclare a core function, so that one name is renamed for you.

Needs the post type to support 'custom-fields', which register_post_type() does not add by default. Without it the key registers cleanly and never appears in a response.

WordPress 6.4 and newer, and only for a post type that supports 'revisions'. Accepted and ignored otherwise.

The default leaves auth_callback off, so WordPress uses the post type's own edit capability. A key whose name starts with an underscore is protected and denies every REST write unless you set one.

Meta keys1
  • #1

    What get_post_meta() reads. A leading underscore makes it protected: hidden from the Custom Fields box, and read-only over REST without an auth_callback.

    A JSON schema type, not a PHP one. REST writes are validated against it.

    Off stores a list under one key. The REST schema and the default both change shape to match.

    Returned by get_post_meta() before anything is stored. Needs WordPress 5.5 or newer.

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' );