注册用户时,我挂钩到user_register
钩子发送一些调用外部API。
add_action( 'user_register', 'create_user_on_api' );
在create_user_on_api
函数里面我使用wp_remote_post()
进行调用(nonce在那里,大量安全检查,但我省略了那些简洁)
function create_user_on_api( $user_id ) {
$user_create_response = wp_remote_post( "someurlgoeshere.api", $curl_args );
if ( is_wp_error( $user_create_response ) ) {
throw new Exception( 'wp error' );
} else {
$code = wp_remote_retrieve_response_code( $user_create_response );
$msg = wp_remote_retrieve_response_message( $user_create_response );
$body = wp_remote_retrieve_body( $user_create_response );
if ( $code !== 200 ) {
throw new Exceprion( 'error' );
}
}
}
这是它的要点。
问题是如下:使用任何错误句柄导致白屏死机,我的debug.log中记录错误,我想在我的管理仪表板上发出通知,即使api调用失败,用户也是在WP中创建的,您必须在API上手动创建它或重试。
我尝试回应的东西,自定义异常处理提到here,但到目前为止没有运气。
知道怎么做吗?我可以以任何方式挂钩管理员通知吗?
好的,找到了解决方案。一个hacky,但它的工作原理。
在api错误中,我在瞬态存储错误消息,其中有60秒的到期时间我创建了一个user_notices
函数,我挂钩在admin_notices
钩子上。在其中我检查两件事:如果我在用户屏幕上
$current_screen = get_current_screen();
if ( $current_screen->id === 'users' ) {
}
然后在里面我检查瞬态是否为空,然后将它们分配给$error_message
数组,我在foreach
中输出(如果我有多个错误消息)
if ( ! empty( $error_msg_array ) ) {
foreach ( $error_msg_array as $error_msg_key => $error_msg ) {
?>
<div class="error notice">
<p><?php printf( '<strong>Apigee error:</strong> %s', esc_html( $error_msg ) ); ?></p>
</div>
<?php
}
}
这似乎有效。