Can’t publish post using ajax

To those who are experiencing the same issue, there are few steps to take to determine the culprit. Create posts manually in the WP Dashboard area (set it to future) .. and manually publish (look for the error in the log). step 1 and trash the post, in the Trash, try reverting that post (this … Read more

WordPress Admin AJAX Serialize

Instead of relying on bespoke/custom serialising, and the ancient admin AJAX, consider using JSON and the REST API, e.g.: add_action( ‘rest_api_init’, function () { register_rest_route( ‘ramkumar/v1’, ‘/test/’, array( ‘methods’ => ‘POST’, ‘callback’ => ‘ramkumar_rest_test’ ) ); } ); function ramkumar_rest_test( $request ) { $reviewer_notes = $request[‘reviewer_notes’]; $result = ….; return $result; // gets sent to … Read more

admin-ajax.php slow website, how to fix

If you want to save the generated css to a file, take a look at file_get_contents() and file_put_contents() native PHP functions. https://www.php.net/manual/en/function.file-put-contents.php You can also find some Q&A’s on the topic both in SO and WPSE, e.g. https://stackoverflow.com/questions/25559913/write-from-wordpress-plugin-to-text-file-with-php You can then enqueue the created css file inside wp_enqueue_scripts like any other css file. You might … Read more

wp_localize_script not working on ajax response

Please try below code : script.js jQuery(function($) { var i; for(i=1;i<21;i++){ $(‘body’).on(‘change’, ‘#file’+i, function() { var file_data = $(this).prop(‘files’)[0]; var id = $(this).attr(‘id’); var form_data = new FormData(); form_data.append(‘file’, file_data); form_data.append(‘action’, ‘file_upload’); jQuery.ajax({ url: pw_script_vars.ajax_url, type: ‘POST’, contentType: false, processData: false, data: form_data, success: function (response) { alert( response.alert ); } }); }); } }); … Read more

How to insert a record by clicking on the link using AJAX

If this is your request data for an AJAX POST: data: { action: ‘gs_order’, nonce_data: ajax_vars.nonce, product_groups: $(this).data(“pgid”), products: $(this).data(“pid”) }, Then you access product_groups and products like this: $data = [ ‘product_groups’ => $_POST[‘product_groups’], ‘products’ => $_POST[‘products’], ]; Just make sure you sanitize and escape the data as required. If they’re going to be … Read more

Get Author Post on author.php with AJAX

Author.php means you are looking at an archive of the author already, so why would you need to display posts by that author? Regardless, you can get the author ID from get_queried_object, which returns the whole user object. <?php $curauth = get_queried_object(); if($curauth && isset($curauth->ID)){ //…trigger ajax to pass ID via the data object } … Read more

wordpress and add actions to button by coding

Try below method. <a id=”view_site_description” href=”https://wordpress.stackexchange.com/questions/365101/javascript:void(0);”>View Our Site Description</a> add_action( ‘wp_footer’, ‘add_js_to_wp_footer’ ); function add_js_to_wp_footer(){ ?> <script type=”text/javascript”> jQuery(‘#view_site_description’).click(function(){ jQuery.ajax({ type: ‘POST’, url: ajaxurl, data: {“action”: “myfilter”}, success: function(data){alert(data);} }); return false; }); </script> <?php }

Unable to parse JSON response from wp_send_json_success [closed]

In short, you should use response.data.messages. And that’s because wp_send_json_success() will send a JSON response (an object) with the property data set to whatever that you passed to the function. wp_send_json_success( 123 ); // In JS, response.data would be an integer. I.e. response.data = 123 wp_send_json_success( array( ‘foo’ => ‘bar’ ) ); // In JS, … Read more