我似乎找不到解决这个问题的办法。我找到了save_post
钩子,但这会在帖子保存到数据库后激发。
我有一个appointments
自定义帖子类型和我想做的是在创建帖子之前验证一些东西,否则如果验证失败,则忽略创建帖子。
有类似的钩子吗save_post
但它不是在创建帖子之后开火,而是在创建帖子之前开火。也许是这样的before_save_post
我似乎找不到解决这个问题的办法。我找到了save_post
钩子,但这会在帖子保存到数据库后激发。
我有一个appointments
自定义帖子类型和我想做的是在创建帖子之前验证一些东西,否则如果验证失败,则忽略创建帖子。
有类似的钩子吗save_post
但它不是在创建帖子之后开火,而是在创建帖子之前开火。也许是这样的before_save_post
如果您想让用户知道他的帖子有问题,那么应该在JS中进行验证。
用户发布帖子后,希望将其存储在服务器上,但发布的副作用是页面被重新加载,如果由于验证而未保存数据,用户如何知道被拒绝的内容是什么,因为重新加载可能会显示旧内容。
如果您认为基于JS的验证对于您的用例来说还不够,那么您可以实现一个服务器端验证,它可以完成两件事
通过在编辑帖子之前进行验证,防止帖子状态设置为已发布,使用WP错误API显示错误消息但这应该是对JS验证的补充,而不是取而代之。
是的,似乎没有办法停止后期保存。我正在检查wp includes/post。php,我所看到的是wp_insert_post_data
过滤您可以检查和修改帖子数据的位置(保存前),但仅此而已。
或您可以连接到save_post
, 如果邮件有问题,请致电wp_delete_post()
并立即移除:-)
这里有一个非常基本和粗糙的方法来实现你所需要的。
有时我自己也会使用它进行快速验证,帮助客户避免遗漏任何重要内容。
我创建了相同的自定义post类型slug,因此css/js目标将与您的类似。
一旦你有了这个概念,无论你需要什么,都要改变它。将css和js代码移动到适当的文件中,添加相关注释,您就有很大的空间来做您需要的事情。
add_action(\'admin_footer\', \'bt_before_submit_validation\');
function bt_before_submit_validation () {
?>
<style>
.post-type-appointments .custom-error {
padding: 10px;
box-sizing: border-box;
display: block;
}
.post-type-appointments #publishing-action {
position: relative;
width: 100%;
}
.post-type-appointments #publishing-action .vbs-btn {
width: 100%;
height: 100%;
position: absolute;
z-index: 1;
top: 0;
left: 0;
}
</style>
<script>
($ => {
// add facade submit button
// This button will be used to trigger validation before submit
// (change "Validate and Publish") to what ever you want
$(\'.post-type-appointments #publishing-action [type="submit"]\').before(\'<button type="button" class="vbs-btn button button-primary">Validate and Publish</button>\');
// now that we have a button we can use it to start validating
$(document).on(\'click\', \'.post-type-appointments #publishing-action .vbs-btn\', e => {
// we will do a basic validation for title
const $this = $(e.currentTarget);
// remove all previous errors
$(\'.custom-error\').remove();
// will indicate if validation was successful
// by default we assume everything is good, once we encounter a problem we will change this to false
let isValid = true;
// get the elements
const $title = $(\'[name="post_title"]\');
// get the value
const titleVal = $title.val().trim();
// validate title field
if (!titleVal) {
isValid = false;
$title.after(\'<span class="custom-error notice notice-error">Title field cannot be empty</span>\');
} else if (titleVal.length < 10) {
isValid = false;
$title.after(\'<span class="custom-error notice notice-error">Title field must contain at least 10 characters.</span>\');
} // continue adding as many validations as needed
// validation faild, return
if (!isValid) return;
// everything is ok, lets continue
// trigger click on submit button
$this.next(\'[type="submit"]\').click();
// remove the facade button, this is optional
$this.remove();
});
})(jQuery);
</script>
<?php
}
如果出现问题,请确保检查css/js目标。为了创建appointments
岗位类型
我目前正在为Wordpress编写我的第一个OOP插件。为了帮助我找到一点结构,a boiler plate 这为我奠定了基础。在里面Main.php 有一种方法可以为管理员加载JS和CSS资产:/** * Register all of the hooks related to the admin area functionality * of the plugin. * * @since 0.1.0 * @access private