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:

  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.

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.

本文标签: hooksHow to prevent post update using transitionpoststatus