Custom URL for each product variation – rewrite rules

Ok, for anyone looking for this, here’s a complete plugin that I came up with, that solves the issue: <?php /* Plugin Name: WooCommerce Variations URL Description: Adds support for variation-specific URL’s for WooCommerce product variations Author: Václav Greif Version: 1.0 Author URI: https://wp-programator.cz */ namespace WCVariationsUrl; final class Init { /** * Call this … Read more

Does WP Have a Function To Generate .htaccess RewriteCond?

In short yes… ‘External’ Rewrite Rules If in add_rewrite_rule the rule is isn’t directed to index.php then the rule is treated as an ‘external’ rule (i.e. not to be processed by WordPress’ internal handing of rewrites) and instead written to the .htaccess file. I.e. if: add_rewrite_rule(‘^nutrition/([^/]*)/([^/]*)/?’,’somethingelse.php?p=12&food=$1 &variety=$2′,’top’); is used instead of add_rewrite_rule(‘^nutrition/([^/]*)/([^/]*)/?’,’index.php?p=12&food=$matches[1]&variety=$matches[2]’,’top’); Then the rule … Read more

Add Rewrite Rule to point to a file on the server

(Revised answer) Don’t call flush_rewrite_rules() on init. Instead, if you want to do it programmatically, call the function from your plugin or theme activation function. If you’re not doing it programmatically, you could simply visit the Permalink Settings page (Settings → Permalinks on the WordPress back-end). WordPress does support adding a rewrite rule that doesn’t … Read more

Rewrite url for existing page without flush_rewrite_rules

If you want to rewrite example.com/test123 (a standard WordPress Page) to example.com/test234 (another standard WordPress Page) without having to save the rewrite rules in the database, then one option is using the request filter hook: add_filter( ‘request’, function ( $query_vars ) { if ( isset( $query_vars[‘pagename’] ) ) { $slug = $query_vars[‘pagename’]; // Define a … Read more

add_rewrite_rule() not working

You rewrite rule is quite broad and will most likely generate a lot of conflicts. add_action(‘init’, ‘dcc_rewrite_tags’); function dcc_rewrite_tags() { add_rewrite_tag(‘%propref%’, ‘([^&]+)’); } add_action(‘init’, ‘dcc_rewrite_rules’); function dcc_rewrite_rules() { add_rewrite_rule(‘^cottage-details/(.+)/?$’,’index.php?page_id=2&propref=$matches[1]’,’top’); } Then you can access to propref query var like: $propref = get_query_var( ‘propref’ ); And remember to flush the rewrite rules; you can do it … Read more

Custom Post Type rewrite redirects to homepage

The most important args for rewrite redirects when registering new post type are: ‘public’ => true, ‘publicly_queryable’ => true, ‘query_var’ => true, I have pasted some code below which I have tested and it working fine for the url : www.example.com/people/john $labels_employee = array(‘name’ => _x( ‘Employees’, ‘Post typegeneral name’, ‘textdomain’ ),’singular_name’ => _x( ‘Employee’, … Read more