hooks - How to prevent post update using transition_post_status

I am building an application using WP REST API. Ideally, users are able to post only if they have a subscription. Here i

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:

  1. 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 using strtotime('+30 days') to set expire_by at the time of sign up.
  2. 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 
}
  1. Inside subscriptions page, users are redirected to payment gateway. Once the payment is complete, users' expire_by meta key is set to strtotime('+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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745568032a4633512.html

相关推荐

  • hooks - How to prevent post update using transition_post_status

    I am building an application using WP REST API. Ideally, users are able to post only if they have a subscription. Here i

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信