返回介绍

wp_add_inline_script()

发布于 2017-09-11 11:20:02 字数 4640 浏览 1390 评论 0 收藏 0

wp_add_inline_script( string $handle,  string $data,  string $position = 'after' )

Adds extra code to a registered script.


description

Code will only be added if the script in already in the queue. Accepts a string $data containing the Code. If two or more code blocks are added to the same script $handle, they will be printed in the order they were added, i.e. the latter added code can redeclare the previous.


参数

$handle

(string) (Required) Name of the script to add the inline script to.

$data

(string) (Required) String containing the javascript to be added.

$position

(string) (Optional) Whether to add the inline script before the handle or after.

Default value: 'after'


返回值

(bool) True on success, false on failure.


源代码

File: wp-includes/functions.wp-scripts.php

function wp_add_inline_script( $handle, $data, $position = 'after' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

	if ( false !== stripos( $data, '</script>' ) ) {
		_doing_it_wrong( __FUNCTION__, sprintf(
			/* translators: 1: <script>, 2: wp_add_inline_script() */
			__( 'Do not pass %1$s tags to %2$s.' ),
			'<code>&lt;script&gt;</code>',
			'<code>wp_add_inline_script()</code>'
		), '4.5.0' );
		$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
	}

	return wp_scripts()->add_inline_script( $handle, $data, $position );
}

更新日志

Versiondescription
4.5.0Introduced.

相关函数

Uses

  • wp-includes/class.wp-scripts.php: WP_Scripts::add_inline_script()
  • wp-includes/functions.wp-scripts.php: wp_scripts()
  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: _doing_it_wrong()

Used By

  • wp-includes/widgets/class-wp-widget-media-audio.php: WP_Widget_Media_Audio::enqueue_admin_scripts()
  • wp-includes/widgets/class-wp-widget-media-video.php: WP_Widget_Media_Video::enqueue_admin_scripts()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::enqueue_admin_scripts()
  • wp-includes/script-loader.php: wp_localize_jquery_ui_datepicker()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by Clifford Paulick

    The following code can be used to easily add Typekit’s JavaScript to your theme:

    
    function mytheme_enqueue_typekit() {
       wp_enqueue_script( 'mytheme-typekit', 'https://use.typekit.net/.js', array(), '1.0' );
       wp_add_inline_script( 'mytheme-typekit', 'try{Typekit.load({ async: true });}catch(e){}' );
    }
    add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_typekit' );
    

    Which results in:

    
    <script type="text/javascript" src="https://use.typekit.net/.js?ver=1.0">
        try{Typekit.load({ async: true });}catch(e){}
    </script>
    

    From https://make.wordpress.org/core/2016/03/08/enhanced-script-loader-in-wordpress-4-5/

  2. For adding inline script

    
    function theme_prefix_enqueue_script() {
       wp_enqueue_script( 'main-js', '/main.js', array(), '1.0' );
       wp_add_inline_script( 'main-js', 'alert("hello world");' );
    }
    add_action( 'wp_enqueue_scripts', 'theme_prefix_enqueue_script' );
    

    For jQuery-dependent scripts use this:

    
    function theme_prefix_enqueue_script() {
       if ( ! wp_script_is( 'jquery', 'done' ) ) {
         wp_enqueue_script( 'jquery' );
       }
       wp_add_inline_script( 'jquery-migrate', 'jQuery(document).ready(function(){});' );
    }
    add_action( 'wp_enqueue_scripts', 'theme_prefix_enqueue_script' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文