根据wp_post_insert,过滤器wp_insert_post_empty_content应该适合您。
/**
* Filters whether the post should be considered "empty".
*
* The post is considered "empty" if both:
* 1. The post type supports the title,editor,and excerpt fields
* 2. The title,and excerpt fields are all empty
*
* Returning a truthy value to the filter will effectively short-circuit
* the new post being inserted,returning 0. If $wp_error is true,a WP_Error
* will be returned instead.
*
* @since 3.3.0
*
* @param bool $maybe_empty Whether the post should be considered "empty".
* @param array $postarr Array of post data.
*/
if ( apply_filters( 'wp_insert_post_empty_content',$maybe_empty,$postarr ) ) {
if ( $wp_error ) {
return new WP_Error( 'empty_content',__( 'Content,title,and excerpt are empty.' ) );
} else {
return 0;
}
}
您可以在wp_insert_post_empty_content过滤器中进行条件查询,并返回true以阻止帖子被插入。
,
感谢您的投入,我认为@KGreene的答案值得一试。
我已经改变了策略,如果用户没有jeton,我将重定向用户,这样他们甚至都看不到编辑器,然后在其他钩子上发布时烧掉jeton。
/*
* Redirect user that doesnt have jeton,let others proceed
* https://developer.wordpress.org/reference/hooks/save_post/
*/
function handle_before_publish_post($post_id,$post,$update) {
// Only on post creation not update (they can update with 0 jeton)
if ($update) {
return;
}
$poster_id = $post->post_author;
$meta_jeton = get_user_meta($poster_id,'jeton')[0];
if ($meta_jeton > 0) {
return;
} else {
wp_redirect('[my-domain]/wp-admin/edit.php?post_type=[my-custom-post-type]');
}
}
add_action('save_post_[my-custom-post-type]','handle_before_publish_post',10,3);
/*
* Use a jeton on post creation
* https://developer.wordpress.org/reference/hooks/new_status_post-post_type/
*/
function handle_after_publish_post($post_id,$post) {
// Burn jeton
$poster_id = $post->post_author;
$meta_jeton = get_user_meta($poster_id,'jeton')[0];
$new_jeton_count = $meta_jeton - 1;
update_user_meta($poster_id,'jeton',$new_jeton_count);
return;
}
add_action('publish_[my-custom-post-type]','handle_after_publish_post',3);
这不是我想要的方式,但是效果很好。
谢谢大家的时间
本文链接:https://www.f2er.com/3144009.html