-
-
Save courtneyr-dev/7e99e8d6a92238eec5e726e125999f05 to your computer and use it in GitHub Desktop.
My 404 handler for scottkclark.com
This file contains 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() { | |
if ( is_admin() || ! is_404() ) { | |
return; | |
} | |
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. | |
$text = $title_text . ' ' . $is_are . ' changing every day, but it is still important to keep up-to-date.'; | |
// 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/2017/08/tribe-compact-logo.png" /> | |
<meta property="og:image:secure_url" content="https://www.scottkclark.com/wp-content/uploads/2017/08/tribe-compact-logo.png" /> | |
<meta property="og:image" content="https://www.scottkclark.com/wp-content/uploads/2017/08/pods-logo-green@2x.png" /> | |
<meta property="og:image:secure_url" content="https://www.scottkclark.com/wp-content/uploads/2017/08/pods-logo-green@2x.png" /> | |
<link rel="icon" href="https://3gnvse2l73p81ybmwt3vdvmo-wpengine.netdna-ssl.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-32x32.jpeg" sizes="32x32" /> | |
<link rel="icon" href="https://3gnvse2l73p81ybmwt3vdvmo-wpengine.netdna-ssl.com/wp-content/uploads/2017/08/cropped-23858759ec12476f5c1df5aff33ef8f8-192x192.jpeg" sizes="192x192" /> | |
<link rel="apple-touch-icon-precomposed" href="https://3gnvse2l73p81ybmwt3vdvmo-wpengine.netdna-ssl.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; | |
} | |
</style> | |
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1" frameborder="0" allowfullscreen allow="autoplay; fullscreen"></iframe> | |
<?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, 50000, 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