I have already written plugins that do similar things but using a template inside the plugin's own folder and intercepting with template_redirect
. I thought there was something like that already written but I did not 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 parse_request
.
A new rewrite_rule
is included with the virtual address and when checking request , if it is the virtual address, print a template simulation and stop the rest of the execution.
Install the plugin and click Update in the Permalinks ( /wp-admin/options-permalink.php
) options. Then visit: http://example.com/virtual
. The ideal is to register a flush_rewrite_rules
on activation and deactivation, but in my tests it is not working on deactivating 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;
}
}