函数文档

options_general_add_js()

💡 云策文档标注

概述

options_general_add_js() 函数用于在页面中显示 JavaScript 代码,是 WordPress 核心功能的一部分,自 3.5.0 版本引入。

关键要点

  • 函数名称:options_general_add_js()
  • 主要用途:在页面中显示 JavaScript
  • 引入版本:WordPress 3.5.0
  • 相关函数:get_home_url() 用于获取站点前端可访问的 URL,wp_json_encode() 用于将变量编码为 JSON 并进行置信度检查

📄 原文内容

Display JavaScript on the page.

Source

function options_general_add_js() {
?>
<script type="text/javascript">
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>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/options.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/options.php#L34">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/options.php#L34-L106">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_home_url/">get_home_url()</a><code>wp-includes/link-template.php

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

wp_json_encode()wp-includes/functions.php

Encodes a variable into JSON, with some confidence checks.

Changelog

Version Description
3.5.0 Introduced.