“pre_transient_{$transient}” is a dynamic WordPress PHP filter that allows you to control the value of an existing transient before it is retrieved.
Usage
function my_custom_transient_filter( $pre_transient, $transient ) { // Your custom code here return $pre_transient; } add_filter( 'pre_transient_example_transient', 'my_custom_transient_filter', 10, 2 );
Parameters
- $pre_transient (mixed)
- The default value to return if the transient does not exist.
- Any value other than false will short-circuit the retrieval of the transient, and return that value.
- $transient (string)
- Transient name.
Examples
Cache an API response
Scenario: Cache an API response for a specific period to reduce server load.
function cache_api_response( $pre_transient, $transient ) { $api_response = file_get_contents( 'https://api.example.com/data' ); set_transient( 'example_transient', $api_response, 3600 ); return $api_response; } add_filter( 'pre_transient_example_transient', 'cache_api_response', 10, 2 );
In this example, the cache_api_response()
function fetches data from an external API and caches the response using the set_transient()
function for 1 hour (3600 seconds). If the transient value exists, it will be returned. If not, the API data will be fetched, cached, and then returned.
Display a custom ‘maintenance mode’ message
Scenario: Show a custom message for users when the website is in maintenance mode.
function maintenance_mode_message( $pre_transient, $transient ) { return '<h2>Our website is currently under maintenance. Please check back later!</h2>'; } add_filter( 'pre_transient_maintenance_mode', 'maintenance_mode_message', 10, 2 );
In this example, the maintenance_mode_message()
function returns a custom maintenance message. When the maintenance_mode
transient is checked, it will always return the custom message, effectively enabling maintenance mode on the website.
Cache a WordPress query result
Scenario: Cache the results of a custom WordPress query to improve performance.
function cache_query_results( $pre_transient, $transient ) { $args = array( 'post_type' => 'post', 'posts_per_page' => 5, ); $query = new WP_Query( $args ); $posts = $query->get_posts(); set_transient( 'example_transient', $posts, 3600 ); return $posts; } add_filter( 'pre_transient_example_transient', 'cache_query_results', 10, 2 );
In this example, the cache_query_results()
function creates a custom query for the latest 5 posts, caches the result using the set_transient()
function for 1 hour (3600 seconds), and returns the posts. If the transient value exists, it will be returned. If not, the query will be executed, cached, and then returned.
Cache a user’s profile data
Scenario: Cache a user’s profile data to reduce database queries.
function cache_user_profile( $pre_transient, $transient ) { $user_id = get_current_user_id(); $user_data = get_userdata( $user_id ); set_transient( 'example_transient', $user_data, 3600 ); return $user_data; } add_filter( 'pre_transient_example_transient', 'cache_user_profile', 10, 2 );
In this example, the `cache_user_profile()` function retrieves the current user’s profile data using the `get_userdata()` function, caches it using the `set_transient()` function for 1 hour (3600 seconds), and returns the user data. If the transient value exists, it will be returned. If not, the user data will be fetched, cached, and then returned.
Cache a custom option value
Scenario: Cache a custom option value to reduce database queries.
function cache_custom_option( $pre_transient, $transient ) { $option_value = get_option( 'my_custom_option' ); set_transient( 'example_transient', $option_value, 3600 ); return $option_value; } add_filter( 'pre_transient_example_transient', 'cache_custom_option', 10, 2 );
In this example, the cache_custom_option()
function retrieves the value of a custom option using the get_option()
function, caches it using the set_transient()
function for 1 hour (3600 seconds), and returns the option value. If the transient value exists, it will be returned. If not, the option value will be fetched, cached, and then returned.