How do I know if I should enqueue JS code or just include it in ONE PHP function?

Best practice in this case would be to enqueue the js file inside the WP functions.php file. Are the braintree submit pages on their own unique templates? If so you could add something like this to your functions file: function register_scripts() { if(is_page_template(‘template-parts/page-braintree.php’)): wp_register_script( ‘braintree-js’, get_template_directory_uri() . ‘/js/braintree.js’, array( ‘jquery’ ), false, true ); wp_enqueue_script( … Read more

Include script files for admin submenu page

You must have created submenu page using add_submenu_page() hook, so you can use wp_enqueue_script inside callback function of add_submenu_page hook. It will load script only on your submenu page. add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug,’callback’); function callback() { //enqueue script } Or if you want to enqueue script in dashboard(all pages) then you can enqueue … Read more

Using wp_enqueue_script on scripts that contain PHP

If you’re outputting the script in the footer, you can call wp_localize_script at any point after your script is enqueued but before the script is output on wp_footer priority 20. function wp_enqueue_woocommerce_style(){ wp_enqueue_script( ‘chroma_js’, get_template_directory_uri() . ‘/js/chroma.js’, ”, ‘1.1’, true ); } add_action( ‘wp_enqueue_scripts’, ‘wp_enqueue_woocommerce_style’ ); function wp_localize_woocommerce_style(){ $array = array( ‘var1’ => $var1, ‘var2’ … Read more

wp_enqueue wont work on some files?

That isn’t how functions.php works in WordPress. The functions.php in your theme root directory is loaded into WordPress environment by default as long as your theme is activated. It’s used as a plugin to add features and functionality of your theme to WordPress. You should take a look at theme functions and including css & … Read more

WordPress wp_enqueue_script only adds text to top of page source

You need to wrap your text with <?php and ?>. Like this: <?php function my_scripts() { wp_enqueue_script( ‘jquery’ ); wp_register_script( ‘parallax’, get_template_directory_uri() . ‘/parallax.js’, array( ‘jquery’ ), NULL, true ); wp_enqueue_script( ‘parallax’ ); } add_action( ‘wp_enqueue_scripts’, ‘my_scripts’ ); ?> Otherwise, the PHP parser never sees it – and it’s sent as text to the browser. … Read more

jQuery plugin not loading

It might be the case that you are using you JS validation before enqueuing this script. You code may be like.. <html> $(“#myForm”).validate(); or JS file that uses validate() jquery.validate.js here </html> Above code is just for demonstrating. So you are using validate() function before it has been enqueued so it is throwing validate() is … Read more