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

Referencing files in JavaScript in WordPress Plugin

Use wp_localize_script. function pr_add_link_sound () { //Register the script first wp_register_script(‘add_sound’, plugins_url( ‘add_sound.js’, __FILE__ ), array(‘jquery’), ”, true); //Localize the sound file url to use in the script $args = array( ‘sound1’ => plugins_url( ‘sound.mp3’, __FILE__ ), ); wp_localize_script( ‘add_sound’, ‘PRSound’, $args ); //Enqueue the script wp_enqueue_script(‘add_sound’); } You can reference the sound in the … 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

Getting 404 in the Javascript

I think this is not good idea – create many enter points. Open Web Inspector and check your URL which using in JS code. But much better use the default admin_url( ‘admin-ajax.php’ ) to creates path which should be used in Java Script.

Link to page with posts of specific type

First, never use query_posts, use WP_Query to create additional queries. When you register your post type, set has_archive to true, or a string which will be the URL slug for your post type archive. You can then use get_post_type_archive_link() to output the URL to your post type archive. <a href=”https://wordpress.stackexchange.com/questions/210453/<?php echo get_post_type_archive_link(“issues’ ); ?>”><li>View All</li></a>

How do I make multiple dependent input fields that use the jquery autocomplete function?

Add a autocomplete ‘select’ catch to your first autocomplete field. Use this to get the chosen value from the autocomplete. Use an Ajax call to obtain your next autocomplete source based on the selected value and apply the new source to the next autocomplete field. jQuery( “#cns-make” ).autocomplete({ source: availableTags, select: function (event, ui) { … Read more

Run JS Code on userlog out

wp_logout is being triggered server side when the user is logging out, but the fact that it is being triggered do not indicate that there is a browser on the other side that actually triggered it. For example a user might log out via Ajax and then the JS code will not be evaluated. In … Read more