1

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 https://snag.gy/zNXUmv.jpg

My guess is that issue cause by something called nonce but i dont really understand what is nonce and how to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.