Custom JS on a specific page

First of all, you should declare your scripts using wp_enque_script. Second, jQuery is loaded automatically in WordPress and is set to no conflict mode. See the documentation for how to link a script that depends on jQuery. Lastly, if you want it to appear on just one page you have two options In your functions.php … Read more

Add JavaScript to single post

One option would be to create a shortcode exclusively for that script. Even thought it’s not an elegant solution, it works. In your functions file (functions.php) of your theme add: function custom_script_shortcode(){ $code=”<script>”; $code .= ‘var foo = “bar”;’ $code .= ‘</script>’; return $code; } add_shortcode(“custom_script_shortcode”, “custom_script”); And in your post editor then add the … Read more

Including Styles and JS files to work ON my plugin interface

Well after a bit of better searching i found the answer, function my_enqueue($hook) { if ( ‘settings_page_data-layer-management’ != $hook ) { return; } wp_enqueue_script( ‘my_custom_script’, plugins_url( ‘js/dlm-window.js’, __FILE__ )); } add_action( ‘admin_enqueue_scripts’, ‘my_enqueue’ ); to explain just incase anyone has this issue. what i had earlier was a ‘front-end’ hook which is just wp_enqueue_scripts for … Read more

CDATA removing new line in script tag in wordpress

This is being caused by core bug #33106 which was unfortunately introduced with the security fixes in 4.1.6 (and 4.2.3). New lines are erroneously being stripped from CDATA blocks in this situation. From the bug ticket: As of 4.2.3, depending on how a CDATA block is used, the stripping of new lines in this content … Read more

print script on wordpress header after user registered

There’s quite a few issues here: do_action(‘user_register’, $user_id ); No need to fire the action yourself (especially with an undefined variable $user_id) – the idea of actions & filters is that you “hook” your function to one, and then let WordPress trigger it when applicable. function MY_Callback(){ $data[‘user_id’] = $user_id; My_print_js($data); } Again $user_id is … Read more

Using javascript on my site to create nested tabs

A couple of things before I get into the code: 1) It would probably be a cleaner solution to solve this in the theme via extended functionality (PHP modifications) as you gain more experience with WordPress. 2) If you are able to use numbers (1,2,3) instead of words (one,two,three) you can clean up the javascript … Read more

Normal for scripts to be loaded after body tag?

This happens when something is incorrectly outputting markup within the head tag that should be inside the body tag. The browser rendering engine moves the body tag before whatever markup is incorrectly inserted. It looks like your #wrapper div is the likely culprit here and is being output in the wrong place.

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