Skip to content

Instantly share code, notes, and snippets.

@andykillen
Created January 28, 2018 18:59
Show Gist options
  • Select an option

  • Save andykillen/e8f62982a9387bba16f4878d17fcf280 to your computer and use it in GitHub Desktop.

Select an option

Save andykillen/e8f62982a9387bba16f4878d17fcf280 to your computer and use it in GitHub Desktop.

Revisions

  1. Andrew Killen created this gist Jan 28, 2018.
    45 changes: 45 additions & 0 deletions page-quizredirect.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?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();