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 not really sure how to phrase the questions, but hope it's not too confusing.

Basically I've create a plugin which inject custom tabs into the posts content when displaying. For now, just assume I'm using DomTab, and I have permalink configured as /%category%/%postname%/. Now I would like achieve 2 things with the tabs.

  1. Beautify the url by appending the tab name to the url according to the tab highlighted. e.g. If tab "Detail" is clicked/highlighted, the url should change from http://myhost/catgegory/post1/ to http://myhost/catgegory/post1/detail/ without requiring to refreshing the page, much like how google did it. The content of all the tabs are preloaded and hidden.
  2. When url with the tab name are access directly, say from a bookmark, the tab should be highlighted automatically. e.g. If user access http://myhost/catgegory/post1/comment/ then the "Comment" tab should highlighted instead of the default tab when displayed.

I don't really need a full solution to this, but hints of where to start. As I understand query, but not sure how to access it with permalink enable.

Thanks

EDIT: Currently accessing http://myhost/catgegory/post1/comment/ ends up in 404

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I've found out the solutions, though not perfect, but it gets the job done. Following is the code

add_action('init', 'my_init');

function project_ace2_init()
{
    global $wp;
    $wp->add_query_var('tab'); // Needed so get_query_var('tab') will work
    add_rewrite_rule(
        //'(.+?)/([^/]+)(/([^/]+))?/?$', //Original : '(.+?)/([^/]+)(/[0-9]+)?/?$'
        '((?:(?!page|tag|category|author|search|feed|type))[^/]+)/([^/]+)(/([^/]+))?/?$'
        'index.php?category_name=$matches[1]&name=$matches[2]&tab=$matches[4]',
        'top'
    );
}

The rule is taken from the default Wordpress rules for permalink as /%category%/%postname%/, I just modified the last numeric portion into alphabetical. This method will require refreshing of page when changing tab, but it's good enough for now.

Next is getting the tab name so I can be highlight, which is simpler with just this $selected_tab = get_query_var('tab');

EDIT: Updated rewrite rules to fix bug where page/2/ is captured

share|improve this answer

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.