Home > Net >  How to create custom route to a page in WordPress
How to create custom route to a page in WordPress

Time:12-10

I want to set a new Route in WordPress that will be redirected to the desired path by entering a specific pattern.

for example: When I enter any url like below:

http://mywebsite.com/audio/Everything

http://mywebsite.com/audio/**********

http://mywebsite.com/audio/0123456789

These routes and similar ones redirects to sound.php

http://mywebsite.com/wp-content/sounds/sound.php

CodePudding user response:

You can use add_rewrite_rule function, Here is official WP reference - https://developer.wordpress.org/reference/functions/add_rewrite_rule/

As per your example above, You can do like below,

add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
    add_rewrite_rule('^audio/([^/]*)/?','wp-content/sounds/sound.php','top');
}
  1. Try above code as per your requirement and add into your theme's function.php OR plugin file.
  2. After code added, You must Go to Settings > Permalinks page on your site admin panel and do click on Save Changes button.
  • Related