Skip to content
Docs
Get RestroPress

Action

rpress_api_output_{$format}

Updated
On this page

Fires when an API response is requested in a format other than JSON or XML.

Runs in the default branch of the format switch in RPRESS_API::output(), so a callback hooked to rpress_api_output_{$format} is responsible for printing the whole response in that format. rpress_api_output_after fires afterwards, followed by rpress_die(). Combine with rpress_api_output_format to accept the extra format name.

Parameters#

ParameterTypeDescription
$arg1arrayThe response data to render.
$thisRPRESS_APIThe API instance.

Example#

function my_api_output_csv( $data, $api ) {
	header( 'Content-Type: text/csv; charset=utf-8' );
	$out = fopen( 'php://output', 'w' );
	foreach ( $data as $key => $value ) {
		fputcsv( $out, array( $key, wp_json_encode( $value ) ) );
	}
	fclose( $out );
}
add_action( 'rpress_api_output_csv', 'my_api_output_csv', 10, 2 );

Notes#

The hook name is built from the format string, for example rpress_api_output_csv. Not fired for 'json' or 'xml'.

Source#

Fired from:

  • includes/api/class-rpress-api.php (line 1523), in RPRESS_API::output()
  • includes/class-rpress-api.php (line 1124), in RPRESS_API::output()

See also#

Was this page helpful?