Using WordPress ‘options_reading_add_js()’ PHP function

The options_reading_add_js() WordPress PHP function is used to display JavaScript on a page.


To use the options_reading_add_js() function, simply call it in your theme or plugin file:



This function has no parameters.

More information

See WordPress Developer Resources: options_reading_add_js


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>
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('send', 'pageview');
add_action('wp_footer', 'insert_google_analytics');

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) {
                behavior: "smooth"
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!");
add_action('wp_footer', 'add_jquery_click_event');