Ajax gives 400 error

Explanation: The issue you are likely facing is that you are trying to add your ajax actions from within the widget itself that may be instantiated on the elementor/widgets/widgets_registered hook. If so, this is hook elementor/widgets/widgets_registered is too late and do_action( “wp_ajax_{$action}” ) and do_action( “wp_ajax_nopriv_{$action}” ) have already fired. Working example: NOTE: very rough … Read more

How to declare a JS variable in an AJAX call

The right way is to post the data back to the WordPress admin ajax url then in your PHP function access them using the $_POST variables. Example jQuery function that passed a category id back to wordpress: function cat_edit_get() { jQuery( “#loading-animation”).show(); var catID = jQuery(“#cat :selected”).val(); var text = jQuery(“#cat :selected”).text(); jQuery(‘#replace’).html(text); jQuery.ajax({ type: … Read more

Why none of the plugins that have ajax doesn’t work in my website?

Taking a look at your code, I noticed one huge issue (also pointed out by Milo). Your script tags (the ones that pull in jQuery and other scripts) are wrong. For example, this: <script data-djssrc=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/jquery.min.js” type=”text/djs”></script> <script data-djssrc=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/jquery.nivo.slider.js” type=”text/djs”></script> <script data-djssrc=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/mediaflux.js” type=”text/djs”></script> Should really be this: <script src=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/jquery.min.js” type=”javascript”></script> <script src=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/jquery.nivo.slider.js” type=”javascript”></script> <script src=”http://2barnamenevis.com/wp-content/themes/mediaflux2/js/mediaflux.js” type=”javascript”></script> … Read more

making sense of admin-ajax

The function that returns the contents of that ajax request will not be in admin-ajax.php. That is the core WordPress file that handles ajax requests, but it is not used for specifying the data returned by ajax functions in themes / plugins. In the theme (somewhere), will be a line that looks like this: add_action(‘wp_ajax_myajax-submit’, … Read more

Using wp_handle_upload and media_handle_sideload with ajax

Uploading files in ajax is a bit tricky because it is not possible to upload files using the browser’s XMLHttpRequest object so you need to use some kind of Ajax upload plugin. Also wp_handle_upload() is not what your using in your code its media_handle_sideload() wp_handle_upload() – should be used for file uploads (input file field) … Read more