20

How does the core wp routing works? I am having a hard time understanding... In MVC, your url looks like mycontroller/myaction that maps to MyController->myaction()

In drupal, it's index.php?q=mycustomerpath/hello which can be mapped to any function you like that returns a content that is "themed" into your theme layout.

But in wp, i have no clue how things are done... it's ?p=1 then ?product=1 ... I have searched for documentation of the routing flow but can't find any (google just returns articles on custom routes).. i want to understand the fundamentals of the core routing first..

7
  • digging in the code, i see on each request it calls query_posts? why on earth does it needs to query posts each time? isn't there cases where you don't actually want to display posts??
    – yeahman
    Apr 12, 2014 at 7:49
  • 1
    Contents are saved as post in WP. So, when you need to show contents you need to query it
    – Sisir
    Apr 12, 2014 at 8:36
  • 1
    May I suggests that you read about "the loop" which is the concept that you need to understand to know how WordPress works. Essential "the loop" displays an array of posts which is the result of query_posts. For non-admin URL requests WP is designed to show only posts and it takes custom programming to show something besides a post. Admin URL requests are different and these do not use "the loop" and show non posts things.
    – user27457
    Apr 12, 2014 at 8:52
  • ok but this approach is a bit weird and not very flexible tbh
    – yeahman
    Apr 12, 2014 at 10:07
  • say I want to display a contact form.. i need to put my html in a page content type? I am still trying to find where to put the logic for the form submission... (in the theme page.php? very ugly approach)
    – yeahman
    Apr 12, 2014 at 10:08

1 Answer 1

31

In WordPress, URLs don't map to routes. They map to database queries.

When using WordPress in the "default" permalinks mode, you have a set of variables in the main URL query, like ?p=1 or ?page=234 and so forth. There's also ?s=search and many others.

If you use the "pretty" permalinks, then a big set of rules called the "rewrite rules" is created which directly maps various URL patterns onto this same set of URL parameters. So a URL like /2014/04/12/example would map to ?year=2014&month=04&day=12&postname=example or similar. So the following applies to these as well, after this mapping is done.

These variables are essentially controlling the main instance of the WP_Query class. The WP_Query class holds all the information that builds the database query to get the "posts" from the database. The various parameters passed into it control what kind of query it builds and what data it gets.

See, everything that can be displayed by WordPress is essentially a "post". A blog is a series of posts in reverse time-based order. A "page" is a static post with a defined name. A "custom post type" is exactly what it sounds like, a "post" with a custom type that you define. All main queries to display anything in WordPress are getting some subset of posts from the wp_posts table.

The WP_Query is what does that. And the parameters from the URL are sent directly into that main query and used there.

The theme then determines what template to use based on what the query comes back with. If you requested /category/example, then that becomes ?category_name=example which means that the main $wp_query->query_vars array will get that information, and the WP_Query will pull out the last X posts for the "example" category, and it will set its is_category flag to true.

The template-loader will run after this, see that is_category() returns true, and decide to pick the category template, so it will look for category-example.php and fall back to category.php and so on, according to the Template Hierarchy.

So, the question if you want to change how URLs work is simple: Do you want to change the URLs, or what they are mapped to? Because URLs are not mapped to functions, they're mapped to parameters that control the query. If you want have the URL adjust that main query, then it's a slightly different process than if you want a special URL to run some totally other special code.

And to answer your specific question in the comments: "isn't there cases where you don't actually want to display posts?" No, there is not. Everything is a post. All content is stored in posts. If you want to store content elsewhere and be different, then you can do that, but it's more difficult because, honestly, it's not usually necessary. If you have special content, make a custom post type, store your content as a post with that type, map a URL pattern to it. Easy.

18
  • I understand that everything should be represented in a post (via custom post types etc..) very similar to custom types in drupal 6... but does it affect performance having a single posts table to store every single content of the site? drupal 7 solved it by introducing entity type so that you don't have to create a custom type and store everything in the node table but in your own entity table which can still leverage from the drupal framework. I hope wordpress introduces such approach in the future. thx for the detailed explanation.
    – yeahman
    Apr 12, 2014 at 10:14
  • i guess if i want to map a url to a my own function/theme, wp router would help?
    – yeahman
    Apr 12, 2014 at 10:15
  • 1
    @yeahman: So if you think Drupal is better for a specific case, then use Drupal. It's all free software. Use whatever fits your needs best. It won't hurt WordPress's feelings one bit. I mean, if you were making a wiki, I would not suggest WordPress for that either. :)
    – Otto
    Apr 12, 2014 at 18:48
  • 14
    after more than 1 year working with wordpress now... I still is not convinced by it ... the framework is not elegant and quite ugly... It works as a simple blog but if you want to develop other types of websites.. it's kind of hacking wordpress to do something it was not meant to do.
    – yeahman
    Nov 7, 2015 at 17:47
  • 1
    Nearly all hacks are the result of you having an insecure server or plugin/theme code. WordPress's core is perfectly secure.
    – Otto
    Nov 7, 2015 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.