Check ajax triggered from front-end or from dashboard

Ajax request in WordPress is always performed in admin side, so is_admin() returns always true. If you need to know where the ajax request come from you need to check where the script is executed, not where the script send the ajax request. For example: add_action(‘wp_enqueue_scripts’, ‘cyb_enqueue_scripts’); function cyb_enqueue_scripts() { //Register the script. wp_register_script(‘my-script’, get_stylesheet_directory_uri(). … Read more

Scrolling Posts with Ajax

Well first of all, that tutorial is “doing it wrong” in the AJAX department. WordPress has an AJAX API, which should be used in this case. That said, the basis of your problem is this: $args = array( ‘posts_per_page’ => $numPosts, ‘paged’ => $page ); Those are your query arguments for the posts you’re requesting. … Read more

Simplest ajax form not working when it should

I have been investing some quality time (and headaches) with WP ajax so I understand where you are coming from. In all honesty, it could be anything from an error in your JS code or something completely bizarre, like: add_action(‘wp_ajax_UpdateMeta’, ‘UpdateMeta’); Should be changed to: add_action(‘wp_ajax_updatemeta’, ‘UpdateMeta’); … to remove the capitals because by some … Read more

Dequeue script to prevent javascript event conflict on wordpress child theme

First of all, to resolve the javascript conflict I’ve set up a simple tl_custom.js under my theme js/ folder, with the following code jQuery(document).ready(function($) { // Remove handler set by themes/Divi/js/custom.js at line 2642 $( ‘a[href*=#]:not([href=#])’ ).off(); }); Then I add the script with the following code in functions.php function tl_custom_scripts() { if ( ! … Read more

wordpress add_action() issue in ajax call

You’re sending your AJAX request to the wrong place. Of course calling functions.php directly will get you undefined function errors, because the WordPress API isn’t loaded by functions.php ( it’s the other way round ). A car doesn’t make fuel go forward, fuel makes a car go forward, simply getting in the car won’t make … Read more

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

Ajax button not working

As for your PHP not being executed – the action parameter in your JavaScript AJAX call needs to match the hook name in your add_action call. Take a look at the example below. I haven’t tested it but it should work. I have simplifed the AJAX call, feel free to modify it to suit your … 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(); }