0

I am fairly new to php so forgive the lack of knowledge.

Currently I am trying to add a small javascript file to a child theme. I have added the following code to the child's functions.php file.

 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' );
        }

My javascript is

var pass1 = "QuL7eD";

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

   }
} 

I have run the javascript in a test.html file with success so clearly it is just not running in the wordpress template.

Any help would be great!

3
  • 7
    Just as a tip, you should never have passwords stored in javascript like that where the user could easily view the source and read it straight from your code. Commented Jan 28, 2016 at 21:30
  • 5
    $get_stylesheet_directory_uri()? variable functions? You sure that shouldn't be just get_stylesheet_directory_uri()? no $? Commented Jan 28, 2016 at 21:32
  • The password is just for testing at the moment but yes, thanks Nick. And Marc you were right about the $. Thanks! Commented Jan 29, 2016 at 18:34

1 Answer 1

1

Don't wrap WordPress Hooks/Filters inside of IF statements. The if statement is returning false because you aren't inside of the template when your functions.php file is evaluated.

The if statement should be inside of your add_form_unlock function, like this:

 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' );
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Linnea! This worked for me. I did have an extra $ in there too but took care of that!
Glad to have helped! Just updated it to remove that stray $.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.