[Coding Snippets #1]: Wordpress URL Rewrite
Hey everyone! How are you? Did you miss me? :P I've been having a hard time finding something to post about, so I decided to make a series of "Coding Snippets" with various things I regularly use in my projects!
I'll try to explain the code as best as I can, but you can probably find a better explanation anywhere else!
Intro
Let's start with Custom Rewrites for Wordpress. This is useful for CPTs (Custom Post Types) or custom searches that you want to go around the default search functionality of Wordperss. In a client's site I have added a custom post type with chassis numbers for motorcycles, that are being used in various ways (show the model using the specific chassis number, spare parts & accessories compatible with the model). I did it as a CPT because it will be easier for the client to manage it this way. This is a "must" if you want to have pretty permalinks for something custom you are doing, as SEO specialists say it will make a difference.
This is the code:
add_action( 'init', 'add_some_rewrite' );
function add_some_rewrite() {
global $wp;
$wp->add_query_var( 'category' );
add_rewrite_rule('^urltorewrite/([.+]*)/?$','index.php?pagename=target_pagename&category=$matches[1]','top');
}
Call a function at init
The first line add_action...
tells Wordpress to run the function add_some_rewrite
during the init
ialization of the site. Running the add_rewrite_rule
function at any other time instead of the initialization, will result in issues.
Line 2 is defining the function add_some_rewrite
. Nothing to explain here, maybe that we don't need any parameters in the function.
Import Wordpress internal functions
This brings us to Line 3: global $wp;
PHP has 3 variable types (scopes). Global, local, and static. In a nutshell:
1) global variables are declared outside of a function and can only be accessed outside of a function
2) local variables are declared inside a function, can be accessed only inside the specific function and they are destroyed when the function completes its execution. That means that if I execute the same function, the variable will be reset to it's original value.
3) static variables are declared using the static keyword (example: static $x = 12;
) but their value remains in the memory until all the script execution finishes. Here's a snippet to make sense:
function staticIncreaseValue() {
static $x = 0;
echo $x;
$x++;
}
If you call staticIncreaseValue
3 times, the output will be 0 1 2.
If the static
keyword was missing, the output would be 0 0 0.
Now that we got this out of the way, $wp
is a global variable of the WP class. The WP Class allows a developer to parse the request URL, query posts, sending headers (for example, HTTP status codes like my favorite 418: I'm a teapot
) etc. Using the global
keyword in front of a global variable inside a function, we essentially import the global variable into the function (not the best wording in the world, but gets the message across), and we can use it. It does not create a copy, so everything we do, will be passed to the global variable outside of the function in case of a modification.
Add a public query variable
Line 4: $wp->add_query_var( 'category' );
Now we add 'category' as a public query variable. This can be compared to a GET variable from a query string ( hxxps://awesome.url.x/?category=something
). If we skip this line, we will never get the 2nd input from our custom URL. You'll understand what I mean in the next section. You should change "category" to something that may fit your case better.
Create the rewrite rule
Line 5: add_rewrite_rule(...)
This is essentially a rule telling Wordpress what to do in case of getting a link that will fit our requirements. In the example, if a user visits hxxp://awesome.url.x/urltorewrite/gotit/
, he will get as a reply what is in this link: hxxp://awesome.url.x/index.php?pagename=target_pagename&category=gotit
without the ugly URL.
The variable $matches
is essentially a list of all the query variables in our URL. In the above ugly URL $matches[0]
is target_pagename
and $matches[1]
is gotit
(that's the "second input" I was talking about earlier.)
That's all for this post! If you have any questions, feel free to write a comment and I'll try to help if I can. As I said, I plan to make a series of posts like this, and I'll do my best to post them regularly.
WordPress is a registered trademark of The Wordpress Foundation. Wordpress and the code I talked about in the post are both released under the GPLv2 license. You can read the license terms here