google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Chèn JavaScript vào WordPress hiệu quả hơn với tính năng định vị hóa Bài viết này sẽ cung cấp cho chúng ta một hướng dẫn chi tiết về tính năng localization (định vị hóa) trong nền tảng WordPress, qua đó chúng ta có thể quản lí các tập tin/mã nguồn JavaScript được thêm vào hiệu quả hơn.


Nhãn: chèn JavaScript vào WordPress, định vị hóa, tính năng localization, nền tảng WordPress

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

When adding scripts to WordPress, you will inevitably run into a small, but painful, issue of localization.

Localizing a plugin or theme is relatively straightforward, but JavaScript presents its own difficulties since we can't easily call the PHP functions necessary (which is one reason authors embed JavaScript in PHP files).

Since embedding JavaScript in PHP files is never a good technique, we use localization to save the day.

With JavaScript localization, you can use PHP magic to build your localized strings, and then use JavaScript to read/parse those strings. What you do with them is only limited to your imagination.

Furthermore, if you display anything with JavaScript, chances are your users will want the strings to be localized.

Fortunately, WordPress provides the ultra-handy wp_localize_script function.

wp_localize_script

The wp_localize_script takes three arguments:

  • handle
  • object_name
  • l10n

Handle

The handle argument will be the same handle you use for your script name.
For example, if you have a handle of my_script, you would use the same name when calling the wp_localize_script function.

Object_name

The object_name argument is a string that tells WordPress to create a JavaScript object using the name you specify.

It's important that the string you pass is as unique as possible in order to minimize naming conflicts with other scripts.

For the upcoming example, our object name will be my_unique_name.

l10n

The l10n argument is an array of strings you would like to localize.
Within this array, you will want to take advantage of the __ function.

wp_localize_script Example

For the purpose of this example, let's create a function called localize_vars and have it return an array.


<?php
function localize_vars() {

    return array(
        'SiteUrl' => get_bloginfo('url'),
        'OtherText' => __('my text', "my_localization_name")

    );
} //End localize_vars
?>

Please note the use of the __() function. It takes in the text we want to localize, and our localization name. This will be the same name you use if you take advantage of localization within WordPress.

The variable SiteURL gives us the http path to our WordPress site, which comes in handy in certain situations.

From another area in our code, we call the localize_vars function:


<?php 
wp_enqueue_script('my_script', plugins_url('your-plugin-name') .'/my_script.js', array('jquery'), '1.0.0');

wp_localize_script( 'my_script', 'my_unique_name', localize_vars());
?>

WordPress then conveniently adds localization JavaScript immediately before our main script is included. Viewing the page source will reveal:


/* <![CDATA[ */
    my_unique_name = {
        SiteUrl: "http://www.mydomain.com",

        OtherText: "my localized text"
    }
/* ]]> */
</script>
<script type='text/javascript' src='http://www.mydomain.com/wp-content/plugins/my_plugin/my_script.js?ver=1.0.0'></script>

With the localize example, you can use PHP magic to add just about anything to your localization object. Hence, no need to ever embed JavaScript within a PHP file.

Now you can call your localized JavaScript variables from your my_script.js file. Here's an example of an alert:


alert(my_unique_name.SiteUrl);

alert(my_unique_name.OtherText);

It's really as easy as that. You can now localize JavaScript strings.

iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web