Last active
April 14, 2021 14:45
-
-
Save sc0ttkclark/05020b15255891580347e7698b1c4e3a to your computer and use it in GitHub Desktop.
My 404 handler for scottkclark.com
This file contains hidden or 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 | |
/** | |
* Handle 404's in a way that can trick Slack and other link previews. | |
*/ | |
function skc_404_handler() { | |
$video_urls = [ | |
'https://www.youtube.com/watch?v=JB3INq8n9is', | |
'https://www.youtube.com/watch?v=jY5Mo_7kLFE', | |
'https://www.youtube.com/watch?v=rbsPu1z3ugQ', | |
'https://www.youtube.com/watch?v=f-tLr7vONmc', | |
'https://www.youtube.com/watch?v=dQw4w9WgXcQ', | |
]; | |
if ( is_admin() || ! is_404() ) { | |
return; | |
} | |
shuffle( $video_urls ); | |
$video_url = reset( $video_urls ); | |
$video_url = str_replace( 'watch?v=', 'embed/', $video_url ); | |
status_header( 200 ); | |
$year = date_i18n( 'Y' ); | |
$month = date_i18n( 'm' ); | |
$day = date_i18n( 'd' ); | |
$date_format = 'F Y'; | |
// Detect y + y/m + y/m/d formatting in URL. | |
$parts = current( explode( '?', $_SERVER['REQUEST_URI'] ) ); | |
$parts = explode( '/', trim( $parts, '/' ) ); | |
$path = basename( implode( '/', $parts ) ); | |
$title_text = ucwords( str_replace( '-', ' ', $path ) ); | |
if ( ! empty( $parts[0] ) && is_numeric( $parts[0] ) ) { | |
$year = (int) $parts[0]; | |
if ( ! empty( $parts[1] ) && is_numeric( $parts[1] ) ) { | |
$month = (int) $parts[1]; | |
if ( ! empty( $parts[2] ) && is_numeric( $parts[2] ) ) { | |
$day = (int) $parts[2]; | |
$date_format = 'F dS, Y'; | |
} | |
} | |
} | |
// Set up timestamp. | |
$time = strtotime( $year . '-' . $month . '-' . $day ); | |
// Set page title. | |
$title = $title_text . ' - ' . date_i18n( $date_format, $time ) . ' - Scott Kingsley Clark'; | |
$is_are = 'is'; | |
// Try to english a little. | |
if ( 's' === substr( $title_text, - 1 ) ) { | |
$is_are = 'are'; | |
} | |
// Set content dynamically. | |
$rando = [ | |
$title_text . ' ' . $is_are . ' happening, it\'s time that I made an announcement.', | |
$title_text . ' ' . $is_are . ' a big deal right now, I think I should explain further.', | |
]; | |
shuffle( $text ); | |
$text = reset( $text ); | |
if ( 'will-never-give-you-up' === $path ) { | |
$text = 'An imporant announcement about what is going on in the world of Scott(TM)'; | |
} | |
// Output main HTML. | |
?> | |
<!DOCTYPE html> | |
<html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#"> | |
<head> | |
<meta http-equiv="content-type" content="text/html" charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> | |
<link rel="profile" href="http://gmpg.org/xfn/11"> | |
<link rel="canonical" href="https://www.scottkclark.com<?php echo esc_attr( $_SERVER['REQUEST_URI'] ); ?>" /> | |
<meta property="og:locale" content="en_US" /> | |
<meta property="og:type" content="website" /> | |
<meta property="og:title" content="<?php echo esc_attr( $title ); ?>" /> | |
<meta property="og:description" content="<?php echo esc_attr( $text ); ?>" /> | |
<meta property="og:url" content="https://www.scottkclark.com<?php echo esc_attr( $_SERVER['REQUEST_URI'] ); ?>" /> | |
<meta property="og:site_name" content="Scott Kingsley Clark" /> | |
<meta property="og:image" content="https://www.scottkclark.com/wp-content/uploads/2021/01/the-events-calendar-logo-300x70.png" /> | |
<meta property="og:image" content="https://www.scottkclark.com/wp-content/uploads/2017/08/[email protected]" /> | |
<link rel="icon" href="https://www.scottkclark.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-32x32.jpeg" sizes="32x32" /> | |
<link rel="icon" href="https://www.scottkclark.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-192x192.jpeg" sizes="192x192" /> | |
<link rel="apple-touch-icon-precomposed" href="https://www.scottkclark.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-180x180.jpeg" /> | |
<meta name="msapplication-TileImage" content="https://www.scottkclark.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-270x270.jpeg" /> | |
<title><?php echo esc_html( $title ); ?></title> | |
<!-- Global site tag (gtag.js) - Google Analytics --> | |
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-7609391-1"></script> | |
<script> | |
window.dataLayer = window.dataLayer || []; | |
function gtag() { | |
dataLayer.push( arguments ); | |
} | |
gtag( 'js', new Date() ); | |
gtag( 'config', 'UA-7609391-1', { 'custom_map' : { 'dimension1' : 'viewtype' } } ); | |
gtag( 'event', 'viewtype_dimension', { 'viewtype' : 'LOL404' } ); | |
</script> | |
</head> | |
<body> | |
<?php | |
$is_slack = false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'slack' ); | |
$is_facebook = false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'facebook' ); | |
$is_linkedin = false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'linkedin' ); | |
$is_twitter = false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'twitter' ); | |
if ( ! $is_slack && ! $is_facebook && ! $is_linkedin && ! $is_twitter && empty( $_GET['skcdebug'] ) ) { | |
// Embed the video! | |
?> | |
<style type="text/css"> | |
body, html { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
background: #000; | |
color: #FFF; | |
font-family: Archivo, 'Helvetica Neue', sans-serif; | |
font-size: 0.9em; | |
} | |
a, a:visited, a:hover, a:active, a:focus { | |
color: #FFF; | |
} | |
</style> | |
<iframe width="100%" height="94%" src="<?php echo esc_url( $video_url ); ?>?autoplay=1" frameborder="0" allowfullscreen allow="autoplay; fullscreen"></iframe> | |
<p align="center"> | |
<strong>All of my favs:</strong> | |
<?php foreach ( $video_urls as $vid_url ) : ?> | |
<a href="<?php echo esc_url( $vid_url ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $vid_url ); ?></a> | |
<?php endforeach; ?> | |
</p> | |
<?php | |
} else { | |
// Output lots of content so the link preview has something to see. | |
?> | |
<p class="testing-12939j yes maybe"><?php echo implode( '</p><p class="testing-12939j yes maybe">', array_fill( 0, 5000, esc_html( $text ) ) ); ?></p> | |
<?php } ?> | |
</body> | |
</html> | |
<?php | |
die(); | |
} | |
add_action( 'template_redirect', 'skc_404_handler' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment