Title: WP_REST_Server::get_data_for_route
Published: December 9, 2015
Last modified: February 24, 2026

---

# WP_REST_Server::get_data_for_route( string $route, array $callbacks, string $context ): array|null

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#wp--skip-link--target)

Retrieves publicly-visible data for the route.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#parameters)󠁿

 `$route`stringrequired

Route to get data for.

`$callbacks`arrayrequired

Callbacks to convert to data.

`$context`stringoptional

Context for the data. Accepts `'view'` or `'help'`. Default `'view'`.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#return)󠁿

 array|null Data for the route, or null if no publicly-visible data.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#source)󠁿

    ```php
    public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
    	$data = array(
    		'namespace' => '',
    		'methods'   => array(),
    		'endpoints' => array(),
    	);

    	$allow_batch = false;

    	if ( isset( $this->route_options[ $route ] ) ) {
    		$options = $this->route_options[ $route ];

    		if ( isset( $options['namespace'] ) ) {
    			$data['namespace'] = $options['namespace'];
    		}

    		$allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;

    		if ( isset( $options['schema'] ) && 'help' === $context ) {
    			$data['schema'] = call_user_func( $options['schema'] );
    		}
    	}

    	$allowed_schema_keywords = array_flip( rest_get_allowed_schema_keywords() );

    	$route = preg_replace( '#\(\?P<(\w+?)>.*?\)#', '{$1}', $route );

    	foreach ( $callbacks as $callback ) {
    		// Skip to the next route if any callback is hidden.
    		if ( empty( $callback['show_in_index'] ) ) {
    			continue;
    		}

    		$data['methods'] = array_merge( $data['methods'], array_keys( $callback['methods'] ) );
    		$endpoint_data   = array(
    			'methods' => array_keys( $callback['methods'] ),
    		);

    		$callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;

    		if ( $callback_batch ) {
    			$endpoint_data['allow_batch'] = $callback_batch;
    		}

    		if ( isset( $callback['args'] ) ) {
    			$endpoint_data['args'] = array();

    			foreach ( $callback['args'] as $key => $opts ) {
    				if ( is_string( $opts ) ) {
    					$opts = array( $opts => 0 );
    				} elseif ( ! is_array( $opts ) ) {
    					$opts = array();
    				}
    				$arg_data             = array_intersect_key( $opts, $allowed_schema_keywords );
    				$arg_data['required'] = ! empty( $opts['required'] );

    				$endpoint_data['args'][ $key ] = $arg_data;
    			}
    		}

    		$data['endpoints'][] = $endpoint_data;

    		// For non-variable routes, generate links.
    		if ( ! str_contains( $route, '{' ) ) {
    			$data['_links'] = array(
    				'self' => array(
    					array(
    						'href' => rest_url( $route ),
    					),
    				),
    			);
    		}
    	}

    	if ( empty( $data['methods'] ) ) {
    		// No methods supported, hide the route.
    		return null;
    	}

    	return $data;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/class-wp-rest-server.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/rest-api/class-wp-rest-server.php#L1606)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/rest-api/class-wp-rest-server.php#L1606-L1686)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#related)󠁿

| Uses | Description | 
| [rest_get_allowed_schema_keywords()](https://developer.wordpress.org/reference/functions/rest_get_allowed_schema_keywords/)`wp-includes/rest-api.php` |

Get all valid JSON schema properties.

  | 
| [rest_url()](https://developer.wordpress.org/reference/functions/rest_url/)`wp-includes/rest-api.php` |

Retrieves the URL to a REST endpoint.

  |

| Used by | Description | 
| [WP_REST_Server::get_data_for_routes()](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_routes/)`wp-includes/rest-api/class-wp-rest-server.php` |

Retrieves the publicly-visible data for routes.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_server/get_data_for_route/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_server%2Fget_data_for_route%2F)
before being able to contribute a note or feedback.