Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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..

share|improve this question
    
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 at 7:49
    
Contents are saved as post in WP. So, when you need to show contents you need to query it –  Sisir Apr 12 at 8:36
    
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. –  Magenta Cuda Apr 12 at 8:52
    
ok but this approach is a bit weird and not very flexible tbh –  yeahman Apr 12 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 at 10:08

1 Answer 1

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.

share|improve this answer
    
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 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 at 10:15
    
Adding a full routing system is not usually necessary. There are simpler ways. The basis of WordPress is to display user-generated content, which is all stored in the posts table. If you're wanting to display content which is not user generated, then you generally do it in the theme or in a plugin. There are hundreds (thousands) of action hooks and filters and other ways for code to override various parts of the process as the page is generated. And with things like shortcodes, inserting custom html into content is relatively easy. –  Otto Apr 12 at 10:19
    
how do I add custom html/php into a post type i created? without having the modify the theme's single.php or create a single-mycustompost.php (not a very portable approach) –  yeahman Apr 12 at 10:24
    
Shortcodes. codex.wordpress.org/Shortcode_API –  Otto Apr 12 at 10:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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