返回介绍

wp_localize_script()

发布于 2017-09-11 12:23:09 字数 5635 浏览 974 评论 0 收藏 0

wp_localize_script( string $handle,  string $object_name,  array $l10n )

Localize a script.


description

Works only if the script has already been added.

Accepts an associative array $l10n and creates a JavaScript object:

"$object_name" = {
    key: value,
    key: value,
    ...
}

参数

$handle

(string) (Required) Script handle the data will be attached to.

$object_name

(string) (Required) Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.

$l10n

(array) (Required) The data itself. The data can be either a single or multi-dimensional array.


返回值

(bool) True if the script was successfully localized, false otherwise.


源代码

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

function wp_localize_script( $handle, $object_name, $l10n ) {
	global $wp_scripts;
	if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
		_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
		return false;
	}

	return $wp_scripts->localize( $handle, $object_name, $l10n );
}

更新日志

Versiondescription
2.2.0Introduced.

相关函数

Uses

  • wp-includes/class.wp-scripts.php: WP_Scripts::localize()

Used By

  • wp-includes/script-loader.php: wp_localize_community_events()
  • wp-includes/theme.php: the_custom_header_markup()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::enqueue_scripts()
  • wp-includes/customize/class-wp-customize-background-image-control.php: WP_Customize_Background_Image_Control::enqueue()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::prepare_items()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::prepare_items()
  • wp-includes/media.php: wp_enqueue_media()
  • wp-includes/customize/class-wp-customize-header-image-control.php: WP_Customize_Header_Image_Control::enqueue()
  • wp-includes/script-loader.php: wp_just_in_time_script_localization()
  • Show 4 more used by Hide more used by

User Contributed Notes

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

    Example

    
    // Register the script
    wp_register_script( 'some_handle', 'path/to/myscript.js' );
    
    // Localize the script with new data
    $translation_array = array(
    	'some_string' => __( 'Some string to translate', 'plugin-domain' ),
    	'a_value' => '10'
    );
    wp_localize_script( 'some_handle', 'object_name', $translation_array );
    
    // Enqueued script with localized data.
    wp_enqueue_script( 'some_handle' );
    

    You can access the variables in JavaScript as follows:

    
    // alerts 'Some string to translate'
    alert( object_name.some_string );
    
  2. Example

    
    // Register the script
    wp_register_script( 'some_handle', 'path/to/myscript.js' );
    
    // Localize the script with new data
    $translation_array = array(
    	'some_string' => __( 'Some string to translate', 'textdomain' ),
    	'a_value' => '10'
    );
    wp_localize_script( 'some_handle', 'object_name', $translation_array );
    
    // Enqueued script with localized data.
    wp_enqueue_script( 'some_handle' );
    

    You can access the variables in JavaScript as follows:

    
    <script>
    	// alerts 'Some string to translate'
    	alert( object_name.some_string);
    </script> 
    

    to send ajax request from theme files we can use wp_localize_script to globally declare our javascript variables.

    function theme_enqueue_scripts() {
    	/**
    	 * frontend ajax requests.
    	 */
    	wp_enqueue_script( 'frontend-ajax', JS_DIR_URI . 'frontend-ajax.js', array('jquery'), null, true );
    	wp_localize_script( 'frontend-ajax', 'frontend_ajax_object',
    		array( 
    			'ajaxurl' => admin_url( 'admin-ajax.php' ),
    			'data_var_1' => 'value 1',
    			'data_var_2' => 'value 2',
    		)
    	);
    }
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );

    in our js file we can use any of the globally declared variable frontend_ajax_object.ajaxurl, frontend_ajax_object.data_var_1, frontend_ajax_object.data_var_1.

    jQuery(document).ready(function($) {
    	$.ajax({
            url: frontend_ajax_object.ajaxurl,
            type: 'get',
            data: {
                'action':'states_city_filter'
            },
            success: function( response ) {
            	console.log(response);
            },
        });
    });

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

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

发布评论

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