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

Identical wp_rest nonce returned from rest_api

Despite you already figured it out or found a solution, since this is a follow-up to the other question, I thought it’d be benefecial to post this answer, so hopefully it helps you. =) WordPress uses the native setcookie() function in PHP for setting the authentication cookies, and $_COOKIE for reading the cookies values. But … Read more

how to send Ajax request in wordpress backend

When you enqueue or localize a script you’re doing it specifically for the front end or the admin. If you want to enqueue or localize a script in both, you have to specifically do it for both. This is used to enqueue/localize for the front end add_action( ‘wp_enqueue_scripts’, ‘your_function_front’ ); your_function_front() { wp_localize_script(‘scripts’, ‘myAjax’, array( … Read more