如何在 wordpress 自定义休息路由回调中将代码分离到另一个函数?
How to separate code to another functions in wordpress custom rest route callback?
我的 wordpress 中有自定义休息路线:
add_action( 'rest_api_init', function () {
register_rest_route( 'site', '/test-route', array(
'methods' => 'POST',
'callback' => 'handle_webhook',
) );
} );
一切正常,但我现在正在进行重构,我想更改以前的代码:
function handle_webhook( $request ) {
//
// processing
//
return new WP_REST_Response('Done, bro!', 200);
die();
}
进入:
function another_function_1( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 1, bro!', 200);
die();
}
function another_function_2( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 2, bro!', 200);
die();
}
function handle_webhook( $request ) {
if ($something) {
another_function_1( $request );
} else {
another_function_2( $request );
}
return new WP_REST_Response('Done, bro!', 200);
die();
}
所以总的来说,我想将代码分离到另一个函数中。问题是我总是收到主函数的响应 ('Done, bro!', 200
)。
当我将 return
放入 if 语句时它起作用了:
if ($something) {
return new WP_REST_Response('works here!', 200);
} else {
return new WP_REST_Response('works also here when $something is !true', 200);
}
但是从另一个函数我可以return回复。
我怎样才能做到这一点?
您需要 return function ()
:
function another_function_1( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 1, bro!', 200);
/** die(); */
}
function another_function_2( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 2, bro!', 200);
/** die(); */
}
function handle_webhook( $request ) {
if ($something) {
return another_function_1( $request ); /** Added return */
} else {
return another_function_2( $request ); /** Added return */
}
/** With the if {} else {} above, this code will never be reached.
/** return new WP_REST_Response('Done, bro!', 200); */
/** die(); */
}
否则,它只会继续超出函数调用。
此外,在 return 之后不需要 die();
,代码是静音的,因为代码中的过程永远不会到达那个点。
试试这样的东西:
function handle_webhook( $request ) {
if ($something) {
$result = another_function_1( $request );
} else if ($somethingElse) {
$result = another_function_2( $request );
} else {
$result = new WP_REST_Response('Default', 200);
}
return $result;
}
Because of the way HTTP and/or HTTPS works is that, you can only send one response (for one request), but you could use JSON
array or something, to workaround that limitation and .
我的 wordpress 中有自定义休息路线:
add_action( 'rest_api_init', function () {
register_rest_route( 'site', '/test-route', array(
'methods' => 'POST',
'callback' => 'handle_webhook',
) );
} );
一切正常,但我现在正在进行重构,我想更改以前的代码:
function handle_webhook( $request ) {
//
// processing
//
return new WP_REST_Response('Done, bro!', 200);
die();
}
进入:
function another_function_1( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 1, bro!', 200);
die();
}
function another_function_2( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 2, bro!', 200);
die();
}
function handle_webhook( $request ) {
if ($something) {
another_function_1( $request );
} else {
another_function_2( $request );
}
return new WP_REST_Response('Done, bro!', 200);
die();
}
所以总的来说,我想将代码分离到另一个函数中。问题是我总是收到主函数的响应 ('Done, bro!', 200
)。
当我将 return
放入 if 语句时它起作用了:
if ($something) {
return new WP_REST_Response('works here!', 200);
} else {
return new WP_REST_Response('works also here when $something is !true', 200);
}
但是从另一个函数我可以return回复。
我怎样才能做到这一点?
您需要 return function ()
:
function another_function_1( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 1, bro!', 200);
/** die(); */
}
function another_function_2( $request ) {
//
// processing
//
return new WP_REST_Response('Done from 2, bro!', 200);
/** die(); */
}
function handle_webhook( $request ) {
if ($something) {
return another_function_1( $request ); /** Added return */
} else {
return another_function_2( $request ); /** Added return */
}
/** With the if {} else {} above, this code will never be reached.
/** return new WP_REST_Response('Done, bro!', 200); */
/** die(); */
}
否则,它只会继续超出函数调用。
此外,在 return 之后不需要 die();
,代码是静音的,因为代码中的过程永远不会到达那个点。
试试这样的东西:
function handle_webhook( $request ) {
if ($something) {
$result = another_function_1( $request );
} else if ($somethingElse) {
$result = another_function_2( $request );
} else {
$result = new WP_REST_Response('Default', 200);
}
return $result;
}
Because of the way HTTP and/or HTTPS works is that, you can only send one response (for one request), but you could use
JSON
array or something, to workaround that limitation and .