#20: map_meta_cap

The map_meta_cap filter allows you to modify which primitive capabilities are mapped to which meta capabilities. That is to say, allow a specific user or users to have or not have a capability in a specific context.

It is evaluated in map_meta_cap() in the wp-includes/capabilities.php file.

map_meta_cap accepts 4 arguments:

  1. array $caps An array of primitive capabilities matched to the provided meta $cap
  2. string $cap The name of the meta capability to map to
  3. int $user_id The current user’s id or a specified one
  4. array $args An array of extra arguments for the meta capability. Sometimes empty

Example:

Let’s say you only want to allow super admins to upload files in your Multisite installation (for some reason). The following returns do_not_allow for any non-super admin on the upload_files cap.

View the code example on Gist.

#19: delete_{$meta_type}_metadata

The delete_{$meta_type}_metadata dynamic filter effectively allows you to prevent certain meta data from being deleted. The dynamic $meta_type variable accepts ‘user’, ‘comment’, or ‘post’.

It is evaluated in delete_metadata() in the wp-includes/meta.php file.

delete_{$meta_type}_metadata accepts 5 arguments:

  1. bool null If anything other than null is returned, delete_metadata() will short-circuit and return the boolean equivalent of that value. Default return for delete_metadata() is true on successful delete, false on failure
  2. int $object_id The user, comment or post object id
  3. string $meta_key The meta key to delete
  4. string $meta_value The meta value to delete
  5. bool $delete_all Whether to ignore $object_id further down in delete_metadata(). Default is false

Example:

Let’s say you want to keep non-admin users from deleting featured images once they’re set (for some reason). The following uses the dynamic delete_post_metadata hook accomplish this.

View the code example on Gist.

#18: pre_link_description

The pre_link_description filter allows you to do modify a link’s description before it is saved into the database.

It is evaluated in sanitize_bookmark_field() under the db context, which is used in wp_insert_link() in the wp-admin/includes/bookmark.php file.

You should run any string text you return via this filter through esc_attr() as it’s used in the link’s title attribute. See the note below about slashing link data.

pre_link_description accepts a single argument in the form of a string containing the link description/title attribute.

Side note: Link data coming out of the database via wp_insert_link() is unslashed via wp_unslash() in 3.6+ (stripslashes_deep() in older versions) but the data going in will automatically be slashed via mysql_real_escape_string() in the $wpdb->insert|prepare methods (thanks Rarst for the assist!). In other words, this is a case where Core handles slashing the data for you.

Example:

Let’s say you run a site that still actively uses the Link Manager to build a blog roll or links list, and let’s say you want to subtly indicate when a link was last edited. The following replaces the link description with the last-edited date and time which is then saved in the database.

View the code example on Gist.