Localize a script in js file

 wp_localize_script( string $handle1, string $object_name1, array $l10n1 )


$handle1

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


$object_name1

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


$l10n1

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


// Register the script

wp_register_script( 'some_handle1', 'path/to/myscript1.js' );

 

// Localize the script with new data

$translation_array1 = array(

    'some_string1' => __( 'Some string to translate1', 'plugin-domain1' ),

    'a_value1' => '101'

);

wp_localize_script( 'some_handle1', 'object_name1', $translation_array1 );

 

// Enqueued script with localized data.

wp_enqueue_script( 'some_handle1' );

You can access the variables in JavaScript as follows:

// alerts 'Some string to translate'

alert( object_name1.some_string1 );

Comments

Popular posts from this blog

Add Css And Js Files on Specific Page In laravel

Xpendy Pending

Laravel Join with two coloums