• Home
  • About
  • Portfolio
  • Hire me
  • Templates
blog.lopau.com

Just Another Dang Blog

A tech blog about web development, graphic designs, freelancing, cloud computing, mobile development, innovations and seo.
Welcome to lopau.com - a digital playground, online portfolio, web experiments and blog of Paulo Orquillo
« Installing GoDaddy SSL on an EC2 Ubuntu Instance in AWS
3D Augmented Reality platform(QR+AR) for iPhone, iPad, Android, BB »
Aug 29
Create rewrite rules for friendly url for WordPress plugin custom queries

I have this personal web application that was built from scratch last year and has already been considerably indexed by google. I used some Apache mod rewrite to make the urls friendly. Last week I decided it would be faster/easier to jumpstart the project again if I just focus on my application and just use a framework to handle other intracacies like abstractions, security and etc. I was aiming for CodeIgniter but got a glimpse on BuddyPress for WordPress(not a framework) which has most of the features I want for my app. So long story short I ended up porting my application to be a plugin for WordPress.


One thing that got me stuck for awhile was how to recreate my old URLs. Posting some tips for an example on how I got the issue solved.

Target: Make WordPress rewrite this URLs

http://www.thesite.com/mypage/food/

to

http://www.thesite.com/mypage/?ingredients=food

http://www.thesite.com/mypage/food/asia/

to

http://www.thesite.com/mypage/?ingredients=food&origin=asia

1. Create your plugin and create a shortcode

(eg. add_shortcode(‘myapp’, ‘myfunction’); )

2. Create a new Page and add the shortcode

[myapp]

With Permalinks off your page looks like

http://www.thesite.com/?page_id=5

And is basically the same as

http://www.thesite.com/index.php?page_id=5

With Permalinks on

http://www.thesite.com/mypage/

3. If you pass a query on your app the URL may look like this, Im passing the queries on the URL

http://www.thesite.com/mypage/?ingredients=food

or

http://www.thesite.com/mypage/?ingredients=food&origin=asia

Depending on the queries passed the contents on your page changes.

4. Now to rewrite the URLs to be friendly you need to add a filter and use query_vars

add_filter(‘query_vars’, ‘my_query_vars’);
function my_query_vars($public_query_vars) {
$public_query_vars[] = “ingredients”;
$public_query_vars[] = “origin”;
return $public_query_vars;
}

This ensure that when WordPress parses the URL, the ingredients and origin gets saved in the query variables.

4. Next is create your rewrite rules and store in an array

add_filter(‘generate_rewrite_rules’, ‘my_rewrite’);

function my_rewrite($wp_rewrite) {
$wp_rewrite->rules =
array_merge(array
(
‘^mypage/([^/]+)/?$’ => ‘index.php?page_id=5e&ingredients=$matches[1]‘,
‘^mypage/([^/]+)/([^/]+)/?$’ => ‘index.php?page_id=5&ingredients=$matches[1]&origin=$matches[2]‘

), $wp_rewrite->rules);
}

Note: That you are rewriting to the original URL of the page. You are storing each rewrite rule to an array and finally adding them to a filter by generate_rewrite_rules.

5. To get the variables back just call the get_query_vars function on your plugin.

echo get_query_var(‘ingredients’);

So this is how I got my problem solved. Hopefully it would be a great help to others. Happy rewriting and coding!


This entry was posted on Monday, August 29th, 2011 at 10:23 pm and is filed under Blogs, Wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

  • Popular Posts

    • Fixing power problems with your LG Flatron Monitor (LG1753S)
    • Finally passed my Force.com Certified Developer Exam
    • DIY: HP Laptop Power Adapter Repair
    • How to Setup PHP, MySQL and phpMyAdmin on Mac OS X Snow Leopard
    • Setup EC2 and FileZilla with PPK file on Mac OS X
    • Safari Bug on Lion: The application Safari canceled restart

    Recent Comments

    • Swati: I am planning to write dev 401 next month.Could you please send me the materials and dumps that you have.My...
    • Apex: Great tut,Thanks
    • matt: Never been a fan of Steve Jobs or Bill Gates as they do seem to steal other peoples ideas then use them as...
    • Ash: I encountered the same issue, I went to activity monitor, checked on windowed processes and force killed Safari,...
    • lasersoul: Hi Guys Similar problem with a LG W2452T near enough to effect the fix in the same way. 4 bulging...
    • Ione Bentler: Actually there is another alternative. Get a refund. That’s what I did. I was so pissed off that...
    • Recent Posts

      • R.I.P Steve Jobs, Stay Hungry Stay Foolish
      • Inkling by Wacom
      • Create a Web Service using SOAP
      • 3D Augmented Reality platform(QR+AR) for iPhone, iPad, Android, BB
      • Create rewrite rules for friendly url for WordPress plugin custom queries
    • Blogroll

      • Abiel Online
      • Abiel's Tech Blog
      • Copongcopong’s Notes from Walk the walk
      • Get Rich Talks
      • Javi’s Go Blog
      • The Passionate Pilgrim’s Musings and Ramblings
      • Tinokla’s Scribbles and Doodles
    • Links

      • LS Blogs
      • Pampanga Province Websites
      • Top Blogs Philippines
      • Travel and Gobble
    • Proudly Pinoy!
    • Add to Technorati Favorites
    • Web Development & Design - Top Blogs Philippines

    • Sponsored Ads

      Summer Savings! $7.49 .com
      Ensogo
    • Categories

      • Adsense
      • Airsoft
      • Android
      • apple
      • Blogs
      • Browsers
      • Cloud Computing
      • CSS
      • Desktop Publishing
      • Facebook
      • Firefox
      • Flex
      • Fonts
      • Free WP Templates
      • Freelance
      • Google
      • Hardware
      • HTML 5
      • iOS
      • Ipad
      • iPhone
      • IT
      • Javascript
      • microsoft
      • Mobile Development
      • Online Scams
      • Others
      • Personal
      • Personal Finance
      • Photography
      • Photoshop
      • PHP
      • Political
      • Projects
      • SalesForce
      • Security
      • SEO
      • Social Marketing
      • Travelling
      • Troubleshooting
      • Tutorials
      • WAMP
      • Web Resources
      • Wordpress
    • Tags

      Adsense among ed andriod apache apple blogging Blogs Cloud Computing CSS cuil development earn online enable gd library Firefox Fonts force.com Freelance godaddy Google google analytics government increase traffic internet fraud internet scams iPhone knol knowledge content Money mysql online purchase page rank pagerank PHP politics pr search engines SEO social media content social networks text browser themes web traffic western union Wordpress zend certification

    Just Another Dang Blog is proudly powered by WordPress
    Entries (RSS) and Comments (RSS).
    Valid XHTML 1.0 Transitional Valid CSS!