Skip to content

Instantly share code, notes, and snippets.

@mattheu
Last active August 29, 2015 14:08
Show Gist options
  • Save mattheu/c4ec739e53b107d59f0c to your computer and use it in GitHub Desktop.
Save mattheu/c4ec739e53b107d59f0c to your computer and use it in GitHub Desktop.
Idea for new register meta functionality in core.
<?php
$_wp_registered_meta = array();
/**
* Register meta key
*
* Supported arguments.
* label, field_type, sanitize_callback, auth_callback
*
* @since 3.3.0
*
* @param string $meta_type Type of meta
* @param string $meta_key Meta key
* @param array $args Arguments
*/
function register_meta( $meta_type, $meta_key, $args = array() ) {
global $_wp_registered_meta;
$args = wp_parse_args( $args, array(
'label' => $meta_key,
'field_type' => 'text',
) );
$_wp_registered_meta[$meta_type][$meta_key] = $args;
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) )
add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
if ( empty( $args['auth_callback'] ) ) {
if ( is_protected_meta( $meta_key, $meta_type ) )
$args['auth_callback'] = '__return_false';
else
$args['auth_callback'] = '__return_true';
}
if ( is_callable( $args['auth_callback'] ) ) {
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
}
}
/**
* Get all registered meta.
*
* @return array
*/
function get_registered_meta( $type = null ) {
global $_wp_registered_meta;
if ( ! $_wp_registered_meta ) {
return array();
}
if ( $type ) {
if ( isset( $_wp_registered_meta[ $type ] ) ) {
return $_wp_registered_meta[ $type ];
} else {
return array();
}
}
return $_wp_registered_meta;
}
@mattheu
Copy link
Author

mattheu commented Oct 28, 2014

  • I have changed the args required by the register_meta function - although we could work with the old args for backwards compat if neccesary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment