Using WordPress ‘options_permalink_add_js()’ PHP function

The options_permalink_add_js WordPress PHP function adds JavaScript to the page.

Usage

To use the function, simply call it within your theme or plugin:

options_permalink_add_js();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: options_permalink_add_js

Examples

In this example, we use the options_permalink_add_js function to add JavaScript to the footer of the website.

function my_theme_add_js_to_footer() {
  echo '<script>';
  options_permalink_add_js();
  echo '</script>';
}
add_action('wp_footer', 'my_theme_add_js_to_footer');

Add JavaScript to the header

In this example, we use the options_permalink_add_js function to add JavaScript to the header of the website.

function my_theme_add_js_to_header() {
  echo '<script>';
  options_permalink_add_js();
  echo '</script>';
}
add_action('wp_head', 'my_theme_add_js_to_header');

Add JavaScript to a specific page

In this example, we use the options_permalink_add_js function to add JavaScript only to a specific page with the ID of 42.

function my_theme_add_js_to_specific_page() {
  if (is_page(42)) {
    echo '<script>';
    options_permalink_add_js();
    echo '</script>';
  }
}
add_action('wp_footer', 'my_theme_add_js_to_specific_page');

Add JavaScript to a custom hook

In this example, we use the options_permalink_add_js function to add JavaScript to a custom hook called ‘my_theme_custom_hook’.

function my_theme_add_js_to_custom_hook() {
  echo '<script>';
  options_permalink_add_js();
  echo '</script>';
}
add_action('my_theme_custom_hook', 'my_theme_add_js_to_custom_hook');

Add JavaScript to a specific post type

In this example, we use the options_permalink_add_js function to add JavaScript only to posts of the custom post type ‘event’.

function my_theme_add_js_to_custom_post_type() {
  if (is_singular('event')) {
    echo '<script>';
    options_permalink_add_js();
    echo '</script>';
  }
}
add_action('wp_footer', 'my_theme_add_js_to_custom_post_type');