Output multi-steps form results in same page

You will need a function that will handle the sent data from the form. Then you have to attach this function to the wp_ajax_nopriv_[your_action_name] hook. function process_form() { // process $_POST data, prepare $output to return // // send: array( ‘success’ => true, ‘data’ => $data ) wp_send_json_success( $data ); wp_die(); } // add_action( ‘wp_ajax_my_action’, … Read more

How to continuously send feedback via AJAX responses to my client?

wp_send_json just does a basic data sanitization under the hood using wp_json_encode function. So you can use PHP’s built-in functionality json_encode the problem that you’re going to have is keeping the connection alive, If you’re not going to have a lot of visitors you can block the PHP thread by simply running a while true … Read more

How can I wp_send_json data?

It seems wp_send_json_success works! I just did something very wrong with it. When you hit the AJAX endpoint, at the end of the logic, simply: wp_send_json_success( [‘next_step’ => ‘enpoint_2’], 200 ); This will send an object to JS: Object{next_step: ‘endpoint_2’} Which you can then use to further your process.

Get post content and show it in a div

OK, I managed to make it work, here’s how i did : Jquery part : jQuery(“.post-link”).click(function(){ var post_id = jQuery(this).data(‘id’); jQuery.post( ajaxurl, { ‘action’: ‘load_post_content’, ‘the_ID’: post_id }, function(response){ jQuery(‘#the-post-content’).html(response); } ); return false; }); And the function.php part : add_action( ‘wp_ajax_load_post_content’, ‘load_post_content’ ); add_action( ‘wp_ajax_nopriv_load_post_content’, ‘load_post_content’ ); function load_post_content() { $the_post_id = $_POST[‘the_ID’]; $args … Read more

How to check nonce lifetime value of plugins?

Check codex info about Nonces life time here. Here is a quick code that will echo life time of nonces in footer of your site’s front-end as html comment. Put it in your functions.php file. $n = “”; add_filter(‘nonce_life’, ‘wptuts_change_nonce_hourly’); function wptuts_change_nonce_hourly( $nonce_life ) { global $n ; $n = $nonce_life; return $nonce_life; } // … Read more

javascript ajax and nonce

Okay, so I found a working solution which is as simple as I thought the whole process should be. The confusing WordPress codex made things harder really. The creation and naming of the nonce is as simple as: wp_create_nonce( ‘example’ ); This is passed though AJAX and localisation of the script to the PHP. Then … Read more

How to disable drag-and-drop upload function in Media Library?

Instead of removing upload script you can make the upload error out if the user does not have admin privileges. Like this- function tomjn_only_upload_for_admin( $file ) { if ( ! current_user_can( ‘manage_options’ ) ) { $file[‘error’] = ‘You can\’t upload images without admin privileges!’; } return $file; } add_filter( ‘wp_handle_upload_prefilter’, ‘tomjn_only_upload_for_admin’ ); For other possible … Read more