Initial import of 404 Enhacer
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Swaps in the custom 404 template and enqueues its assets.
|
||||
*
|
||||
* @package 404_Enhancer
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the front-end 404 template.
|
||||
*/
|
||||
final class FE_Template {
|
||||
|
||||
/**
|
||||
* Registers front-end hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
add_action( 'template_redirect', array( __CLASS__, 'handle_404' ) );
|
||||
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* On 404 requests: send no-cache headers and swap the template.
|
||||
*/
|
||||
public static function handle_404() {
|
||||
if ( ! is_404() ) {
|
||||
return;
|
||||
}
|
||||
nocache_headers();
|
||||
add_filter( 'template_include', array( __CLASS__, 'include_template' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the template path for 404 requests.
|
||||
*
|
||||
* Themes can override the template via the `fe_404_template` filter.
|
||||
*
|
||||
* @param string $template Current template path.
|
||||
* @return string
|
||||
*/
|
||||
public static function include_template( $template ) {
|
||||
$custom = FE_PLUGIN_DIR . 'templates/404.php';
|
||||
/**
|
||||
* Filters the 404 template path.
|
||||
*
|
||||
* @param string $custom Default template inside the plugin.
|
||||
*/
|
||||
return apply_filters( 'fe_404_template', $custom );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the 404 stylesheet only on 404 pages.
|
||||
*/
|
||||
public static function enqueue_assets() {
|
||||
if ( ! is_404() ) {
|
||||
return;
|
||||
}
|
||||
wp_enqueue_style(
|
||||
'fe-404',
|
||||
FE_PLUGIN_URL . 'assets/css/fe-404.css',
|
||||
array(),
|
||||
FE_VERSION
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user