Created
May 16, 2012 20:13
-
-
Save ramseyp/2713576 to your computer and use it in GitHub Desktop.
Functions for a Genesis child theme's splash page asking the visitor to certify he/she is over 21.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Set & read cookie for age-check in a Genesis child theme. On first visit to the site (any page) | |
* visitors are redirected to the page /age-check/. The age check page uses a custom page template | |
* containing part of the code here. The first two functions "set_newuser_cookie()" | |
* and "cur_page_url()" should go in your functions file. | |
* The code below that is for your custom page template. | |
* | |
* @author Pat Ramsey | |
* | |
*/ | |
/* | |
* This goes in your functions file | |
*/ | |
add_action( 'genesis_pre', 'set_newuser_cookie'); | |
function set_newuser_cookie() { | |
if ( !is_admin() ) { | |
if (isset($_COOKIE['check_over21'])) { | |
return;// wp_redirect( site_url() ); | |
} else if (!isset($_COOKIE['check_over21']) && $_COOKIE['check_over21'] == '') { | |
$curr_page = cur_page_url(); | |
$splash_page = site_url() . '/age-check/'; | |
if ($curr_page == $splash_page) { | |
$curr_page = site_url(); | |
} | |
setcookie('check_over21', $curr_page, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); | |
$pos = strpos($check_redirect, 'wp-login.php'); // test for login otherwise go to Join | |
if ($pos !== false) { | |
$check_redirect = get_bloginfo( 'siteurl' ) . 'wp-login.php'; | |
//echo '<p>' . $check_redirect . '</p>'; | |
wp_safe_redirect( $check_redirect ); | |
} else { | |
wp_safe_redirect( $splash_page ); | |
} | |
} | |
} | |
} | |
/* | |
* This goes in your functions file | |
*/ | |
function cur_page_url() { | |
$pageURL = 'http'; | |
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} | |
$pageURL .= "://"; | |
if ($_SERVER["SERVER_PORT"] != "80") { | |
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; | |
} else { | |
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; | |
} | |
return $pageURL; | |
} | |
/* | |
* This goes in your page template. | |
* | |
*/ | |
add_action ('wp_head','check_lpage_hdr' ); | |
function check_lpage_hdr() { | |
echo '<style type="text/css">body{background-image:none !important;background-color:transparent !important;}</style>'; | |
echo ' | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
function setCookie(name,value,days) { | |
if (days) { | |
var date = new Date(); | |
date.setTime(date.getTime()+(days*24*60*60*1000)); | |
var expires = "; expires="+date.toGMTString(); | |
} | |
else var expires = ""; | |
document.cookie = name+"="+value+expires+"; path=/"; | |
}; | |
function deleteCookie(name) { | |
setCookie(name,"",-1); | |
} | |
$("#over21 a").click(function(e){ | |
setCookie("check_over21", 1, 1); | |
window.location = "'.get_bloginfo( 'siteurl' ).'"; | |
e.preventDefault(); | |
}); | |
$("#under21 a").click(function(e){ | |
deleteCookie("check_over21"); | |
window.location = "http://www.google.com"; | |
e.preventDefault(); | |
}); | |
}); | |
</script>'; | |
} | |
remove_action ( 'genesis_loop','genesis_do_loop' ); | |
add_action ( 'genesis_loop','check_landing_page_loop' ); | |
function check_landing_page_loop() { | |
echo '<h1>Age Check</h1>'; | |
echo '<div class="entry-content">'; | |
the_content(); | |
if ( isset( $_COOKIE['check_over21'] ) ) { | |
if (1 == $_COOKIE['check_over21'] ) { | |
$age_redirect = get_bloginfo( 'siteurl' ); | |
} else { | |
$pos = strpos( $age_redirect, 'wp-content' ); // test for bad link | |
if ( false !== $pos ) { | |
$age_redirect = get_bloginfo( 'siteurl' ); | |
} | |
echo '<p>'. $age_redirect . '</p>'; | |
echo '<p id="over21">I am <a href="#">Over 21</a></p>'; | |
echo '<hr />'; | |
echo '<p id="under21">I am <a href="#">Under 21</a></p>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment