B
B
BonBon Slick2017-01-27 11:06:30
WordPress
BonBon Slick, 2017-01-27 11:06:30

Ajax wp_update_post() giving 500 error?

An Ajax request comes in, to update the post, this is the file code for the update, I try to do this:

$product_id = $_POST['product_id'];
$product_new_menu_order = $_POST['product_new_menu_order'];

$product = array(
  'ID'           => $product_id ,
  'menu_order' => $product_new_menu_order,
  );

$post_id = wp_update_post( $product, true );
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
  foreach ($errors as $error) {
    echo $error;
  }
}


Tried hooks:
function update_my_product() {
  $product = array(
    'ID'           => $product_id ,
    'menu_order' => $product_new_menu_order,
    );
  wp_update_post( $product );
  wp_die();
}

add_action('wp_ajax_update_product', 'update_my_product');
add_action('wp_ajax_nopriv_update_product', 'update_my_product');


JS - Ajax:

jQuery(document).ready(function($) {

  $(document).on('change', '#menu_order', function(event) {

    var plugin_url = $(this).data('url'),
    product_id = $(this).data('product-id'),
    product_new_menu_order = $(this).val();

    $.ajax({
      url: plugin_url,
      type: 'POST',
      data: {
        'action': 'update_product',
        'product_id': product_id,
        'product_new_menu_order': product_new_menu_order,
      },
    })
    .done(function(data) {
      console.log(data);
      console.log("success");
    })
    .fail(function() {
      console.log("error");
    });


  });
});


I look in the Google Chrome XHR console, 500 errors and that's it, there is no preview.
I don't understand why it throws an error?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WP Panda, 2017-01-27
@BonBonSlick

in $product_id and $product_new_menu_order you pass the string and you need (int)$product_id and (int)$product_new_menu_order because
https://github.com/WordPress/WordPress/blob/f3a9d2...
https://github.com/WordPress /WordPress/blob/f3a9d2...
Plus it's not critical in this case, but you should always add a check for presence and cleaning

N
Nikolai Konyukhov, 2017-01-27
@heahoh

Look in server logs (Apache, for example).
will not be processed by the interpreter, are you sure that such characters can be there?

W
Wexter, 2017-08-17
@Nickname-no

www.ubnt.su/ubiquiti/powerbeam_m2-400.htm
wifi bridge, for wireless connection of two points

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question