The cache_javascript_headers()
WordPress PHP function sets the headers for caching for 10 days with a JavaScript content type.
Usage
Here’s a simple example of using cache_javascript_headers()
:
add_action( 'init', 'cache_javascript_headers' );
In this example, the cache_javascript_headers()
function is hooked to the ‘init’ action, enabling JavaScript content to be cached for 10 days as soon as WordPress initializes.
Parameters
- No parameters for this function.
More information
See WordPress Developer Resources: cache_javascript_headers()
This function was implemented in WordPress 2.1.0. Source code for this function can be found in wp-includes/functions.php
.
Examples
Using cache_javascript_headers()
on Plugin Activation
This example sets the cache headers when a specific plugin is activated.
function my_plugin_activation() { cache_javascript_headers(); } register_activation_hook( __FILE__, 'my_plugin_activation' );
This code will set the cache headers for JavaScript content when the plugin file this code resides in is activated.
Using cache_javascript_headers()
on Custom Page Template
This example sets the cache headers when a specific custom page template is used.
if ( is_page_template( 'template-custom.php' ) ) { cache_javascript_headers(); }
This code checks if the current page uses the ‘template-custom.php’ template. If it does, the cache headers for JavaScript content are set.
Using cache_javascript_headers()
on AJAX Request
This example sets the cache headers during an AJAX request.
function my_ajax_request() { cache_javascript_headers(); // Process AJAX request here } add_action( 'wp_ajax_my_action', 'my_ajax_request' );
In this example, the cache headers for JavaScript content are set whenever an AJAX request is made to ‘my_action’.
Using cache_javascript_headers()
on Custom Hook
This example sets the cache headers whenever a custom action hook is triggered.
function my_custom_action() { cache_javascript_headers(); } add_action( 'my_custom_hook', 'my_custom_action' );
This code will set the cache headers for JavaScript content whenever the custom hook ‘my_custom_hook’ is triggered.
Using cache_javascript_headers()
on Specific Post
This example sets the cache headers when a specific post is viewed.
if ( is_single( '123' ) ) { cache_javascript_headers(); }
In this example, the cache headers for JavaScript content are set when the post with the ID of 123 is viewed.