【发布时间】:2022-01-25 19:42:03
【问题描述】:
以前我是这样做的:
function my_save_post($post_id, $post, $update)
{
$errors = new WP_Error('my_errors', 'Something wrong:');
// if having error
$errors->add('my_errors', 'some error message');
//...
//...
//...
if (count($errors->get_error_messages()) > 1) {
global $wpdb;
// set the status of the post to **pending** as its metas has some errors.
$wpdb->update($wpdb->posts, array('post_status' => 'pending'), array('ID' => $post->ID));
set_transient('my_errors', $errors, 10);
} else {
// publish the post and update the metas
}
}
add_action('save_post', 'my_save_post', 10, 3);
function my_notices()
{
if ($errors = get_transient('my_errors')): ?>
<div class="error">
<?php foreach ($errors->get_error_messages() as $error): ?>
<p><?php esc_html_e($error) ?></p>
<?php endforeach; ?>
</div><?php
delete_transient('my_errors');
endif;
}
add_action('admin_notices', 'my_notices');
但现在我不知道如何实现它来验证数据,以及是否有任何错误来取消发布帖子,因为它有错误,然后在 Gutenberg 中返回一些错误消息。
【问题讨论】:
标签: php wordpress wordpress-gutenberg