Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created December 22, 2011 07:33
Show Gist options
  • Save trepmal/1509381 to your computer and use it in GitHub Desktop.
Save trepmal/1509381 to your computer and use it in GitHub Desktop.
WordPress: Countdown, Redirect
<?php
//Plugin Name: Countdown, Redirect
//Description: Just a demo, thus has limitations
new Countdown_Redirect();
class Countdown_Redirect {
function __construct() {
add_action( 'admin_menu', array( &$this, 'menu' ) );
add_action( 'wp_head', array( &$this, 'countdown_js' ) );
}
function menu() {
add_options_page( 'Countdown, Redirect', 'Countdown, Redirect', 'edit_posts', __FILE__, array( &$this, 'page' ) );
}
function page() {
?><div class="wrap"><h2><?php _e('Countdown, Redirect'); ?></h2><?php
if (isset($_POST['countdown_page'])) {
update_option( 'countdown_redirect', array(
'onpage' => intval( $_POST['countdown_page'] ),
'url' => esc_url( $_POST['redirect_url'] ),
'timeout' => intval( $_POST['timeout'] ),
) );
echo '<div class="updated"><p>Updated!</p></div>';
}
extract( get_option( 'countdown_redirect', array( 'onpage' => '','url' => '','timeout' => '' ) ) );
?>
<form method="post">
<label>Page ID: <input type="number" name="countdown_page" value="<?php echo $onpage; ?>" /></label><br />
<label>Redirect URL: <input type="url" name="redirect_url" value="<?php echo $url; ?>" /></label><br />
<label>Timeout: <input type="number" name="timeout" value="<?php echo $timeout; ?>" /></label><br />
<input type="submit" />
</form>
</form>
</div><?php
}
function countdown_js() {
extract ( get_option( 'countdown_redirect', array() ) );
if ( ! isset( $onpage ) || ! is_page( $onpage ) ) return;
?>
<script type="text/javascript">
var ss = <?php echo $timeout; ?>;
function countdown() {
ss = ss-1;
if (ss<0) {
window.location="<?php echo $url; ?>";
} else {
document.getElementById("countdown").innerHTML=ss;
window.setTimeout("countdown()", 1000);
}
}
window.onload=countdown;
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment