在函数中导入JavaScript.php不起作用


importing javascript in functions.php not working

我对php相当陌生,所以请原谅缺乏知识。

目前,我正在尝试将一个小的javascript文件添加到子主题中。我已将以下代码添加到子级函数.php文件中。

 if ( is_page_template( 'page-templates/edit-profile.php' ) ) {
        function add_form_unlock() {
            wp_enqueue_script(
                'form_unlock',
                $get_stylesheet_directory_uri() . '/js/form_unlock.js',
                array( 'jquery'),
                '1.1',
                true
            );   
        }
        add_action( 'wp_enqueue_scripts', 'add_form_unlock' );
        }

我的JavaScript是

var pass1 = "QuL7eD";
function check(){
   var pass2 = document.getElementById("password").value;
     if (pass1 == pass2) {
             document.getElementById("button").disabled = false;
             document.getElementById("test").disabled = false;
   }
} 

我已经成功地在测试.html文件中运行了javascript,很明显它只是没有在wordpress模板中运行。

任何帮助都会很棒!

不要将 WordPress 钩子/过滤器包装在 IF 语句中。 if 语句返回 false,因为在计算函数.php文件时,您不在模板内。

if 语句应该位于 add_form_unlock 函数内部,如下所示:

 function add_form_unlock() {
     if ( is_page_template( 'page-templates/edit-profile.php' ) ) { 
          wp_enqueue_script(
               'form_unlock',
               get_stylesheet_directory_uri() . '/js/form_unlock.js',
               array( 'jquery'),
               '1.1',
               true
         );   
      }
 }
add_action( 'wp_enqueue_scripts', 'add_form_unlock' );