admin管理员组文章数量:1023251
I am building an application using WP REST API. Ideally, users are able to post only if they have a subscription. Here is how I plan to make things work:
- Set
expire_by
user meta when they sign up. This key is set to a timestamp 30 days from now. (i.e. users are given a 30 day free trial). I am usingstrtotime('+30 days')
to setexpire_by
at the time of sign up. - Users can post if current time is less than
expire_by
timestamp. So, I am doing similar to this:
if(strtomtime('now')>get_the_author_meta('expire_by', $user_id)){
// let the user post
} else {
// prevent from posting
// show an error
}
- Inside
subscriptions
page, users are redirected to payment gateway. Once the payment is complete, users'expire_by
meta key is set tostrtotime('+30 days')
The problem is step 2. I can't seem to make it work with transition_post_status
hook. So far, I have tried this:
add_action( 'transition_post_status', 'check_plan_validity', 11, 3 );
function check_plan_validity( $new_status, $old_status, $post ) {
$expire_by = (int) get_the_author_meta('expire_by', $post->post_author);
$now = strtotime("now");
if($now>$expire_by){
$error['error'] = 'Plan expired';
echo json_encode($error);
die();
}
}
Using the above code gives out a 500 server error on wp/v2/posts/
routes. My question is how can I prevent the post update and show an error on REST routes. I have tried pre_post_update
hook as well. But no dice. I have also tried this but the method described there only shows error on admin dashboard. But I want a solution for REST routes.
My logic can be wrong. But I am open to suggestions. Is there any better way I can use to prevent post update/create? I don't want to use any subscription plugin because most of them do not have any support for REST routes.
Any help is much appreciated.
I am building an application using WP REST API. Ideally, users are able to post only if they have a subscription. Here is how I plan to make things work:
- Set
expire_by
user meta when they sign up. This key is set to a timestamp 30 days from now. (i.e. users are given a 30 day free trial). I am usingstrtotime('+30 days')
to setexpire_by
at the time of sign up. - Users can post if current time is less than
expire_by
timestamp. So, I am doing similar to this:
if(strtomtime('now')>get_the_author_meta('expire_by', $user_id)){
// let the user post
} else {
// prevent from posting
// show an error
}
- Inside
subscriptions
page, users are redirected to payment gateway. Once the payment is complete, users'expire_by
meta key is set tostrtotime('+30 days')
The problem is step 2. I can't seem to make it work with transition_post_status
hook. So far, I have tried this:
add_action( 'transition_post_status', 'check_plan_validity', 11, 3 );
function check_plan_validity( $new_status, $old_status, $post ) {
$expire_by = (int) get_the_author_meta('expire_by', $post->post_author);
$now = strtotime("now");
if($now>$expire_by){
$error['error'] = 'Plan expired';
echo json_encode($error);
die();
}
}
Using the above code gives out a 500 server error on wp/v2/posts/
routes. My question is how can I prevent the post update and show an error on REST routes. I have tried pre_post_update
hook as well. But no dice. I have also tried this but the method described there only shows error on admin dashboard. But I want a solution for REST routes.
My logic can be wrong. But I am open to suggestions. Is there any better way I can use to prevent post update/create? I don't want to use any subscription plugin because most of them do not have any support for REST routes.
Any help is much appreciated.
本文标签: hooksHow to prevent post update using transitionpoststatus
版权声明:本文标题:hooks - How to prevent post update using transition_post_status 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745568030a2156567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论