Puddinq.com sharing knowledge

WordPress Rewrite urls to pretty permalinks

Wordpress Rewrite urls to pretty permalinks

WordPress is brilliant and handles a lot out of the box. This post goes beyond that. In settings you can influence your permalinks. You can build structures with prefixes, categories, tags, and dates but that’s about it, what if you want to use acf fields for permalink structure?

Permalinks

The permalink for this post is ‘https://puddinq.mobi/wordpress/wordpress-rewrite-urls-zo-pretty-permalinks/’ which is done by defining a custom structure in settings -> permalinks : /%category%/%postname%/ . With php you can add a get variable to the url but that is ugly.

  • To send some information url has www.example.nl/?variable=on behind it and with php you can say $variable = $_GET[‘variabele’] which will be ‘on’.
  • But what if you want www.example.nl/varon/

Rewrite rules

Adding to the rewrite rules gives you the ability do do as you like before and after WordPress decides what to display. WordPress resolves around wp_query and the (main) loop.

  1. You need wp_query to understand your variables.
  2. You need to make a ‘translation’ for your url / get combination(s), and put them before or after the basic ones.

 

Just to scare you off. 😉  If you use the code below, you will find all rules in your setup.

global $wp_rewrite;
print_r($wp_rewrite->rules);

The shotcode on the right outputs (hidden in html with <!– –>) The code that was used for the current page you are on

add_shortcode('rewrite-shortcode', 'rewrite_shortcode');
 
function rewrite_shortcode() {
    global $wp, $template;
 
    echo "\r\n";
    echo '<!– Request: ';
echo empty($wp->request) ? 'None' : esc_html($wp->request);
echo ' –>;'."\r\n";
echo '<!– Matched Rewrite Rule: ';
echo empty($wp->matched_rule) ? 'None' : esc_html($wp->matched_rule);
echo ' –>;'."\r\n";
echo '<!– Matched Rewrite Query: ';
echo empty($wp->matched_query) ? 'None' : esc_html($wp->matched_query);
echo ' –>'."\r\n";
echo '<!– Loaded Template: ';
echo basename($template);
echo ' –>'."\r\n";
}

The code below:

  • first adds 3 variables ‘neer’, ‘price’, ‘signs’ as variables to wp_query arguments.
  • The second parts adds the translation for WordPress using regular expressions.
    • First it checks if all three are used then one less each new line
function add_hotel_query_vars($aVars) {
    array_push($aVars, 'neer', 'price', 'signs');
    //$aVars[] = "img_group"; // represents the name of the product category as shown in the URL
    return $aVars;
}
 
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_hotel_query_vars');
 
 
function add_hotel_rewrite_rules($aRules) {
    $aNewRules1 = array('bar/(.+?)/neer-(.+?)/price-(.+?)/signs-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;neer=$matches[2]&amp;price=$matches[3]&amp;signs=$matches[4]');
    $aNewRules2 = array('bar/(.+?)/neer-(.+?)/price-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;neer=$matches[2]&amp;price=$matches[3]');
    $aNewRules3 = array('bar/(.+?)/neer-(.+?)/signs-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;neer=$matches[2]&amp;signs=$matches[3]');
    $aNewRules4 = array('bar/(.+?)/price-(.+?)/signs-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;price=$matches[2]&amp;signs=$matches[3]');
    $aNewRules5 = array('bar/(.+?)/neer-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;neer=$matches[2]');
    $aNewRules6 = array('bar/(.+?)/price-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;price=$matches[2]');
    $aNewRules7 = array('bar/(.+?)/signs-(.+?)/?$' => 'index.php?product_cat=$matches[1]&amp;signs=$matches[2]');
 
    $aRules = $aNewRules1 + $aNewRules2 + $aNewRules3 + $aNewRules4 + $aNewRules5 + $aNewRules6 + $aNewRules7 + $aRules;
    return $aRules;
}
 
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_hotel_rewrite_rules');