[英]Validating post title and content in WordPress
我打算让用户通过前端创建博客文章。 我需要确保帖子标题和内容都少于 100 个字符。
我应该把我的验证功能挂到哪个钩子上?
提前致谢。
此代码确保帖子标题不为空或不超过 100 个字符。 如有必要,您可以根据需要进行调整。 该代码将按如下方式工作:
post_title
和post_status
不会改变;your_custom_post_type
替换为您的自定义帖子类型。编码:
<?php
add_action('save_post_{your_custom_post_type}', 'custom_validate_post', 10, 2);
if (!function_exists('custom_validate_post')) {
function custom_validate_post(int $post_ID, WP_Post $post): void
{
// Exit early if save_post is triggered when trashing the post
if ($post->post_status === 'trash' || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
$errors = [];
// Validation filters
if (empty($post->post_title)) {
$errors['title'] = 'The title is required';
}
if(strlen($post->post_title) > 100) {
$errors['title'] = 'The title should not be longer than 100 characters';
}
// If we have errors lets setup some messages
if (!empty($errors)) {
// Save the errors as option
update_option('post_errors', $errors);
// admin_notice is create by a $_GET['message'] with a number that wordpress uses to
// Display the admin message so we will add a filter for replacing default admin message with a redirect
add_filter('redirect_post_location', function (string $location) {
// Remove $_GET['message']
$location = remove_query_arg( 'message', $location );
// Add our new query sting
$location = add_query_arg( 'custom_validation', 'failed', $location );
// Return the location query string
return $location;
});
}
}
}
// We use this action to get the data before and after the update in order to revert the data if validation fails
add_action('post_updated', 'custom_set_old_post_data_if_error', 10, 3);
if (!function_exists('custom_set_old_post_data_if_error')) {
function custom_set_old_post_data_if_error(int $post_ID, WP_Post $post_after, WP_Post $post_before)
{
// Exit early if post_updated is triggered when trashing the post
if ($post_after->post_status === 'trash' || $post_after->post_type !== 'your_custom_post_type') {
return;
}
if (empty($post_after->post_title) || strlen($post_after->post_title) > 100) {
// We must remove this action or it will loop forever since we use wp_update_post below
remove_action('post_updated', 'custom_set_old_post_data_if_error');
// Set old `post_title` and `post_status` if validation fails
wp_update_post(
[
'ID' => $post_after->ID,
'post_title' => $post_before->post_title,
'post_status' => $post_before->post_status,
]
);
add_action('post_updated', 'custom_set_old_post_data_if_error');
}
}
}
// Add new admin message
add_action( 'admin_notices', function () {
if (isset($_GET['custom_validation']) && $_GET['custom_validation'] === 'failed') {
// Get the errors from the option album_errors
$errors = get_option('post_errors');
// Now delete the option post errors
delete_option('post_errors');
$display = '<div id="notice" class="error"><ul>';
// Because we are storing as an array we should loop through them
foreach ( $errors as $error ) {
$display .= '<li>' . $error . '</li>';
}
$display .= '</ul></div>';
// Finally echo out our display
echo $display;
// Add some jQuery
?>
<script>
jQuery(function($) {
$("#title").css({"border": "1px solid red"})
});
</script>
<?php
}
});
有几件事需要注意:
save_post
挂钩而不是save_post_{your_custom_post_type}
并从代码中删除$post_after->post_type !== 'your_custom_post_type'
条件。祝你好运!
尝试这个:
为此使用插件。 它会帮助你需要的。
请参考: http ://wordpress.org/plugins/post-title-validation/
或者
使用此代码。
add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
global $current_screen, $post;
if ( $current_screen->parent_base == 'edit' ){
if((!$post->post_name && !$post_content) && $_GET['post']) {
wp_redirect(admin_url('post-new.php?empty=1'));
}
if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>';
}
}
需要的钩子是 wp_insert_post_data
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.