Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. // This page should be visible only for 48 hours since the first visit
  2. // Drop this in functions.php
  3.  
  4. function make_the_page_visible_only_certain_time_after_first_visit() {
  5. $secret_page = PAGE_ID_HERE;
  6. $redirect_to_when_expired = URL_TO_REDIRECT;
  7. $time_to_visit = 48 * 3600; //48h
  8. if ( is_page( $secret_page ) && ! is_user_logged_in() ) {
  9. if ( empty( $_GET[ 'visitor_id' ] ) ) {
  10. wp_redirect( URL_OF_EXPIRED_PAGE );
  11. exit; // Stop scrip exec.
  12. }
  13. $visitor_email = $_GET[ 'visitor_id' ];
  14. $has_visited_before = get_post_meta( $secret_page, 'visited_' . $visitor_email, true );
  15. if ( ! $has_visited_before ) {
  16. // Log the time of the visit in post meta
  17. add_post_meta( $secret_page, 'visited_' . $visitor_email, time(), true );
  18. } else if ( intval( $has_visited_before ) + $time_to_visit < time() ) {
  19. wp_redirect( URL_OF_EXPIRED_PAGE );
  20. exit; // Stop scrip exec.
  21. }
  22. }
  23. }
  24. add_action( 'template_redirect', 'make_the_page_visible_only_certain_time_after_first_visit' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement