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.

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

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *