Using WordPress ‘auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}’ PHP filter

The auth_{$object_type}meta{$meta_key}for{$object_subtype} WordPress PHP filter allows you to control whether a user can edit a specific meta key for a particular object type and subtype.

Usage

add_filter('auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}', 'your_custom_function', 10, 6);

function your_custom_function($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    // Your custom code here

    return $allowed;
}

Parameters

  • $allowed (bool): Whether the user can edit the object meta. Default is false.
  • $meta_key (string): The meta key.
  • $object_id (int): Object ID.
  • $user_id (int): User ID.
  • $cap (string): Capability name.
  • $caps (string[]): Array of the user’s capabilities.

More information

See WordPress Developer Resources: auth_{$object_type}meta{$meta_key}for{$object_subtype}

Examples

Allow editing of custom meta key for posts

Allow users with the edit_posts capability to edit a custom meta key my_custom_key for posts.

add_filter('auth_post_meta_my_custom_key_for_post', 'allow_edit_custom_meta_key', 10, 6);

function allow_edit_custom_meta_key($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    if (in_array('edit_posts', $caps)) {
        $allowed = true;
    }

    return $allowed;
}

Restrict editing of user meta key for administrators

Prevent users from editing the admin_notes meta key for users unless they have the manage_options capability.

add_filter('auth_user_meta_admin_notes_for_user', 'restrict_user_meta_key_edit', 10, 6);

function restrict_user_meta_key_edit($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    if (in_array('manage_options', $caps)) {
        $allowed = true;
    } else {
        $allowed = false;
    }

    return $allowed;
}

Allow editing of term meta key for specific taxonomy

Allow users with the manage_categories capability to edit the term_color meta key for categories.

add_filter('auth_term_meta_term_color_for_category', 'allow_edit_term_meta_key', 10, 6);

function allow_edit_term_meta_key($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    if (in_array('manage_categories', $caps)) {
        $allowed = true;
    }

    return $allowed;
}

Restrict editing of comment meta key based on user role

Prevent users with the subscriber role from editing the comment_rating meta key for comments.

add_filter('auth_comment_meta_comment_rating_for_comment', 'restrict_comment_meta_key_edit', 10, 6);

function restrict_comment_meta_key_edit($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    $user = get_userdata($user_id);

    if (in_array('subscriber', $user->roles)) {
        $allowed = false;
} else {
$allowed = true;
}

return $allowed;
}

Allow editing of post meta key based on custom user capability

Allow users with the `edit_advanced_meta` custom capability to edit the `advanced_options` meta key for posts.

add_filter('auth_post_meta_advanced_options_for_post', 'allow_edit_advanced_meta_key', 10, 6);

function allow_edit_advanced_meta_key($allowed, $meta_key, $object_id, $user_id, $cap, $caps) {
    if (in_array('edit_advanced_meta', $caps)) {
        $allowed = true;
    }
    
    return $allowed;
}