Skip to content

Instantly share code, notes, and snippets.

@samhotchkiss
Created October 29, 2014 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhotchkiss/28b5d8b24bd40c8b152f to your computer and use it in GitHub Desktop.
Save samhotchkiss/28b5d8b24bd40c8b152f to your computer and use it in GitHub Desktop.
Proposed Modifications to register_meta()
<?php
/**
* Register meta key
*
* @since 3.3.0
*
* @param string $meta_type Type of meta
* @param string $meta_key Meta key
* @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
* @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
* @param array $args Arguments
*/
function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null, $args = array() ) {
$defaults = array(
'label' => '',
'description' => '',
'meta_format' => '', //short_text, long_text, date, datetime, color, image, number, select
'meta_format_args' => array(),
'default' => '',
'public' => true
);
$protected = ( '_' == $meta_key[0] );
if( $protected )
$defaults[ 'public' ] = false;
if( $meta_type == 'post' )
$defaults[ 'post_type' ] = 'post';
if( $meta_type == 'term' )
$defaults[ 'taxonomy' ] = 'tag';
$args = wp_parse_args( $args, $defaults );
$args = (object) $args;
if ( is_callable( $sanitize_callback ) )
add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
if ( empty( $auth_callback ) ) {
if ( is_protected_meta( $meta_key, $meta_type ) )
$auth_callback = '__return_false';
else
$auth_callback = '__return_true';
}
if ( is_callable( $auth_callback ) )
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
//TODO: put the data about the meta into a global
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment