When does WordPress automatically enqueue jQuery?
On the frontend, WordPress enqueues jQuery only when the admin bar is visible, usually only for users who are logged in. If you need jQuery, enqueue it. It will not be included twice in this case.
On the frontend, WordPress enqueues jQuery only when the admin bar is visible, usually only for users who are logged in. If you need jQuery, enqueue it. It will not be included twice in this case.
You have to use the admin-ajax handler to do the AJAX call. Replace file.php in var url = “file.php”; with yoursite.com/wp-admin/admin-ajax.php?action=simple_ajax And in your plugin / functions.php file, add add_action(‘wp_ajax_nopriv_simple_ajax’,’process_simple_ajax’); //for non logged in user add_action(‘wp_ajax_simple_ajax’,’process_simple_ajax’); //for nlogged in user function process_simple_ajax(){ $data = $_REQUEST; // retrieve your submitted data wp_send_json($data); // return the processed … Read more
Script won’t execute in the media manager
The result that is currently returned is raw text, which means data[0] accesses the first element (i.e. character) of the string, which is {. Obviously, you want to turn the returned data into a JSON object. To do this, jQuery.ajax() has a nifty property called dataType, which determines the way jQuery interprets the data returned … Read more
You have missed few things in your ajax. Please find following updated code which will help you to solve your problem function add_ajaxurl_cdata_to_front(){ ?> <script type=”text/javascript”> //<![CDATA[ ajaxurl=”<?php echo admin_url( “admin-ajax.php’ ); ?>’; //]]> </script> <?php } add_action( ‘wp_head’, ‘add_ajaxurl_cdata_to_front’, 1); add_action( ‘wp_footer’, ‘add_js_to_wp_footer’ ); function add_js_to_wp_footer(){ ?> <script type=”text/javascript”> jQuery(‘document’).ready(function(){ jQuery(‘#reg_email’).on(“change”,function(){ jQuery.ajax({ type: ‘POST’, … Read more
Yes, it is typically considered bad form to do that. The only reason you might do that is if the script you are including will be used on the page you are editing and ONLY that page.
Ajax url should be set to wp-admin/admin-ajax.php as ajax requests should be handled via wp-admin/admin-ajax in wordpress. if ajaxurl is not defined you can get it by defining html attribute data-url as data-url=”<?php echo get_admin_url().’admin-ajax.php’?>”. jQuery(document).ready(function($){ $(“#wp-submit”).on(‘click’,function(e){ e.preventDefault(); username = $(“#userName”).val(); password = $(“#passWord”).val(); ajaxurl = $(this).data(‘url’); $.ajax({ type: “POST”, url: ajaxurl, data : { … Read more
When enqueuing a script, you should state your script’s dependencies. In your case, it’s jQuery. Enqueue your script in the following way: wp_enqueue_script ( ‘my-plugin’, ‘path/to/the.js’, array( ‘jquery’ ) ); You may also want to ensure that your script IS loaded in the footer, by setting the last argument to true: wp_enqueue_script ( ‘my-plugin’, ‘path/to/the.js’, … Read more
You haven’t passed jquery as a dependency for your script, and you are using the dollar sign too, which is not directly supported in WordPress due to conflict. First, pass jQuery as a requirement while enqueuing your navigation.js: wp_enqueue_script( ‘themename-navigation’, get_template_directory_uri() . ‘/js/navigation.js’, array(‘jquery’), ‘20151215’, true ); Then, wrap your code in a self invoking … Read more
This error means that the resource (JS file in this case) that you’re loading is expected to be JS and is instead HTML. It could be because your path is incorrect or because the local file is not JS. More than likely, your provided script at /assets/js/masonry-script.js is not a JS file or couldn’t be … Read more