Tom Yates – Web developer based in Manchester
Specialising in PHP, HTML, JavaScript, CSS, Yii, Flash, AS3
Show MenuHide Menu

Category Archives: Wordpress

Passing Parameter to WordPress using Friendly URL

January 22, 2012

This took me bloody ages to sort out, I must have had the redirect syntax wrong, but after much fretting, I got it working:

Add the following to the functions.php


add_rewrite_rule('photo/([^/]+)/([^/]+)/?','index.php?pagename=photo&pid=$matches[2]','top');
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars )
{
    $query_vars[] = 'pid';
    return $query_vars;
}

This will redirect photo/a-lovely-seal/34 to index.php?pagename=photo&pid=34 so we can load photo id 34 and we have a nice SEO friendly URL for sharing.

You need to flush the redirection rules once after creating to add them to the database – you should only do this after each time you change the rules as it’s an expensive operation:


flush_rewrite_rules();