I had a very short window to get this functioning today. I haven’t toyed with WordPress rewrites API in some time so I had to dive right in and try to figure this out.
The Problem
I needed /plugin-content/ to rewrite to a blank page I could place a form on. I needed this to be done without having to create a page in the WordPress admin first. There are ways to create this page automagically when you first activate the plugin. I wanted it to not be tied to a post/page in the database but to simply work. First I needed to build out a rewrite. Let’s do this all in the primary plugin class for now. Let’s imagine our class is called MyPlugin.
Building the Rewrite
First we need to add an action to init, I do this in __construct()
public function __construct() {
add_action( 'init', array( $this, 'your_website_rewrite' ) );
add_action( 'template_redirect', array( $this, 'make_your_website_form' ) );
}
You can see that I also hook into ‘template_redirect’ which we will address momentarily. Here is the your_website_rewrite function:
public function your_website_rewrite() {
add_rewrite_rule( '^your-website$', 'index.php?your-website=display-form', 'top' );
add_rewrite_tag('%your-website%', '([^&]+)');
if ( get_option( 'websites-cpt-flush-rewrites' ) ) {
flush_rewrite_rules();
delete_option( 'websites-cpt-flush-rewrites' );
}
}
The first line creates our write which is ‘your-website’ so when we visit mysite.com/your-website the rewrite will be called. The second parameter sends us to the site’s homepage index.php and tacks on a query string. We need to let WordPress know to expect this query variable ‘your-website’ so we use the add_rewrite_tag function.
The way I handle flush_rewrite_rules is inside the activation of the plugin. I set an option to true that means the rewrites do need to be flushed. You don’t want to flush these every time the plugin is called as it can be very heavy processing. So we only do it once after setting the rewrite rules. They must be flushed this one time though or it won’t work.
Displaying Your Content
I originally set this up to display the content via a shortcode. This was not the requirement though but since I had the shortcode setup and working I just used it here to display the form I needed. That said, you can echo whatever you want in this place. Here’s a quick look at what happens next. We hook into the ‘template_redirect’ action with our function make_your_website_form():
public function make_your_website_form( $template ) {
if ( get_query_var( 'your-website' ) == 'display-form' ) {
get_header();
echo do_shortcode( '[WSCPT]' );
get_footer();
exit();
} else {
return $template;
}
}
This checks the query string to see if your-website is set. If our rewrite worked correctly then it should be there when visiting site.com/your-website.
Now inside that check we call get_header() followed by whatever we want to display then wrap it up with get_footer. It’s important to put the exit(); here though as that is what stops all other content from being loaded.