Hot answers tagged

65

In backend there is global ajaxurl variable defined by WordPress itself. This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself. Good way to do this is to use wp_localize_script. Let's assume your AJAX calls are in my-ajax-script.js file, then add ...


65

WordPress uses the jQuery migrate script to ensure backwards compatibility for any plugins or themes you might be using which use functionality removed from newer versions of jQuery. With the release of WordPress 4.5, it appears they have upgraded the version of jQuery migrate from v1.2.1 to v1.4.0 - Having a quick scan through the code reveals that v1.4.0 ...


55

Here's another option which avoids having to de-register and re-register: /** * Move jQuery to the footer. */ function wpse_173601_enqueue_scripts() { wp_scripts()->add_data( 'jquery', 'group', 1 ); wp_scripts()->add_data( 'jquery-core', 'group', 1 ); wp_scripts()->add_data( 'jquery-migrate', 'group', 1 ); } add_action( '...


45

to use ajaxurl directly, in your plugin file add this: add_action('wp_head', 'myplugin_ajaxurl'); function myplugin_ajaxurl() { echo '<script type="text/javascript"> var ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>'; } you can then use the ajaxurl for ajax request.


44

Warning: You shouldn't replace core jQuery version, especially in the admin panel. Since many WordPress core functionality may depend on the version. Also, other plugin may depend on the jQuery version added in the core. If you are sure that you want to change the core jQuery version, in that case you may add the following CODE in your active theme's ...


40

Here's a working example: add_action( 'wp_enqueue_scripts', 'menu_scripts' ); function menu_scripts() { wp_enqueue_script( 'responsive-menu', get_bloginfo( 'stylesheet_directory' ) . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0' ); wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/js/custom_script.js', array( 'jquery' ...


36

There are many themes and plugins using jQuery as loaded by WP core. Every time jQuery is updated, there is a risk of older themes and plugins breaking (because lots of them are still used but no longer updated). That's why you also see jquery-migrate.js in your source code. It's a script that catches old functions used by plugins/themes and makes sure that ...


27

Yes, jQuery is part of WordPress core. But--it can become outdated, because jQuery updates can happen in between WP releases. The recent release of WordPress does use a very recent version of jQuery. By default, wp_enqueue_script('jquery') grabs jQuery from the core at /wp-includes/js/jquery/jquery.js. The "correct" way to add jQuery to your WP site ...


27

To do that you will first have to deregister your jQuery script and then register again. If you use jQuery comes with WordPress then following is the function your are looking for. function starter_scripts() { wp_deregister_script( 'jquery' ); wp_register_script( 'jquery', includes_url( '/js/jquery/jquery.js' ), false, NULL, true ); ...


27

This will do the trick when added to your functions file: if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11); function my_jquery_enqueue() { wp_deregister_script('jquery'); wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", false, null); wp_enqueue_script('jquery'); }


23

It's not outdated Moving to 2.x or 3.x is too early, people still use IE8 which is only supported on 1.x


22

Best practice method Have a look at wp_localize_script, which is meant to do exactly that. But it does require previous usage of wp_enqueue_scripts, hence you will need to move your JS to a separate file indeed. It will be worth those few minutes of effort though, for sure. function wpse_96370_scripts() { if ( is_single() ) { ...


22

A better solution: add_action( 'wp_default_scripts', 'move_jquery_into_footer' ); function move_jquery_into_footer( $wp_scripts ) { if( is_admin() ) { return; } $wp_scripts->add_data( 'jquery', 'group', 1 ); $wp_scripts->add_data( 'jquery-core', 'group', 1 ); $wp_scripts->add_data( 'jquery-migrate', 'group', 1 ); } ...


17

Here is a working solution (tested as is) The HTML (could be part of page content) <input type="text" name="keyword" id="keyword" onkeyup="fetch()"></input> <div id="datafetch">Search results will appear here</div> The JavaScript ( goes to your theme's functions.php ) // add the ajax ...


14

Milo is correct. For instance, go to your theme's functions.php file, and add the following: add_filter( 'allowed_http_origins', 'add_allowed_origins' ); function add_allowed_origins( $origins ) { $origins[] = 'https://site1.example.com'; $origins[] = 'https://site2.example.com'; return $origins; } Now an ajax call from https://site1.example....


13

Since wordpress 4.5 you can add inline script by wp_add_inline_script(). To add a javascript snippet to the footer that requires jQuery, this code helps you function enqueue_jquery_in_footer( &$scripts ) { if ( ! is_admin() ) $scripts->add_data( 'jquery', 'group', 1 ); } add_action( 'wp_default_scripts', 'enqueue_jquery_in_footer' ); ...


13

Hi You have Use this COde For WordPress front-end AJAX file upload tutorial Code Here is my code: In my template file example.php <form enctype="multipart/form-data"> <input type="text" name="support_title" class="support-title"> <input type="file" id="sortpicture" name="upload"> <input class="save-support" name="save_support" ...


13

I have developed a plugin for this specific problem. The plugin doesn't mess with WordPress jQuery as it is only loaded in the front-end. See: jQuery Manager for WordPress Why yet another jQuery Updater / Manager / Developer / Debugging tool? Because none of the developer tools lets you select a specific version of jQuery and/or jQuery Migrate. ...


12

Your $scr in your wp_register_script() function is wrong. Given that your functions.php is inside your plugin, and your removeArrows.js is in the root of your plugin, your $scr should look like this plugins_url( '/removeArrows.js' , __FILE__ ) Another point of note, it is always good practice to load your scripts and styles last. This will ensure that it ...


11

When I build themes, I also like to make the WordPress header as clean as possible and then reconstruct it to my own liking. The code below is excessive for your question, but it might help you with other 'WordPress inserted code' in the future. The key snippet of code you are looking for is wp_deregister_script('jquery'); wp_register_script('jquery', '', '...


11

A useful fast way is to use JavaScript in combination with an Ajax action that outputs the box. Create a small plugin. Create a folder inside plugins directory, naming it 'SubmitReviewConf'. In this folder create a PHP file named SubmitReviewConf.php. In this file, put this code: <?php /** * Plugin Name: Submit for Review Confirmation Message * ...


10

No, this won't work: <?php echo get_option('notice_data[Message]'); ?> Because get_option pulls whole option value by option_name, it doesn't pull by pieces of the serialized array. What you are asking for is a key (option_name) called literally notice_data[Message]. Assuming you've saved the option as notice_data you aren't going to get a match, and ...


10

the problem was in your variable define syntax and also you need to wrap your jQuery script into no conflict wrapper, try this jQuery(window).load(function($) { // your function }); or (function($) { // your function })(jQuery); complete working script (function($) { $(window).load(function() { var lightBox = $('#lightbox'); var ...


10

You could change the log message text to blank in jquery-migrate.min.js but this will not be preserved on core update. The alternative is to add passthrough/filter function copy of console.log to just before the migrate script is loaded, and tell it to ignore logging messages that contain 'Migrate is installed'. Doing it this way will preserve other Migrate ...


9

Given that all of the libraries you need for the datepicker are bundled with WordPress and are registered with all of the appropriate dependencies, all you really need to do is: function enqueue_my_scripts_wpse_97533() { wp_enqueue_script('jquery-ui-datepicker'); } add_action('wp_enqueue_scripts','enqueue_my_scripts_wpse_97533'); If you then look at the ...


9

This was very frustrating to figure out. I spent hours on this issue and discovered your problem is in this input: <input type="text" name="name" id="name" size="30" value=""/> Try changing the input field name to anything but "name", for example: <input type="text" name="user_name" id="name" size="30" value=""/>


9

Not sure what you try to accomplish, but you can get a value by key using the wp.customize object: wp.customize.value('show_on_front')(); wp.customize.value('blogname')(); .... sorry no jQuery here, just javascript, and yes, the extra () are intentional. Edit: Full overview of all settings: wp.customize._value; console.log(wp.customize._value); Edit II: ...


8

i have a php solution for you: add_filter('wp_terms_checklist_args', 'htmlandcms_select_one_category'); function htmlandcms_select_one_category($args) { if (isset($args['taxonomy']) && $args['taxonomy'] == 'category_portfolio') { $args['walker'] = new Walker_Category_Radios; $args['checked_ontop'] = false; } return $args; ...


8

wp_localize_script should be called AFTER wp_enqueue_script: IMPORTANT!: wp_localize_script() MUST be called after the script it's being attached to has been enqueued or registered. It doesn't put the localized script in a queue for later scripts. Fix it in your function: function my_action_callback() { wp_enqueue_script('jscustom'); // I assume you ...


8

add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ wp_deregister_script('jquery'); } That will deregister jquery. But why wouldn't you want jQuery at all? If you mean to simply use your own, you should do it in that function, like this: add_action('wp_enqueue_scripts', 'no_more_jquery'); function no_more_jquery(){ ...


Only top voted, non community-wiki answers of a minimum length are eligible