WordPress: How to create rewrite rules with PHP

On WordPress, you can create your own Rewrite Rules using PHP to generate individual URLs. There are only 3 steps necessary. In this article, I explain how you can easily create your own Rewrite Rules via PHP for your WordPress website.

Creating the Rewrite Rules #

WordPress offers the function add_rewrite_rule for creating RewriteRules.

add_rewrite_rule(string $regex, string|array $query, string $after = 'bottom');

This allows us to create our own rewrite rules at any time. We use this function by implementing it in our functions.php.

Inhalte einer anderen Seite laden #

add_action('init', function(){
    add_rewrite_rule('job/([a-zA-Z0-9\-]+)/bewerben', 'index.php?pagename=jetzt-bewerben&job-name=$matches[1]','top');
});

In our example above, we now create a new rewrite rule that outputs the content of the page ‘https://meine-domain-xyz.de/jetzt-bewerben’ when a URL in this format ‘https://meine-domain-xyz.de/job/elektriker-m-w-d/bewerben’ is called. The parameter “pagename” is used for this purpose.

Additionally, we tell WordPress that the value between ‘job’ and ‘apply’ should be passed as a GET parameter named ‘job-name’. In order for us to use this parameter on WordPress, we need to tell the system that this parameter exists.

Load your own template #

Alternatively, you can load an individual template via your rewrite rule. For this, you only have to use the action hook ‘template_include’ and integrate the template you want to use.

add_action( 'template_include', function( $template ) {
    if ( false == get_query_var( 'job-name' ) || '' == get_query_var( 'job-name' )) {
        return $template;
    }
 
    return get_template_directory() . '/page-job-name.php';
});

Now, whenever the ‘job-name’ parameter occurs in the query, the ‘page-job-name.php’ template is loaded and output.

Define query parameters (query vars) #

In the rewrite rule we created above, we defined a new parameter ‘job-name’. However, in order to use it, we need to register it with WordPress first. For this, we use the filter ‘query_vars’ and add our own parameter to the array.

add_filter('query_vars', function($query_vars){
    $query_vars[] = 'job-name';
    return $query_vars;
});

We can then use ‘get_query_var’ to query our parameter in a simple and straightforward way.

Rewrite Rules Clear Cache #

Finally, we need to reload the cache for the rewrite rules. For this, we can either use the function flush_rewrite_rules, or simply press the “Save” button in the WordPress Dashboard > Settings > Permalinks.

Important: If you decide to use the flush_rewrite_rules function, you should make sure to remove it again. Otherwise, the rewrite rules will be regenerated every time your WordPress website is loaded and will affect the performance of your website.

Summary #

That was it. WordPress, thanks to its modularity, allows us to quickly and easily define our own rewrite rules that we can customize to our needs. Have you already created your own rewrite rules or do you have further suggestions, please let us know.

Hier klicken, um den Beitrag zu bewerten
[Gesamt: 2 Durchschnitt: 4]

Leave A Comment

Title