This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
embed_cache_oembed_types filter
Filters the array of post types to cache oEmbed results for.
To use embed_cache_oembed_types filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function modify_embed_cache_oembed_types_defaults which takes 1 parameter, and we registered it using add_filter. The first parameter embed_cache_oembed_types is the name of the hook, The second parameter modify_embed_cache_oembed_types_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometime, you have to remove a registered hook so you can use remove_filter to remove embed_cache_oembed_types filter.
Parameters
Below the 1 parameter is required to use this hook.
- $post_types: (string[]) Array of post type names to cache oEmbed results for. Defaults to post types with show_ui set to true.
Live Example
apply_filters( 'embed_cache_oembed_types', string[] $post_types )
Below is an example how you can use this hook.
function modify_embed_cache_oembed_types_defaults($post_types) { // Update the $post_types variable according to your website requirements and return this variable. You can modify the $post_types variable conditionally too if you want. return $post_types; } // add the filter add_filter( "embed_cache_oembed_types", "modify_embed_cache_oembed_types_defaults", 10, 1 );
Example 1: Custom Post Type Inclusion
In this example, we include a custom post type named ‘portfolio’ in the oEmbed cache.
function weplugins_modify_embed_cache_oembed_types_defaults($post_types) { $post_types[] = 'portfolio'; return $post_types; } add_filter( "embed_cache_oembed_types", "weplugins_modify_embed_cache_oembed_types_defaults", 10, 1 );
Example 2: Exclude Specific Post Types
This example demonstrates how to exclude certain post types like ‘page’ from the oEmbed cache.
function weplugins_exclude_embed_cache_oembed_types($post_types) { return array_diff($post_types, ['page']); } add_filter( "embed_cache_oembed_types", "weplugins_exclude_embed_cache_oembed_types", 10, 1 );
Example 3: Conditional Post Type Addition
Here, we conditionally add a post type ‘event’ to the oEmbed cache only if a certain condition is met (e.g., a specific plugin is active).
function weplugins_conditional_embed_cache_oembed_types($post_types) { if ( is_plugin_active( 'event-manager/event-manager.php' ) ) { $post_types[] = 'event'; } return $post_types; } add_filter( "embed_cache_oembed_types", "weplugins_conditional_embed_cache_oembed_types", 10, 1 );
To remove a hook callback, use the example below.
remove_filter( "embed_cache_oembed_types", "modify_embed_cache_oembed_types_defaults", 10, 1 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you need any customization or further assistance, feel free to contact us.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.