# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: promotion, homepage, schedule
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/temporary-homepage-redirect/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Temporary Homepage Redirect Between Two Dates" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
