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

Show special field when correct shipping is chosen

You need to setup the change event on the field: $(‘select.shipping-field’).on(‘change’, function()) { $.post( ajaxurl, { action: ‘shipping_special’, data: ‘your data here..’ }, function(response){ // The response should contain your special field HTML } ); }); You can pass the current shipping select option Then your theme’s functions.php file needs to have the action to … Read more

Add ajax load more function using WP API

I’ve solved the problem, I was made a mistake with the variable pull_page that is inside the click event. I’ve moved the variable outside the event and now all works fine. Here is the updated code! var pull_page = 1; var jsonFlag = true; $(‘#load-more’).on(‘click’, function(e){ e.preventDefault(); if(jsonFlag){ pull_page++; $.getJSON(“/u/wp-json/portfolio/all-posts?page=” + pull_page, function(data){ if(data.length){ pull_page++; … Read more

Ajax posts filter by date, comments, top views, top likes

I haven’t tested the code, but this should help. option elements do not have a name attribute: <option name=”top-views” value=”top-views”>Top Views</option> <option name=”top-likes” value=”top-likes”>Top Likes</option> And therefore $_POST[‘top-views’] for example, is not going to be set, or that the value is not the top-views in the above option. Use the proper option markup Based on … 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

Different uniqid when calld in wp_localize_script and shortcode

Inside the __construct() you can put this global $shortcode_id; $shortcode_id = 0; in the display_gallery_shortcode() function you can do this <?php global $shortcode_id; $shortcode_id ++; $settings_arr = array();//Here is your settings for each shortcode ob_start(); ?> <script> if(!my_plugin_data) var my_plugin_data = {} my_plugin_data[<?php echo $shortcode_id ?>] = <?php echo json_encode($settings_arr) ?> </script> <div>MY SHORTCODE CONTENT … Read more

AJAX button run function

In order to make this work as you expect, you need to add the following lines in your constructor (depending on the requirements you have) add_action( ‘wp_ajax_myfunction’, array($this, ‘myfunction’) ); add_action( ‘wp_ajax_nopriv_myfunction’, array($this, ‘myfunction’) ); Note that wp_ajax_nopriv_myfunction executes for users that are not logged in. The other executes for logged in users only. You … 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

class click counter save number

Made ajax call as a function to avoid the Console Error 400 Post. It opens the channel and I was able to get the parameters passed. counter.js jQuery(document).on(‘click’,’.counter’,function(e){ testAjax(the_ajax_script.ajaxurl); }); function testAjax(link) { $.ajax({ url: link, type: ‘post’, data: { ‘action’: ‘set_button_count’, ‘increase’ : ‘1’ }, success: function(data) { jQuery(‘.count’).html(data); } }); } In functions.php … Read more