Temporary Homepage Redirect Between Two Dates
Sends homepage visitors to a promotion or event page during a date range you set, such as a Black Friday sale, then switches back automatically.
Opens WPIDE on
Code
// Promotion period (YYYY-MM-DD HH:MM) and target page, for example '/black-friday/'.
$wpide_promo_from = '';
$wpide_promo_until = '';
$wpide_promo_target = '';
add_action( 'template_redirect', function () use ( $wpide_promo_from, $wpide_promo_until, $wpide_promo_target ) {
if ( ! is_front_page() || current_user_can( 'edit_posts' ) || '' === $wpide_promo_from || '' === $wpide_promo_until || '' === $wpide_promo_target ) {
return;
}
$from = date_create( $wpide_promo_from, wp_timezone() );
$until = date_create( $wpide_promo_until, wp_timezone() );
if ( ! $from || ! $until || time() < $from->getTimestamp() || time() > $until->getTimestamp() ) {
return;
}
$target = home_url( $wpide_promo_target );
if ( untrailingslashit( $target ) !== untrailingslashit( home_url( '/' ) ) ) {
wp_safe_redirect( $target, 302 );
exit;
}
} );
Notes
Fill in both dates and the target first; nothing happens while they are empty. Dates use the site time zone, editors are not redirected, and page caching may delay the switch.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.