Using WordPress ‘disable_captions’ PHP filter

The disable_captions WordPress PHP filter allows you to disable or enable captions for images inserted into the editor.

Usage

add_filter('disable_captions', 'your_custom_function');
function your_custom_function($bool) {
  // your custom code here
  return $bool;
}

Parameters

  • $bool (bool): Determines whether to disable appending captions. Returning true from the filter will disable captions. Default is an empty string.

More information

See WordPress Developer Resources: disable_captions

Examples

Disable captions for all images

Disable captions for all images inserted into the editor.

add_filter('disable_captions', 'disable_all_captions');
function disable_all_captions($bool) {
  return true;
}

Enable captions for all images

Enable captions for all images inserted into the editor.

add_filter('disable_captions', 'enable_all_captions');
function enable_all_captions($bool) {
  return false;
}

Disable captions only for specific post types

Disable captions for images inserted into the editor, but only for specific post types.

add_filter('disable_captions', 'disable_captions_for_post_types', 10, 2);
function disable_captions_for_post_types($bool, $post) {
  $post_types = array('post_type_1', 'post_type_2');
  if (in_array($post->post_type, $post_types)) {
    return true;
  }
  return $bool;
}

Disable captions only for specific user roles

Disable captions for images inserted into the editor, but only for specific user roles.

add_filter('disable_captions', 'disable_captions_for_user_roles');
function disable_captions_for_user_roles($bool) {
  $user = wp_get_current_user();
  $user_roles = array('subscriber', 'contributor');
  if (array_intersect($user_roles, $user->roles)) {
    return true;
  }
  return $bool;
}

Disable captions based on a custom condition

Disable captions for images inserted into the editor based on a custom condition, such as a specific date or time.

add_filter('disable_captions', 'disable_captions_based_on_condition');
function disable_captions_based_on_condition($bool) {
  $disable_date = strtotime('2023-05-01');
  if (time() > $disable_date) {
    return true;
  }
  return $bool;
}