The auth_{$object_type}_{$sub_type}_meta_{$meta_key} WordPress PHP filter allows you to control whether a user has permission to add post metadata to a specific post type and subtype.
Usage
add_filter( 'auth_posttype_subtype_meta_metakey', 'your_custom_function', 10, 6 );
function your_custom_function( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
// your custom code here
return $allowed;
}
Parameters
$allowed(bool): Whether the user can add the post meta. Default is false.$meta_key(string): The meta key.$post_id(int): Post ID.$user_id(int): User ID.$cap(string): Capability name.$caps(array): User capabilities.
More information
See WordPress Developer Resources: auth_{$object_type}_{$sub_type}meta{$meta_key}
Examples
Allow adding a specific meta key for a custom post type
In this example, we’ll allow users to add the ‘custom_meta_key’ for the ‘my_custom_post_type’ post type.
add_filter( 'auth_my_custom_post_type__meta_custom_meta_key', 'allow_custom_meta_key', 10, 6 );
function allow_custom_meta_key( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
return true;
}
Restrict adding a specific meta key based on user role
In this example, we’ll restrict adding the ‘restricted_meta_key’ to only users with the ‘editor’ role.
add_filter( 'auth_post__meta_restricted_meta_key', 'restrict_meta_key_to_editors', 10, 6 );
function restrict_meta_key_to_editors( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
$user = get_userdata( $user_id );
if ( in_array( 'editor', $user->roles ) ) {
return true;
}
return false;
}
Grant access to a specific meta key based on user ID
In this example, we’ll grant access to add the ‘user_specific_meta_key’ only to users with specific user IDs (1, 2, and 3).
add_filter( 'auth_post__meta_user_specific_meta_key', 'allow_meta_key_for_specific_users', 10, 6 );
function allow_meta_key_for_specific_users( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
$allowed_users = array( 1, 2, 3 );
if ( in_array( $user_id, $allowed_users ) ) {
return true;
}
return false;
}
Allow adding a specific meta key for a custom taxonomy term
In this example, we’ll allow users to add the ‘term_specific_meta_key’ to posts that have the ‘special_term’ in the ‘my_custom_taxonomy’ taxonomy.
add_filter( 'auth_post__meta_term_specific_meta_key', 'allow_meta_key_for_special_term', 10, 6 );
function allow_meta_key_for_special_term( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
if ( has_term( 'special_term', 'my_custom_taxonomy', $post_id ) ) {
return true;
}
return false;
}
Restrict adding a specific meta key based on post status
In this example, we’ll restrict adding the ‘status_based_meta_key’ to posts with the ‘draft’ status.
add_filter( 'auth_post__meta_status_based_meta_key', 'restrict_meta_key_based_on_status', 10, 6 );
function restrict_meta_key_based_on_status( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
$post_status = get_post_status( $post_id );
if ( $post_status == 'draft' ) {
return true;
}
return false;
}