Flushing the rewriting rules
Once rewrite rules are modified, it's a must to flush the rules in order to prevent 404 page generation. Flushing existing rules is a time-consuming task which impacts the performance of the application and hence should be avoided in repetitive actions such as init. It's recommended that you perform such tasks in plugin activation or installation as we did earlier in user roles and capabilities. So, let's implement the function for flushing rewrite rules on plugin activation:
public function flush_application_rewrite_rules() {
flush_rewrite_rules();
}
As usual, we need to update the activation_handler function to include the call to the flush_application_rewrite_rules function using the following line of code:
$this->flush_application_rewrite_rules();
Now, go to the admin panel, deactivate the plugin, and activate the plugin again. Then, go to the URL http://www.example.com/user/login and check whether it works. Unfortunately, you will still get the 404 error for the request.
You might be wondering what went wrong. Let's go back and think about the process in order to understand the issue. We flushed the rules on plugin activation. So, the new rules should persist successfully. However, we will define the rules on the init action, which is only executed after the plugin is activated. Therefore, new rules will not be available at the time of flushing.
Consider the updated version of the flush_application_rewrite_rules function for a quick fix to our problem:
public function flush_application_rewrite_rules() {
$this->manage_user_routes();
flush_rewrite_rules();
}
We call the manage_user_routes function on plugin activation, followed by the call to flush_rewrite_rules. So, the new rules are generated before flushing is executed. Now, follow the previous process once again; you won't get a 404 page since all the rules have taken effect.
Now we are ready with our routing rules for user functionalities. It's important to know the existing routing rules of your application. Even though we can have a look at the routing rules from the database, it's difficult to decode the serialized array, as we encountered in the previous section.
So, I recommend that you use the free plugin called Rewrite Rules Inspector. This plugin has not been updated for recent WordPress versions. However, I haven't found any better or simpler plugin for this functionality. You can grab a copy at http://wordpress.org/plugins/rewrite-rules-inspector/. Once installed, this plugin allows you to view all the existing routing rules, as well as offering a button to flush the rules, as shown in the following screenshot:
Let’s identify the columns listed in the screenshot:
- Rule: This column shows the rule used to match the URL. So, in the first row we try to match URL’s that contain user/{any word}.
- Rewrite: This column shows how matched rules are passed into WordPress as dynamic parameters.
- Source: This column shows whether its a default rewriting rules related to WordPress core features like posts, categories, tags or whether it’s a custom rule.