Skip to content

Instantly share code, notes, and snippets.

@3runoDesign 3runoDesign/functions.php
Last active Sep 22, 2018

Embed
What would you like to do?
Create custom page based on URL rewriting
<?php
// https://codex.wordpress.org/Class_Reference/WP_Rewrite
// https://codex.wordpress.org/Rewrite_API/flush_rules
// https://codex.wordpress.org/Plugin_API/Action_Reference/generate_rewrite_rules
add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ) {
$wp_rewrite->rules = array_merge(
['my-route/?$' => 'index.php?param_internal=1'],
$wp_rewrite->rules
);
} );
add_action( 'init', function () {
global $wp_rewrite;
$wp_rewrite->flush_rules();
});
// https://developer.wordpress.org/reference/hooks/query_vars/
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'param_internal';
return $query_vars;
} );
// https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
// https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
add_action( 'template_redirect', function () {
if ( intval( get_query_var( 'param_internal' ) ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/views/template-my-route.php';
});
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.