I'm creating a separate (simple) directory that does not use any internal WP permalinks or pretty link structure. I've been studying how to make my own custom SEF URLs for this part of my site, and almost have it (or thought so).

I have a separate table with records like this:

id   |   section    |  category   | co_name
1    |  Consulting  | Innovation  | Innocentive.com
2    |   Tasks      | Employment  | Microtask.com

The directory shows a page of all the sections & categories from the database at this URL: dailycrowdsource.com/companies/ (I did this with a companies.php file & created a 'page' with the slug 'companies')

I want this URL to display all the 'employment' category listings: dailycrowdsource.com/companies/tasks/employment/

I can handle the php & mySQL, but I don't know how to make the links pretty. This works http://dailycrowdsource.com/companies/?category=poll

This helped using an extra parameter in an URL, & I think I got it working, but I couldn't figure out how to convert the < a href=.... into the SEF link.

I prefer to do this in my functions.php file, as I haven't written a plugin yet. I've done this in Joomla with the router.php file.

In my research ( Need help with friendly URL's in Wordpress, Custom Post Type Rewrite Rule for Author & Paging?, Rewrite API ), I'm seeing a lot of the same tags & buzzwords, but no one explains how to change the actual link. Does WP do it automatically? (In Joomla you have to do: JRoute::_('index.php?var1=great&var2=more'); for the conversion to take place.)

I realize the first answers will tell me to use some built in taxonomy of WP, but I need this to be updated easily. And by adding a record to the database with a brand new section or category, my directory needs to be instantly updated (that's why I've chosen this method - no administration).

I would appreciate someone showing me how to add in hooks/filters/etc to make a custom SEF URL. (All URLs will start with dailycrowdsource.com/companies/ ). (I'm hoping Mike Schinkel reads this - as I got a lot of help from his responses to similar posts)

Thank You -David


2 Answers 2


Modifying the URL structure always consists of two parts: one two modify the URLs you generate with your code, and one to handle the incoming URLs of the new structure. The latter part is done by modifying the rewrite rules.

I have just written a detailed explanation of the relevant rewrite functionality in a response to a similar question, so here is just the code for your situation:

add_action( 'init', 'wpa5444_init' );
function wpa5444_init()
    // Remember to flush the rules once manually after you added this code!
        // The regex to match the incoming URL
        // The resulting internal URL: `index.php` because we still use WordPress
        // `pagename` because we use this WordPress page
        // `category_slug` because we assign the first captured regex part to this variable
        // This is a rather specific URL, so we add it to the top of the list
        // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
        'top' );

add_filter( 'query_vars', 'wpa5444_query_vars' );
function wpa5444_query_vars( $query_vars )
    $query_vars[] = 'company_slug';
    return $query_vars;

The first part, writing your URLs to match this new pretty structure, is not bundled in one place but spread over all the functions that generate URLs. You will need to write a function to handle your case yourself, since you generate a page URL and add the category part to it. It could look like this:

function wpse5444_company_link( $category_slug = '' )
    $company_page_id = 42; // Somehow convert the page slug to page ID
    $link = get_page_link( $company_page_id );
    if ( $category_slug ) {
        // trailingslashit() makes sure $link ends with a '/'
        $link = trailingslashit( $link ) . 'tasks/' . $category_slug;
        // user_trailingslashit() adds a final '/' if the settings require this,
        // otherwise it is removed
        $link = user_trailingslashit( $link );
    return $link;
  • Jan, your explanation of this answer (wordpress.stackexchange.com/questions/5413/…) is phenomenal. Thank you very much. Of all the pages I've read, no one explained WP's Rewriting is fairly uni-directional. I was expecting it to change my href's. You should turn this into a tutorial. Your answer was clear & very detailed. I'm currently NOT using wpse5444_company_link() because I can just make the pretty URLs myself (less resources). Please let me know if that's a bad idea. Dec 16, 2010 at 19:18
  • @David: A function has the advantage that it can adapt to multiple settings, that you can add a filter to it, and maybe that the parsing and generating code are closer to each other. But if you do this for one site, not as a plugin to re-use, this doesn't matter that much. I have no preference for one way or the other.
    – Jan Fabry
    Dec 16, 2010 at 22:58

How would you go about creating an XML sitemap from custom Pretty URL's that are outside of the wordpress realm?

  • You've asked a question in the place where answers are supposed to be posted. Please ask your own question here and delete this one. Feb 1, 2011 at 2:34
  • Colin, first of all, don't use the answers section to post additional questions. Second of all, "outside of the WordPress realm" doesn't apply to WordPress or belong on WordPress Answers ...
    – EAMann
    Feb 1, 2011 at 2:35
  • @EAMann: It can belong here, if you want to extend a sitemap you generate for WordPress with extra URLs from another system. But we'll have to see when colin asks his new question.
    – Jan Fabry
    Feb 1, 2011 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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