Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress add_rewrite_rule for page redirecting incorrectly

I am trying to add custom rules using wordpress's add_rewrite_rule function, but they don't work. The weird thing is that the rules are added and match my URL whenever I inspect them with the Monkeyman Rewrite Analyzer plugin.

When I try to access the page, it redirects to an incorrect location.

Here is my code:

add_rewrite_tag('%cc%','([a-zA-Z]+)');
add_rewrite_tag('%id%','([0-9]+)');

add_rewrite_rule('job-details/([0-9]+)$', 'index.php?pagename=jobs&cc=show&id=$matches[1]', 'top');

global $wp_rewrite;
$wp_rewrite->flush_rules();

As for my URLs, if I try to access the URL site.com/job-details/1 , it redirects me to /jobs/ , which is the page associated with my plugin.

Here is my .htaccess file

RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
like image 665
Samer Bechara Avatar asked Sep 01 '12 10:09

Samer Bechara


1 Answers

Turned out that my script was using a custom function to grab the GET parameters via the $_REQUEST object, which was being nullified by wordpress.

I used the following code to grab it instead.

global $wp_query;
$wp_query->query_vars['cc'];
like image 120
Samer Bechara Avatar answered Nov 03 '22 19:11

Samer Bechara