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.
oembed_ttl filter
Filters the oEmbed TTL value (time to live).
To use the oembed_ttl filter, first you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.
At WePlugins, we 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_oembed_ttl_defaults which takes 4 parameters and we registered using add_filter. The first parameter oembed_ttl is the name of the hook, the second parameter modify_oembed_ttl_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.
Sometimes, you have to remove a registered hook so you can use remove_filter to remove the oembed_ttl filter.
Parameters
Below are the 4 parameters required to use this hook.
- $time : (int) Time to live (in seconds).
- $url : (string) The attempted embed URL.
- $attr : (array) An array of shortcode attributes.
- $post_ID : (int) Post ID.
Live Example
Below is an example of how you can use this hook.
function weplugins_modify_oembed_ttl_defaults($time, $url, $attr, $post_ID) { // Update the $time variable according to your website requirements and return this variable. You can modify the $time variable conditionally too if you want. return $time; } // add the filter add_filter( "oembed_ttl", "weplugins_modify_oembed_ttl_defaults", 10, 4 );
Example 1: Basic Usage
This example shows the basic usage of the oembed_ttl filter to set a custom TTL value.
function weplugins_custom_oembed_ttl($time, $url, $attr, $post_ID) { // Set TTL to 3600 seconds (1 hour) return 3600; } add_filter('oembed_ttl', 'weplugins_custom_oembed_ttl', 10, 4);
Example 2: Conditional TTL
In this example, we set different TTL values based on the post type.
function weplugins_conditional_oembed_ttl($time, $url, $attr, $post_ID) { // Check if the post type is 'video' if (get_post_type($post_ID) == 'video') { return 7200; // 2 hours } return $time; } add_filter('oembed_ttl', 'weplugins_conditional_oembed_ttl', 10, 4);
Example 3: Removing the Hook
To remove a hook callback, use the example below.
remove_filter('oembed_ttl', 'weplugins_modify_oembed_ttl_defaults', 10, 4);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.
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.