WordPress Ajax – looping data into a table

Have you actually added this action through functions.php? This might be of interest for you: https://developer.wordpress.org/reference/hooks/wp_ajax_action/ in your case the php function needs to be added as a hook: add_action(‘wp_ajax_fetch_data’, ‘DisplayInfo’) add_action(‘wp_ajax_nopriv_fetch_data’, ‘DisplayInfo’)

PHPUnit Ajax Serialization of ‘Closure’ is not allowed

Figured out that it was the @runTestsInSeparateProcesses annotation in the test class that was causing the error. Not sure why that would cause it. Once I removed it, the test completed successfully. ./vendor/bin/phpunit –group ajax Installing… Running as single site… To run multisite, use -c tests/phpunit/multisite.xml Not running ms-files tests. To execute these, use –group … Read more

Call pre_get_posts inside ajax

Not the way you’re doing it, no. When the pre_get_post action is run any WP_Query queries run will be modified by the callback. Actions are not persistent, so when you run add_action() inside an AJAX callback that action is only going to be applied for that request, and you’re not running any queries in the … Read more

notify users when changed database

I don’t want Ajax request to be sent every 2 seconds. Is there a way to send me a notification only if the database changes in a certain meta? You can’t watch the database for changes like this. The most you could do is add filters on post meta changes on the PHP side to … Read more

PHP cookie not set within AJAX call

Apparently I was missing the time() attribute. So when adding this to the expire option in the setCookie function, the cookie is being set. Code: setcookie(‘user-zipcode’, $zipcode, time() + MONTH_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN); setcookie(‘delivery-today’, $cookieVal, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);

wp_enqueue_scripts with JS script as a string

Unfortunately, wp_enqueue_script doesn’t work that way — the second parameter is for a URL as per the documentation. But you could use wp_add_inline_script() (if there’s an enqueue’d script to attach it to) or, as in olden times, just hook in to wp_head or wp_footer.