Answer the question
In order to leave comments, you need to log in
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;
}
}
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');
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");
});
});
});
Answer the question
In order to leave comments, you need to log in
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
Look in server logs (Apache, for example).
will not be processed by the interpreter, are you sure that such characters can be there?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question