Environment:
What I am trying to achieve?我有一个JavaScript验证脚本,可以验证注册页面上的输入。此脚本显示错误消息。我想要两件事:字符串应使用WPML进行管理/翻译脚本应仅嵌入注册页面
What did I already achieve?
我向函数中添加了以下脚本。我的主题的php。add_action(\'wp_enqueue_scripts\', function() {
// Register the script
wp_register_script( \'register-validation-js\', \'/wp-content/themes/agreedo/js/register.js\' );
// Localize the script with new data
$translation_array = array(
\'first_name_string\' => esc_attr__( \'First Name 123\', \'agreedo\' ),
\'a_value\' => \'10\'
);
wp_localize_script( \'register-validation-js\', \'object_name\', $translation_array );
// Enqueued script with localized data.
wp_enqueue_script( \'register-validation-js\' );
} );
至少我这么认为,因为当我分析任何页面的源代码时,我会看到以下内容:<script type=\'text/javascript\' id=\'register-validation-js-js-extra\'>
/* <![CDATA[ */
var object_name = {"first_name_string":"First Name 123","a_value":"10"};
/* ]]> */
</script>
<script type=\'text/javascript\' src=\'https://test.agreedo.com/wp-content/themes/agreedo/js/register.js?ver=5.8\' id=\'register-validation-js-js\'></script>
在控制台中,我可以输入:object\\u name。first\\u name\\u string和get;名字123“;
到目前为止还不错。我还成功地扫描了主题,并查看了WPML中可用的字符串:
现在有没有办法只将此代码添加到注册页面而不是所有页面,因为验证只在注册页面中进行?Sources I used to accomplish this: