考虑在WordPress中使用本地化脚本
<?php
// 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\' );
你可以在这里找到更多细节,
wp_localize_script