update( $new_data ); if ( ! $update_status['status'] ) { // validation erorrs! return rest_send_error( $update_status['message'] ); } // Or, like this: if ( ! $widget_type_obj->is_enabled_for( $object_type, $object_id ) ) { return rest_send_error( __( 'Widget not available.', 'frontpage-buddy' ), 500 ); } // Send successful response like this: return rest_send_response( true, array( 'html' => $html ) ); // Or, like this: return rest_send_response( true, null, __( 'Updated', 'frontpage-buddy' ) ); /** * Standard success response. * * @param boolean $success Whether the request was successful. * @param mixed $data Data to return. * @param string $message Message to return. * @param integer $status HTTP status code. Default 200. * @return \WP_REST_Response */ function rest_send_response( $success = true, $data = null, $message = '', $status = 200 ) { $response = array( 'success' => $success, 'message' => $message, 'data' => $data, ); return new \WP_REST_Response( $response, $status ); } /** * Standard error response. * * @param string $message Message to return. * @param integer $status HTTP status code. Default 400. * @param array $additional_data Additional data to return. * @return \WP_REST_Response */ function rest_send_error( $message, $status = 400, $additional_data = array() ) { $data = array_merge( array( 'success' => false, 'message' => $message, ), $additional_data ); return new \WP_REST_Response( $data, $status ); }