WP_Ajax Hook doesn’t execute this PHP code
You’re trying to use get_the_ID inside your callback function, but get_the_ID will only work inside the loop. You should send your post ID to your callback function explicitly instead.
You’re trying to use get_the_ID inside your callback function, but get_the_ID will only work inside the loop. You should send your post ID to your callback function explicitly instead.
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
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
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
Add data-post_id attribute to checkbox and fill it with corresponding post ID. On click on compare button (link) retrieve all post ids from checked checkboxes with JavaScript (jQuery) Redirect to the comparision page with post IDs in url as GET parameters. This is just one scenario how your task could be done. PS your question … Read more
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
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
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
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
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(); }