I have 1 problems that I've been trying to solve. I'm trying to follow some examples when I search to do this, and I'm not very successful...been trying to follow stuff like this... https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
- how do you pass a parameter/args in your callback
this code below does work. The logic I want is that when you pick a day (Y-m-d) format, it return all times available from a start time to an end time chosen by the admin user in wordpress.
here is my rest_api_init
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/day/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
here is the callback function
function my_awesome_func($request) {
$selected_day = '2020-05-05';
$startTime = esc_attr( get_option( 'start_time' )); //this works can hard coded as (10:30)
$endTime = esc_attr( get_option( 'end_time' )); // also works can hard coded (20:00)
$minutesApart = esc_attr( get_option( 'minutes_between')); //also works hard coded (15)
$data = array();
$exploded_daydate = explode('-', $selected_day);
$start_time_raw = $startTime;
$end_time_raw = $endTime;
$start_time_Exploded = (explode(":",$start_time_raw));
$end_time_Exploded = (explode(":",$end_time_raw));
$loopStart = mktime($start_time_Exploded[0], $start_time_Exploded[1], 0, $exploded_daydate[1], $exploded_daydate[2], $exploded_daydate[0]);
$loopEnd = mktime($end_time_Exploded[0], $end_time_Exploded[1], 0, $exploded_daydate[1], $exploded_daydate[2], $exploded_daydate[0]);
$i=0;
while ($loopStart <= $loopEnd) {
$data[$i] = date("Y-d-m h:i:s",$loopStart);
$loopStart = $loopStart + ($minutesApart *60);
$i++;
}
return $data;
}
right now I hard code ($selected_day = '2020-05-05';) but I want this to work that the endpoint url will work by accepting the (Y-m-d) format at the end of the URL endpoint. I just cant figure it out... new to all of this....
any help would be nice.