Ajax insert or update data

I’m not entirely sure you want to call exit at the end of that function, since exit terminates all further PHP execution. This is likely the cause of your troubles. http://php.net/manual/en/function.exit.php That said, you can always test for the result of your queries by setting a variable to be the result, and then sending that … Read more

Nonce doesn’t validate in nopriv call

Use check_ajax_referer: https://codex.wordpress.org/Function_Reference/check_ajax_referer //Check nonce if ( !check_ajax_referer( ‘nonce-action-name’, ‘_wpnonce’, false ) ){ echo __(‘Ajax Security Check’, ‘yourtextdomain’); die(); }

AJAX call broken after actualization

OK, I found out that my DB encoding was LATIN2 and wordpress forced UTF-8 in all calls after the update. When I changed my database’s encoding to UTF-8 it all started to work. I also used: $comment = mb_convert_encoding($comment, “UTF-8”); just to be shure.

Ajax function on #publish only saves as draft – how to make it publish?

Perhaps this solution will work: var flag_ok = false; $(‘#publish’).on(‘click’, function (e) { if ( ! flag_ok ) { e.preventDefault(); var url = shiftajax.ajaxurl; var shift = $(‘#post_ID’).val(); var data = { ‘action’: ‘wpaesm_check_for_schedule_conflicts_before_publish’, ‘shift’: shift, }; $.post(url, data, function (response) { if( response.action == ‘go’ ) { // there aren’t any scheduling conflicts, so … Read more

wp_update_post onclick button using ajax

No no no! Never POST or link directly to a custom PHP file – WordPress won’t be loaded, and to load it manually yourself means making huge assumptions about the file hierarchy. Use the ajax API, which exists specifically for this reason: $.ajax({ url: “<?php echo esc_js( admin_url( ‘admin-ajax.php’ ) ) ?>”, type: “POST”, data: … Read more

use jQuery.load() to include a php file in a div, wp_query() is part of php file

Using AJAX in WordPress is fundamentally pretty simple. You need a function that handles the AJAX request, and a couple of add_action calls to map that function to the action you pass to admin-ajax.php, in this case, some_action: function wpd_ajax_function() { get_template_part( ‘my-content’ ); wp_die(); } add_action( ‘wp_ajax_some_action’, ‘wpd_ajax_function’ ); add_action( ‘wp_ajax_nopriv_some_action’, ‘wpd_ajax_function’ ); This … Read more

Retrieve JSON file from JS trough php

I’m not convinced this is the cleanest way to do it but at least it work until I find something more efficient: function json_gmap(){ $get_file = file_get_contents(get_template_directory_uri() . ‘/data/my.json’); $json_to_array = json_decode($get_file,true); die (json_encode($json_to_array)); } add_action(‘wp_ajax_json_gmap’, ‘json_gmap’); add_action(‘wp_ajax_nopriv_json_gmap’, ‘json_gmap’); Basically I decode my Json file to an array and the re-encode it to have it … Read more