Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.