How can I create custom URL routes?

Add this to your theme’s functions.php, or put it in a plugin. add_action( ‘init’, ‘wpse26388_rewrites_init’ ); function wpse26388_rewrites_init(){ add_rewrite_rule( ‘properties/([0-9]+)/?$’, ‘index.php?pagename=properties&property_id=$matches[1]’, ‘top’ ); } add_filter( ‘query_vars’, ‘wpse26388_query_vars’ ); function wpse26388_query_vars( $query_vars ){ $query_vars[] = ‘property_id’; return $query_vars; } This adds a rewrite rule which directs requests to /properties/ with any combination of numbers following to …

Read more

Using a Rewrite URL in a Plugin to Load a New Page in the Template

add_rewrite_rule( $regex, $redirect, $after ); Rewrite rules to scripts other than index.php The $redirect argument works slightly differently when redirecting to a custom PHP script because WordPress delegates these redirects to .htaccess instead of processing them itself. For this reason, querystring variables should be written like $1 instead of $matches[1]. Personally, I would use a …

Read more

rewrite rule generated with mpdf to a shorter version

ok it was easy, the problem was that to match the left side pattern you have to use $1 not $matches[1], this is the solution /** * Rewrite rules */ add_action( ‘init’, function() { add_rewrite_rule( ‘^invoice_([0-9]+).pdf$’, ‘/wp-content/themes/mysitetheme/invoices/invoice_$1.pdf’, ‘top’ ); } ); UPDATE From the suggestions received in the comments, it is now clear to me …

Read more

How to redirect Webp files?

If I’m reading it right, your regex has only two items in (), which means that only $1 and $2 have any meaning. I copied your sample text and went to https://regex101.com/ (note: this is not an endorsement) to try to assemble a regex that’ll cover all your images. Here’s what I ended up with: …

Read more

Bilingual WP site: How to achieve different URL sturcture rule based on its language?

Try the following steps: Add the rewrite rules for the <language>/blog/%category%/%post_id% structure. I used the post_rewrite_rules hook to add the rewrite rules, but for generating the rewrite rules, I used WP_Rewrite::generate_rewrite_rules() the same way WordPress core used it to generate the rewrite rules for the default permalink structure (that you set via the Permalink Settings …

Read more