Using WordPress ’embed_cache_oembed_types’ PHP filter

The embed_cache_oembed_types WordPress PHP filter allows you to modify the array of post types for which oEmbed results will be cached.

Usage

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

Parameters

  • $post_types (string[]): An array of post type names to cache oEmbed results for. Defaults to post types with show_ui set to true.

More information

See WordPress Developer Resources: embed_cache_oembed_types

Examples

Adding a custom post type to the list

Modify the array to include a custom post type named ‘product’ to cache oEmbed results for.

add_filter('embed_cache_oembed_types', 'add_product_post_type');
function add_product_post_type($post_types) {
  $post_types[] = 'product';
  return $post_types;
}

Removing a specific post type from the list

Remove the ‘page’ post type from the list of post types caching oEmbed results.

add_filter('embed_cache_oembed_types', 'remove_page_post_type');
function remove_page_post_type($post_types) {
  unset($post_types[array_search('page', $post_types)]);
  return $post_types;
}

Only cache oEmbed results for ‘post’ and ‘custom_post_type’

Override the default behavior and cache oEmbed results only for ‘post’ and a custom post type named ‘custom_post_type’.

add_filter('embed_cache_oembed_types', 'cache_only_post_and_custom_post_type');
function cache_only_post_and_custom_post_type($post_types) {
  return array('post', 'custom_post_type');
}

Disable oEmbed caching for all post types

Disable caching of oEmbed results for all post types by returning an empty array.

add_filter('embed_cache_oembed_types', 'disable_oembed_caching');
function disable_oembed_caching($post_types) {
  return array();
}

Cache oEmbed results only for post types with a specific capability

Cache oEmbed results only for post types that have the ‘edit_posts’ capability.

add_filter('embed_cache_oembed_types', 'cache_post_types_with_edit_posts_capability');
function cache_post_types_with_edit_posts_capability($post_types) {
  $allowed_post_types = array();
  foreach ($post_types as $post_type) {
    if (current_user_can('edit_posts', $post_type)) {
      $allowed_post_types[] = $post_type;
    }
  }
  return $allowed_post_types;
}