The oembed_ttl WordPress PHP filter modifies the oEmbed Time to Live (TTL) value, which determines how long the oEmbed data will be cached in seconds.
Usage
add_filter('oembed_ttl', 'your_custom_function', 10, 4); function your_custom_function($time, $url, $attr, $post_id) { // Your custom code here return $time; }
Parameters
$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.
More information
See WordPress Developer Resources: oembed_ttl
Examples
Increase oEmbed TTL to 1 day
This example increases the oEmbed TTL to 1 day (86,400 seconds) for all posts.
add_filter('oembed_ttl', 'increase_oembed_ttl', 10, 1); function increase_oembed_ttl($time) { $time = 86400; // 24 hours * 60 minutes * 60 seconds return $time; }
Decrease oEmbed TTL to 1 hour for a specific post
This example decreases the oEmbed TTL to 1 hour (3,600 seconds) for a specific post with the ID 123.
add_filter('oembed_ttl', 'decrease_oembed_ttl_for_specific_post', 10, 4); function decrease_oembed_ttl_for_specific_post($time, $url, $attr, $post_id) { if ($post_id === 123) { $time = 3600; // 1 hour * 60 minutes * 60 seconds } return $time; }
Set oEmbed TTL based on post category
This example sets the oEmbed TTL based on the post’s category: 1 day for “news” and 1 hour for “events.”
add_filter('oembed_ttl', 'set_oembed_ttl_by_category', 10, 4); function set_oembed_ttl_by_category($time, $url, $attr, $post_id) { if (in_category('news', $post_id)) { $time = 86400; // 24 hours * 60 minutes * 60 seconds } elseif (in_category('events', $post_id)) { $time = 3600; // 1 hour * 60 minutes * 60 seconds } return $time; }
Set oEmbed TTL to never expire
This example sets the oEmbed TTL to never expire by setting the value to 0.
add_filter('oembed_ttl', 'never_expire_oembed', 10, 1); function never_expire_oembed($time) { $time = 0; // Never expire return $time; }
Set oEmbed TTL to 30 minutes for YouTube embeds
This example sets the oEmbed TTL to 30 minutes (1,800 seconds) for YouTube embeds.
add_filter('oembed_ttl', 'set_youtube_oembed_ttl', 10, 4); function set_youtube_oembed_ttl($time, $url, $attr, $post_id) { if (strpos($url, 'youtube.com') !== false) { $time = 1800; // 30 minutes * 60 seconds } return $time; }