注册用户时,我挂钩user_register
挂钩发送一些调用外部API。
add_action( 'user_register', 'create_user_on_api' );
我在create_user_on_api
函数内部使用wp_remote_post()
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,但到目前为止还没有运气。
知道怎么做吗?我能以任何方式挂钩管理员通知吗?
答案 0 :(得分:0)
好的,找到了解决方案。一个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
}
}
这似乎有效。