0

I've got a page with my own widget inside of it which needs some params from the url to produce dynamic db results - don't worry everything is sanitized well and I only let this part out to keep this slim and concentrated on routes which is the crucial part for me ( being wp newbie).

Is it possible to set a custom route in WP with a string and number inside of it and let it point to an existing page identified by slug? I found somewhere route examples but not with a wildcard string and only working with templates - in this case I need an page as the endpoint.

In laravel 3 I would do the following:

// Calling following url:
// http://mypage.com/items/my-red-toy/123
//
// Will produce:
// You String is: my-red-toy
// The Number is: 123

Route::get('pageslug/(:any)/(:num)', function($anystring, $number)
{
    echo "You String is: ".$anystring;
    echo "The Number is: ".$number;
});

Or as rewrite rule in .htaccess (even if not a pretty solution...) :

RewriteRule ^items/([a-z]*)?/?([0-9]*)?/?$ /items [L]

This rewrite rule would work in my usual php pages but only works partialy in wp. It'll work with:

http://mypage.com/items/12314 

but not with

http://mypage.com/items/my-red-toy 

nor with

http://mypage.com/items/my-red-toy/1231

Somehow only one uri segment with numbers inside seems to be allowed.

Could someone point me to the solution? I'm to new to Wordpress so I don't know where to start...

  • 2
    If you feel home with Laravel routing I suggest this plugin (it's mine). Be aware it's beta version. – gmazzap Nov 15 '13 at 15:10
  • Looks promising so far. Why don't you post it as a regular answer? And if you would be so kindly please add an brief example. I'm sure that there're some more interested people around. – Hexodus Nov 15 '13 at 15:38
  • 2
    I don't think the mine is really an answer, and also this is not the place for my plugin usage example. However I write a pastebin for you. – gmazzap Nov 15 '13 at 16:08
  • Wow you made my day Giuseppe! This works like a charm and indeed THIS IS THE SOLUTION for my problem even if you don't post it as answer which is very pity. I think that all the folks should know about your great plugin! THANK YOU SO MUCH! – Hexodus Nov 15 '13 at 16:59
  • 2
    if you want to understand how this actually works in WordPress, have a look at add_rewrite_rule in codex. – Milo Nov 15 '13 at 17:04

Your Answer

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

Browse other questions tagged or ask your own question.