Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Last active June 22, 2023 15:11
  • Star 46 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kovshenin/9129977 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Static Templates
*
* If most of your site content is in .php template files, and you're tired of
* creating new pages, assigning them page templates, creating page templates
* then doing it all over again on production, this plugin is for you.
*
* Examples:
*
* - /foo/ => /wp-content/themes/twentyten/static-templates/foo.php
* - /foo/bar/ => /wp-content/themes/twentyten/static-templates/foo-bar.php
* - /foo/bar/baz/ => /wp-content/themes/twentyten/static-templates/foo-bar-baz.php
*
* You're welcome.
*/
add_filter( 'template_include', function( $template ) {
$url = parse_url( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$home = parse_url( home_url( '/' ) );
$request = trim( preg_replace( '#^' . preg_quote( $home['path'] ) . '#i', '', $url['path'] ), '/' );
if ( ! is_404() || empty( $request ) )
return $template;
// Loop throuh parts and makes ure they're sane.
$parts = explode( '/', $request );
foreach ( $parts as $part )
if ( ! preg_match( '#^[a-z0-9-]+$#i', $part ) )
return $template;
// Using stylesheet directory so parent/child themes don't share static pages.
$template_name = implode( '-', $parts );
$template_path = get_stylesheet_directory() . sprintf( '/static-templates/%s.php', $template_name );
if ( file_exists( $template_path ) ) {
$template = $template_path;
// We want trailing slashes, this is our last chance for a redirect.
if ( trailingslashit( $url['path'] ) != $url['path'] ) {
$url['path'] = trailingslashit( $url['path'] );
$redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $url['path'] );
if ( ! empty( $url['query'] ) )
$redirect .= '?' . $url['query'];
wp_safe_redirect( esc_url_raw( $redirect ) );
die();
}
// We're hi-jacking 404s.
status_header( 200 );
}
return $template;
});
add_action( 'template_redirect', function() {
// Core will redirect /dashboard/ to /wp-admin/, we don't want that. Unless you do.
remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
});
@kjprince
Copy link

Love this plugin. Saves me a lot of time. Have you tested with multi-site networks?

@marceloxp
Copy link

Please add a code before status_header( 200 ); to prevent error404 class inside body tag and Page not found inside title tag:

global $wp_query;
$wp_query->is_404 = false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment