Skip to content

Instantly share code, notes, and snippets.

@ckchaudhary
Last active February 25, 2025 06:11
Show Gist options
  • Save ckchaudhary/274286e3745df5e7cce9469da4be7aa6 to your computer and use it in GitHub Desktop.
Save ckchaudhary/274286e3745df5e7cce9469da4be7aa6 to your computer and use it in GitHub Desktop.
Add url parameters to a GET request made using javascript fetch, in the context of querying wordpress rest api custom endpoints.
<?php
// Send errors like this:
$update_status = $widget_obj->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 );
}
<?php
define( 'FRONTPAGE_BUDDY_REST_ROUTE_NAMESPACE', 'frontpage-buddy/v1' );
...
...
$data = apply_filters(
'frontpage_buddy_script_data',
array(
'config' => array(
'rest_url_base' => rest_url( FRONTPAGE_BUDDY_REST_ROUTE_NAMESPACE ),
'rest_nonce' => wp_create_nonce( 'wp_rest' ),
),
...
...
'object_type' => '',
'object_id' => 0,
)
);
wp_add_inline_script(
'frontpage-buddy-editor',
'var FRONTPAGE_BUDDY = ' . wp_json_encode( $data ) . ';',
'before'
);
let apiUrl = new URL(FRONTPAGE_BUDDY.config.rest_url_base + '/status', window.location.origin);
let data = {
'object_type' : FRONTPAGE_BUDDY.object_type,
'object_id' : FRONTPAGE_BUDDY.object_id,
};
Object.keys(data).forEach(key => apiUrl.searchParams.append(key, data[key]));
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.message || 'Server error occurred');
});
}
return response.json();
})
.then(responseJSON =>{
if (!responseJSON || !responseJSON.success) {
throw new Error(responseJSON.message || 'Invalid response format');
}
$widget.removeClass( 'loading' );
...
$widget.find('.widget-settings').html( responseJSON.data.html );
...
})
.catch(error => {
$widget.removeClass('loading');
console.error('Error:', error);
$widget.find('.widget-settings').html(`<div class="response alert-error">${error.message}</div>`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment