The options_reading_add_js() WordPress PHP function is used to display JavaScript on a page.
Usage
To use the options_reading_add_js() function, simply call it in your theme or plugin file:
options_reading_add_js();
Parameters
This function has no parameters.
More information
See WordPress Developer Resources: options_reading_add_js
Examples
Adding a JavaScript alert to your page
This example will add a simple JavaScript alert to your page when it is loaded.
function display_js_alert() { echo '<script>alert("Hello, this is a JavaScript alert!");</script>'; } add_action('wp_footer', 'display_js_alert');
Load a custom JavaScript file
This example will enqueue a custom JavaScript file located in your theme’s /js
folder.
function load_custom_js() { wp_enqueue_script('my_custom_script', get_template_directory_uri() . '/js/my-custom-script.js', array(), '1.0.0', true); } add_action('wp_enqueue_scripts', 'load_custom_js');
Add Google Analytics tracking code
This example will add Google Analytics tracking code to your WordPress site.
function insert_google_analytics() { echo "<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXXX-Y', 'auto'); ga('send', 'pageview'); </script>"; } add_action('wp_footer', 'insert_google_analytics');
Smooth scroll to anchor links
This example will add smooth scrolling to anchor links on your WordPress site.
function smooth_scroll_to_anchor() { echo '<script> document.querySelectorAll("a[href^=\"#\"]").forEach(anchor => { anchor.addEventListener("click", function (e) { e.preventDefault(); document.querySelector(this.getAttribute("href")).scrollIntoView({ behavior: "smooth" }); }); }); </script>'; } add_action('wp_footer', 'smooth_scroll_to_anchor');
Add a simple jQuery click event
This example will add a simple jQuery click event to a button with a specific class.
function add_jquery_click_event() { echo '<script> jQuery(document).ready(function($) { $(".my-button").click(function() { alert("Button clicked!"); }); }); </script>'; } add_action('wp_footer', 'add_jquery_click_event');