Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active March 27, 2023 13:25
Show Gist options
  • Save deviationist/39d24b3e6c387471481b64d225e7bcff to your computer and use it in GitHub Desktop.
Save deviationist/39d24b3e6c387471481b64d225e7bcff to your computer and use it in GitHub Desktop.
Facebook event app opener - do you have your own redirect in front of your Fb-event? Then it will most likely not open the app since the first request must be to a facebook-domain. But this lil' script attempts to open the app (only on mobile), if not it will redirects you as a fallback.
<?php $fbId = isset($_GET['fb_id']) ? $_GET['fb_id'] : false; ?>
<?php $whitelistedIds = ['your-event-id']; ?>
<?php if ($fbId && (count($whitelistedIds) == 0 || in_array($fbId, $whitelistedIds))) { ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
var id = '<?php echo htmlspecialchars($fbId); ?>',
desktopFallback = 'https://www.facebook.com/events/' + id,
mobileFallback = 'https://www.facebook.com/events/' + id,
app = 'fb://events/' + id;
if ( /Android|iPhone|iPad|iPod/i.test(navigator.userAgent) ) {
window.location = app;
window.setTimeout(function() {
window.location = mobileFallback;
}, 250);
} else {
window.location = desktopFallback;
}
function killPopup() {
window.removeEventListener('pagehide', killPopup);
}
window.addEventListener('pagehide', killPopup);
};
</script>
</body>
</html>
<?php } else { http_response_code(404); } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment