最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Why ajax doesn't work on certain wordpress hooks and reload the page instead?

matteradmin7PV0评论

I built an ajax function which works perfectly when i hook it to admin_head or admin_notices hooks but it doesn't work when i hook it to manage_posts_extra_tablenav hook, instead of doing the ajax call it just reload the page and the url of the opened page after page reload is

Request URL: http://localhost/wc/wp-admin/edit.php?s=&post_status=all&post_type=product&_wpnonce=c1a10340ac&_wp_http_referer=%2Fwc%2Fwp-admin%2Fedit.php%3Fs%26post_status%3Dall%26post_type%3Dproduct%26action%3D-1%26product_cat%26product_type%26stock_status%26paged%3D1%26action2%3D-1&action=-1&product_cat=&product_type=&stock_status=&paged=1&action2=-1

and that upper link do a 302 redirect to that link

http://localhost/wc/wp-admin/edit.php?s&post_status=all&post_type=product&action=-1&product_cat&product_type&stock_status&paged=1&action2=-1

This is my code, however both actions connected to the same function but the version on top works fine and the one on the other hook doesn't work, so i was wondering what is the reason and how to solve it?

add_action( 'manage_posts_extra_tablenav', 'apm_update_all_products_ajax' );
add_action('admin_notices', 'apm_update_all_products_ajax');

function apm_update_all_products_ajax() {
  $product_ids = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
  ?>
    <div class="apm_bulk_update_prices_wrap alignleft actions">
  <button class="button apm_update_prices" id="update_prices" onclick="apm_update_all_products()">Update All Products</button>
    <p id="apm_bulk_status">test</p>
    </div>
          <script type="text/javascript" >
            product_ids = <?php echo json_encode($product_ids); ?>;
            product_ids_n = product_ids.length;
            product_ids_step = 0;
      function apm_update_all_products() {
                product_id = product_ids[product_ids_step];
                    $.ajax({
                        type: "POST",
                        url: ajaxurl,
                        dataType: "JSON",
                        data: {
                                action: 'apm_update_single_product',
                                postId: product_id
                        },
                        success: function(lookup_data) {
                                product_ids_step++
                                $('#bulk_status').text('Updated ' + product_ids_step + ' of ' + product_ids_n );
                                if (product_ids_step < product_ids_n) {
                                    apm_update_all_products();
                                }
                        },
                        error: function(jqXHR, textStatus, errorThrown) {

                        }
                    })
      };

    </script>
  <?php
}

and here is a screenshot showing the place of the 2 hooks .jpg

Post a comment

comment list (0)

  1. No comments so far