JS working when used normally but not in wordpress

Solved. The problem was that mojs has to be included between the <body></body> tags, and the wp_enqueue_script() function that enqueued mojs needed an empty string as argument just before the ‘true’ parameter (I forgot to specify that there was no required version number of that script). So, changing wp_enqueue_script(‘mojs’, ‘https://cdn.jsdelivr.net/mojs/latest/mo.min.js’, array(‘jquery’), true); wp_enqueue_script(‘favjs’, $favbtnscript, array(‘jquery’, … Read more

jQuery ajax method does not return data

Going by the jQuery docs for the call you’re using, it looks like maybe you want the success callback instead of complete. success gets passed the JSON directly, as you expect, but complete gets passed different stuff that you would have to look inside. Refer to the docs for more if you’re sure you want … Read more

Problem when sending file via ajax

Let say you have a form: <form name=”post” id=”post-form” action=”” method=”post” enctype=”multipart/form-data”> <input type=”text” name=”first_name”> <input type=”text” name=”last_name”> <input id=”file” type=”file” name=”featured_image”> </form> Now you have to pass the FormData object to ‘data’ parameter in ajax jQuery(document).ready(function($) { $(‘#post-form’).submit(function(e) { e.preventDefault(); var form = $(this); file_data = $(‘#file’).prop(‘files’)[0]; //get the file form_data = new FormData(); … Read more