I’ve written plugins that do similar things but using a template inside the plugin’s own folder and intercepting with template_redirect
. I believed I had something similar already written but I didn’t find it. I did a search and found Virtual Page Within Theme Template and even published the same solution there at the time of intercepting the parse_request
.
Includes a new rewrite_rule
with the virtual address and when checking the request, if this is the virtual address, print a template simulation and stop the rest of the execution.
Install the plugin and click Upgrade in the Permalinks options (/wp-admin/options-permalink.php
). Then visit: http://example.com/virtual
. The ideal is to register a flush_rewrite_rules
on the activation and deactivation, but in my tests is not working on the deactivation of the plugin, so I prefer to suggest the manual update.
<?php
/*
* Plugin Name: (SOPT) Página virtual
*/
add_action( 'init', 'init_sopt_38020' );
add_filter( 'query_vars', 'query_vars_sopt_38020' );
add_action( 'parse_request', 'parse_request_sopt_38020');
function init_sopt_38020() {
add_rewrite_rule( 'virtual/?', 'index.php?virtual=new', 'top' );
}
function query_vars_sopt_38020( $query_vars ) {
$query_vars[] = 'virtual';
return $query_vars;
}
function parse_request_sopt_38020( $wp ) {
if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
get_header(); ?>
<div id="primary">
<div id="content" role="main">
Olá, mundo!
</div>
</div>
<?php get_footer();
exit;
}
}
In fact, the question seeks exactly to avoid this solution.
– brasofilo