WordPress: How to create rewrite rules with PHP

Marc Wagner, July 14, 2022

On Word­Press, you can crea­te your own Rewri­te Rules using PHP to gene­ra­te indi­vi­du­al URLs. The­re are only 3 steps neces­sa­ry. In this artic­le, I explain how you can easi­ly crea­te your own Rewri­te Rules via PHP for your Word­Press web­site.

Creating the Rewrite Rules #

Word­Press offers the func­tion add_rewrite_rule for crea­ting Rewri­te­Rules.

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

This allows us to crea­te our own rewri­te rules at any time. We use this func­tion by imple­men­ting 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 exam­p­le abo­ve, we now crea­te a new rewri­te rule that out­puts the con­tent of the page ‘https://meine-domain-xyz.de/jetzt-bewerben’ when a URL in this for­mat ‘https://meine-domain-xyz.de/job/elektriker-m-w‑d/bewerben’ is cal­led. The para­me­ter “page­na­me” is used for this pur­po­se.

Addi­tio­nal­ly, we tell Word­Press that the value bet­ween ‘job’ and ‘app­ly’ should be pas­sed as a GET para­me­ter named ‘job-name’. In order for us to use this para­me­ter on Word­Press, we need to tell the sys­tem that this para­me­ter exists.

Load your own template #

Alter­na­tively, you can load an indi­vi­du­al tem­p­la­te via your rewri­te rule. For this, you only have to use the action hook ‘template_include’ and inte­gra­te the tem­p­la­te 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, when­ever the ‘job-name’ para­me­ter occurs in the query, the ‘page-job-name.php’ tem­p­la­te is loa­ded and out­put.

Define query parameters (query vars) #

In the rewri­te rule we crea­ted abo­ve, we defi­ned a new para­me­ter ‘job-name’. Howe­ver, in order to use it, we need to regis­ter it with Word­Press first. For this, we use the fil­ter ‘query_vars’ and add our own para­me­ter 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 para­me­ter in a simp­le and straight­for­ward way.

Rewrite Rules Clear Cache #

Final­ly, we need to rel­oad the cache for the rewri­te rules. For this, we can eit­her use the func­tion flush_rewrite_rules, or sim­ply press the “Save” but­ton in the Word­Press Dash­board > Set­tings > Per­ma­links.

Important: If you deci­de to use the flush_rewrite_rules func­tion, you should make sure to remo­ve it again. Other­wi­se, the rewri­te rules will be rege­ne­ra­ted every time your Word­Press web­site is loa­ded and will affect the per­for­mance of your web­site.

Summary #

That was it. Word­Press, thanks to its modu­la­ri­ty, allows us to quick­ly and easi­ly defi­ne our own rewri­te rules that we can cus­to­mi­ze to our needs. Have you alre­a­dy crea­ted your own rewri­te rules or do you have fur­ther sug­ges­ti­ons, plea­se let us know.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment