当我向我的WordPress Rest API发送发布请求以创建发布时,发布就很好了;但是如果在此发布请求之后添加动作以触发功能,则会收到错误500。
无论我使用什么钩子,都会发生错误:rest_insert_{$this->post_type}
,save_post
或transition_post_status
...
即使我通过邮递员发送请求,也会出现问题。
如何解决此错误并在发布请求后触发我的功能?
更新
这是我使用Nuxt(vuej)发送请求的代码:
async send() {
if (this.$refs.form.validate()) {
let payload = {};
payload = {
first_name: this.first_name,
last_name: this.last_name,
budget: this.budget,
email: this.email,
message: this.message
};
try {
await axios({
method: "post",
url: this.baseUrl,
data: {
title: this.first_name + " " + this.last_name,
fields: payload,
status: "private"
}
});
} catch (error) {
console.error(error);
}
} else {
window.scrollTo(0, 400);
}
}
还有我的WP functions.php
add_action( 'save_post', 'custom_send_admin_email', 10, 3 );
function custom_send_admin_email( $post_id, $post, $update ) {
// Only want to set if this is a new post!
if ( $update ){
return;
}
// Only set for post_type = post!
if ( 'message' !== $post->post_type ) {
return;
}
// Consider sending an email here.
$to = 'adebord@agencedebord.com';
$subject = 'The subject';
$body = 'Nom : ' . $post['acf']['nom'];
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}