I have been trying to implement a custom end point that inserts data from my mobile application into this database. However, I cant seem to find the correct way to do it. Some places used #wpdp->insert, while others used
$wpdb->get_results($query) and query would be "INSERT INTO 'wp_woocommerce_vendor' .....
none has worked for me. I get an error
There has been a critical error on your website.
Learn more about debugging in WordPress.
if possible, is there a way for me to check responses? such as, if insert is succesfull.
return $data['status']='Database added Successfully.';
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'methods' => 'POST',
'callback' => 'custom_phrase',
)
);
}
function custom_phrase() {
global $wpdb;
$data = array();
$parameters = $request_data->get_params();
$vendor_vendor_id = $parameters['vendor_vendor_id'];
$vendor_product_id = $parameters['vendor_product_id'];
$vendor_product_name = $parameters['vendor_product_name'];
$vendor_product_qty = $parameters['vendor_product_qty'];
$vendor_product_unit_price = $parameters['vendor_product_unit_price'];
$vendor_product_amount = $parameters['vendor_product_amount'];
$vendor_percent = $parameters['vendor_percent'];
$vendor_amount = $parameters['vendor_amount'];
$vendor_order_id = $parameters['vendor_order_id'];
$vendor_order_date = $parameters['vendor_order_date'];
$vendor_send_money_status = $parameters['vendor_send_money_status'];
$vendor_send_money_date = $parameters['vendor_send_money_date'];
$vendor_product_delivared = $parameters['vendor_product_delivared'];
$table = 'wp_woocommerce_vendor';
$data = array(
'vendor_vendor_id' => $vendor_vendor_id,
'vendor_product_id' => $vendor_product_id,
'vendor_product_name' => $vendor_product_name,
'vendor_product_qty' => $vendor_product_qty,
'vendor_product_unit_price' => $vendor_product_unit_price,
'vendor_product_amount' => $vendor_product_amount,
'vendor_percent' => $vendor_percent,
'vendor_amount' => $vendor_amount,
'vendor_order_id' => $vendor_order_id,
'vendor_order_date' => $vendor_order_date,
'vendor_send_money_status' => $vendor_send_money_status,
'vendor_send_money_date' => $vendor_send_money_date,
'vendor_product_delivared' => $vendor_product_delivared
);
$wpdb->insert( $table, $data );
$data['status']='Database added Successfully.';
return rest_ensure_response( 'Hello World! This is my first REST API' );
}
Sources: https://www.tychesoftwares.com/creating-custom-api-endpoints-in-the-wordpress-rest-api/ https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/