1

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/

  1. 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.

1 Answer 1

4

never mind... I figured it out... here is the solution I was looking for

add_action( 'rest_api_init', function () {
  register_rest_route( 'myplugin/v1', '/day/(?P<day>([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])))', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
    
  ) );
} );

    function my_awesome_func($request) {
        $selected_day = $request['day'];
      $startTime = esc_attr( get_option( 'start_time' ));
      $endTime = esc_attr( get_option( 'end_time' ));
      $minutesApart = esc_attr( get_option( 'minutes_between'));
      $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-m-d h:i:s",$loopStart);
              $loopStart = $loopStart + ($minutesApart *60);
              $i++;
              }
              return $data;
              }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.