Title: options_general_add_js
Published: April 25, 2014
Last modified: May 20, 2026

---

# options_general_add_js()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#wp--skip-link--target)

Display JavaScript on the page.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#source)󠁿

    ```php
    function options_general_add_js() {
    	?>
    <script>
    	jQuery( function($) {
    		var $siteName = $( '#wp-admin-bar-site-name' ).children( 'a' ).first(),
    			$siteIconPreview = $('#site-icon-preview-site-title'),
    			homeURL = ( <?php echo wp_json_encode( get_home_url(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> || '' ).replace( /^(https?:\/\/)?(www\.)?/, '' );

    		$( '#blogname' ).on( 'input', function() {
    			var title = $.trim( $( this ).val() ) || homeURL;

    			// Truncate to 40 characters.
    			if ( 40 < title.length ) {
    				title = title.substring( 0, 40 ) + '\u2026';
    			}

    			$siteName.text( title );
    			$siteIconPreview.text( title );
    		});

    		$( 'input[name="date_format"]' ).on( 'click', function() {
    			if ( 'date_format_custom_radio' !== $(this).attr( 'id' ) )
    				$( 'input[name="date_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
    		});

    		$( 'input[name="date_format_custom"]' ).on( 'click input', function() {
    			$( '#date_format_custom_radio' ).prop( 'checked', true );
    		});

    		$( 'input[name="time_format"]' ).on( 'click', function() {
    			if ( 'time_format_custom_radio' !== $(this).attr( 'id' ) )
    				$( 'input[name="time_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() );
    		});

    		$( 'input[name="time_format_custom"]' ).on( 'click input', function() {
    			$( '#time_format_custom_radio' ).prop( 'checked', true );
    		});

    		$( 'input[name="date_format_custom"], input[name="time_format_custom"]' ).on( 'input', function() {
    			var format = $( this ),
    				fieldset = format.closest( 'fieldset' ),
    				example = fieldset.find( '.example' ),
    				spinner = fieldset.find( '.spinner' );

    			// Debounce the event callback while users are typing.
    			clearTimeout( $.data( this, 'timer' ) );
    			$( this ).data( 'timer', setTimeout( function() {
    				// If custom date is not empty.
    				if ( format.val() ) {
    					spinner.addClass( 'is-active' );

    					$.post( ajaxurl, {
    						action: 'date_format_custom' === format.attr( 'name' ) ? 'date_format' : 'time_format',
    						date 	: format.val()
    					}, function( d ) { spinner.removeClass( 'is-active' ); example.text( d ); } );
    				}
    			}, 500 ) );
    		} );

    		var languageSelect = $( '#WPLANG' );
    		$( 'form' ).on( 'submit', function() {
    			/*
    			 * Don't show a spinner for English and installed languages,
    			 * as there is nothing to download.
    			 */
    			if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
    				$( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
    			}
    		});
    	} );
    </script>
    	<?php
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/options.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/options.php#L34)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/options.php#L34-L106)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#related)󠁿

| Uses | Description | 
| [get_home_url()](https://developer.wordpress.org/reference/functions/get_home_url/)`wp-includes/link-template.php` |

Retrieves the URL for a given site where the front end is accessible.

  | 
| [wp_json_encode()](https://developer.wordpress.org/reference/functions/wp_json_encode/)`wp-includes/functions.php` |

Encodes a variable into JSON, with some confidence checks.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/options_general_add_js/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.5.0](https://developer.wordpress.org/reference/since/3.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Foptions_general_add_js%2F)
before being able to contribute a note or feedback.