Skip to content

Instantly share code, notes, and snippets.

@andykillen
Created January 28, 2018 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andykillen/e8f62982a9387bba16f4878d17fcf280 to your computer and use it in GitHub Desktop.
Save andykillen/e8f62982a9387bba16f4878d17fcf280 to your computer and use it in GitHub Desktop.
WordPress page template, redirect to Quiz url if not facebook.
<?php
/**
* Template Name: Facebook quiz redirect
*
**/
function check_if_facebook(){
/**
* this function checks if the current thing accessing the site is a facebook
* bot or not.
**/
$is_facebook = false;
$pattern = '/(FacebookExternalHit|visionutils|Facebot)/i';
$agent = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_ENCODED);
if(preg_match($pattern,$agent)){
$is_facebook = true;
}
/**
* Returns TRUE if is facebook bot, FALSE is anybody else
**/
return $is_facebook;
}
/**
* Check if the response if false and redirect if needed
* The redirect using header() must happen before get_header() is called
* If not, it will not work as things will already be sent to the browser/bot
**/
if( ! check_if_facebook() ){
header("Location:" . get_bloginfo('url') . "/quiz");
}
/**
* load get_header() so that all of the Open Graph info is added into the <head>
**/
get_header();
/**
* Loop not needed as facebook reads the head section only
*
* Footer not really needed, but left in for clarity
**/
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment