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.

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

share|improve this question
    
I linked several WP/stackexchange articles I've studied, but spam preventor wouldn't let me add them. Sorry :( [1]: wordpress.org/support/topic/using-an-extra-parameter-in-an-url [2]: stackoverflow.com/questions/4073599/… [3]: wordpress.stackexchange.com/questions/3206/… [4]: codex.wordpress.org/Rewrite_API –  David Bratvold Dec 15 '10 at 21:31
    
Fixed links for you. :) Spam filter is not that harsh, should warm up to you fast. –  Rarst Dec 15 '10 at 21:46
    
Have you read this? codex.wordpress.org/Function_Reference/WP_Rewrite –  Wyck Dec 15 '10 at 21:47
    
@Wyck, I've seen that page, I'm studying it & copying the code, but it's just not making a whole lot of sense. I've added lots of code bytes & tried various variations, but can't get it going. Almost every code example just gives snippets, but doesn't give a full 100% explanation of what to do with it (where to put it, how to tie into it, ...) Thank you @Rarst –  David Bratvold Dec 15 '10 at 23:00
add comment

2 Answers

up vote 0 down vote accepted

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!
    add_rewrite_rule(
        // The regex to match the incoming URL
        'companies/tasks/([^/]+)/?',
        // 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
        'index.php?pagename=companies&category_slug=$matches[1]',
        // 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;
}
share|improve this answer
    
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. –  David Bratvold Dec 16 '10 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 '10 at 22:58
add comment

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

share|improve this answer
    
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. –  MikeSchinkel Feb 1 '11 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 '11 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 '11 at 9:58
add comment

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.