Wordpress custom rewrite rules not working - wordpress

I want to add a simple custom Wordpress rewrite rule but somehow I don't get this working.
This URL
http://www.gewerbesteuer.de/steuer/muenchen
should call this
http://www.gewerbesteuer.de/index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen.
So I want to call a page, which displays tax rates for a certain city, the city should be in the url as the last part.
Here is my code:
function custom_rewrite_tag() {
global $wp_rewrite;
add_rewrite_tag('%loc%', '([^&]+)');
add_rewrite_rule('steuer/([^/]+)',
'index.php?pagename=gewerbesteuerhebesaetze&loc=$matches[1]', 'top');
flush_rewrite_rules();
}
function query_vars($query_vars ) {
$query_vars[] = 'loc';
return $query_vars;
}
add_action('init', 'custom_rewrite_tag');
add_filter( 'query_vars', 'query_vars' );
The rewrite rule is working but the parameter (in this case loc) is not picked up. Even if I hardcode the rewrite rule with a certain city like
add_rewrite_rule('steuer/([^/]+)',
'index.php?pagename=gewerbesteuerhebesaetze&loc=muenchen', 'top');
it still doesen't pick up the value of the loc parameter. I also noticed that the $matches array is empty and doesn't contain any values.
I went through all the docs at wordpress and the questions here, but couldn't find the problem. Any ideas?
Thanks
Bernhard

Can you try adding this and see if this makes any difference?
Edited
I have changed the paramter in the rewrite rule from "pagename" to "page_id" - I am assuming its 2207 as thats what it said from looking at the body class on your website's page.
I also removed flush_rewrite_rules() from the custom_rewrite_rules_tags function, can you try this now then go to Options > Permalinks and re-save again.
function custom_rewrite_rules_tags() {
global $wp_rewrite;
add_rewrite_tag('%loc%', '([^&]+)');
add_rewrite_rule(
'steuer/([^/]+)',
'index.php?page_id=2207&loc=$matches[1]',
'top'
);
}
add_action('init', 'custom_rewrite_rules_tags');
function custom_query_vars($query_vars ) {
$query_vars[] = 'loc';
return $query_vars;
}
add_filter( 'query_vars', 'custom_query_vars' );

Related

add_rewrite_rule not working with custom post type

I am trying to follow the examples in the wordpress documentation about add_rewrite_rule and add_rewrite_tag.
I'm using a custom post type called "panel".
so the pages look like "site.com/panel/post-slug"
url queries work: for example I have 2:
var1 and testquery.
if I go to "/panel/test-page-auto/?var1=10"
And with var1 and testquery:
so what i want is, change the /panel/test-page-auto/?var1=10&testquery=Hi
-> /panel/test-page-auto/10/Hello
my code:
add_action('init', '__rewrite_tag_rule', 10, 0);
function __rewrite_tag_rule() {
$page_type = "^panel/";
add_rewrite_tag('%testquery%', '([^&]+)');
add_rewrite_tag('%var1%', '([^&]+)');
//Add rule
//panel/test-page-auto/10/edit
add_rewrite_rule(
$page_type."test-page-auto/([^/]*)/([^/]*)/?",
'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=$matches[2]',
'top' );
//add rule for show
//panel/test-page-auto/10
add_rewrite_rule(
$page_type."test-page-auto/([^/]*)/?",
'index.php?pagename=test-page-auto&var1=$matches[1]&testquery=Hi',
'top' );
}
add_filter('query_vars', function($vars) {
$vars[] = "testquery";
$vars[] = "var1";
return $vars;
});
But it's not working.
when I try to put /panel/test-page-auto/10/Hi it just redirects me to /panel/test-page-auto and the queries are blank.
Can someone help me understand what I'm doing wrong?
I have already tried:
save permalinks after each change.
edit the rules in various ways.
test the links using the queries and they are working.

Incorrect routing with wordpress rewrite

I have a page setup with the permalink of mydomain.com/events which uses a page template. This page show all my events pulled in from an external source (so can't be a custom post type). I've then setup rewrites to handle a categories parameter to the url and then single events.
add_action('init', 'mydomain_events_rewrite');
function mydomain_events_rewrite()
{
add_rewrite_rule(
'^events/categories/?$',
'index.php?category=$matches[1]',
'top'
);
add_rewrite_rule(
'^events/?$',
'index.php?event=$matches[1]',
'top'
);
}
add_filter('query_vars', 'mydomain_events_rewrite_var');
function mydomain_events_rewrite_var($vars)
{
$vars[] = 'events';
$vars[] = 'categories';
return $vars;
}
So the idea is categories would just provide the category variable to the events page, for example mydomain.com/events/categories/film. And then individual events would be mydomain.com/events/123/my-epic-film.
As it stands, if I go to mydomain.com/events it just redirects me to the homepage. But if I use the rewrite urls, mydomain.com/events/categories/film it goes to a 404. Where am I going wrong here?
Thanks!
So I was missing the correct regex and the page
add_rewrite_rule(
'^events/category/([^/]*)/?',
'index.php?pagename=events&category=$matches[1]',
'top'
);
add_rewrite_rule(
'^events/([^/]*)?',
'index.php?pagename=events&event=$matches[1]',
'top'
);

Wordpress rewrite url not working

I 've create the page tag with page id 3055 and slug videos-tag. I want to add another segment to the url for queries like http://www.example.com/videos-tag/test so i added a rewrite rule.
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
add_rewrite_tag('%videos-tag%', '([^&]+)');
add_rewrite_rule('^videos-tag/([^/]*)/?','index.php?page_id=3055&q=$matches[1]','top');
}
When i click on this link http://www.example.com/videos-tag/football. I get 404 error page not found?
How do i solve?
Did you already add query_vars on your function ? If so you can change your rewrite function become this code bellow
in this case I see your query parameter is 'q', try to something more unique ie : vidq or something else.
add_action( 'init', 'wpse12065_init' );
function wpse12065_init() {
add_rewrite_rule(
'videos-tag(/([^/]+))?(/([^/]+))?/?',
'index.php?pagename=videos-tag&q=$matches[2]',
'top'
);
}
PS : Don't forget to save permalink
reference URL = http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/

Wordpress rewrite add base prefix to pages only

I have a problem I've encountered when trying to finish a project.
I have the current permalink structure set as /%postname%/
I made my own function on giving a prefix to posts only so my posts are rewritten as /{prefix}/%postname%/.
My problem is that I want to change the permalink of the pages as I did with the posts so my pages will have a prefix like /{prefix}/%pagename%/.
What I tried and didn't work:
Re-declare the PAGES post type and set a rewrite slug.
Tried adding a custom rewrite rule as a function but it didn't work:
$rewrite_rules += array('mycustomprefix/(.+?)/([0-9]+)/([^/]+)/([^/]+)/?$' =>'index.php?pagename=$matches[1]',
Is this possible? Are there any developers out there who encountered the same issue?
For anybody interested, I've fixed my issue in the following manner:
function change_author_permalinks() {
global $wp_rewrite;
// Change the value of the author permalink base to whatever you want here
$wp_rewrite->author_base = '';
// Change the value of the page permalink base to whatever you want here
$wp_rewrite->page_structure = 'static/%pagename%';
$wp_rewrite->flush_rules();
}
add_action('init','change_author_permalinks');
Hope this helps others as I couldn't find any help for this anywhere. For morer information on what you can change this way, check out http://codex.wordpress.org/Class_Reference/WP_Rewrite
Have you updated the permalinks structure after adding this rewrite to your functions.php ? It works for me :)
add_filter( 'page_rewrite_rules', 'customprefix_page_rewrite_rules' );
function customprefix_page_rewrite_rules( $rewrite_rules )
{
end( $rewrite_rules );
$last_pattern = key( $rewrite_rules );
$last_replacement = array_pop( $rewrite_rules );
$rewrite_rules += array(
'mycustomprefix/(.+?)/?$' => 'index.php?pagename=$matches[1]',
$last_pattern => $last_replacement,
);
return $rewrite_rules;
}
I found this solution working for me better... and it's also much cleaner code.
add_action( 'init', 'custom_page_rules' );
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . 'your-page-prefix/%pagename%';
}
I found the code here: http://wpforce.com/change-wordpress-page-permalinks/

How to make Wordpress permalinks ignore custom url rewrite

i'm currently working on a wordpress site that needs to have the option to be offered in french. i've found a way to make the theme work with the fr_FR po and mo files when i add a querystring variable l. i.e.
site.tld will yield the vanilla english site, while site.tld/?l=fr will activate the following code in my functions.php to serve the french translation:
<?php
// http://codex.wordpress.org/Plugin_API/Filter_Reference/locale
add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
if ("fr" == substr(strtolower(trim(strip_tags(stripslashes($_GET['l'])))), 0, 2)) {
// set language to french
return "fr_FR";
} else {
// return original locale
return $lang;
}
}
?>
this setup is already working. my question is: how do i rewrite the url so instead of site.tld/?l=fr i can just prepend the folder structure with fr i.e. site.tld/fr/?
so like if there's a page site.tld/portoflio/autumn/ in english, site.tld/fr/portfolio/autumn/ will spit out the french one. i got this idea from the apple website, where the language is always prepended to the folder structure.
i can already make this happen with an external redirect in htaccess:
RewriteBase /
RewriteCond ^[a-z]{2}/
RewriteRule ^([a-z]{2})/(.*)$ /$2?l=$1 [R,L]
this works, but once i remove the R flag it serves the french-translated 404 not found isntead. i'm guessing what i did is messing-up with wordpress' rewrite rules because i need to use pretty permalinks. right now i'm set to use Month and name (/%year%/%monthnum%/%postname%/) in the admin general options.
my question is, how do i make wordpress ignore the /fr/ part and still serve the correct page/post/etc?
is this even possible? am i on the right track here? i need your help especially doing what's WISE and not just what's NICE. i tried this http://pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api but for the life of me i can't make it work. :/
UPDATE: ok, so here's some progress taking cue from #relu below:
i made sure my rules were canned from .htaccess
i then added the rules from #relu but in the following way, because Relu's always sent me to the same address without the /fr:
<?
// http://pmg.co/custom-wordpress-shortlinks
add_action( 'init', 'pmgtut_add_rewrites' );
function pmgtut_add_rewrites() {
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
add_rewrite_rule( '^([a-z]{2})/(.*)$', 'index.php?l=$matches[1]&q=$matches[2]', 'top' );
}
add_filter( 'query_vars', 'pmgtut_query_vars', 10, 1 );
function pmgtut_query_vars( $vars ) {
$vars[] = 'l';
return $vars;
}
?>
now the /fr stays in the address bar, so that's a good thing, but two problems persist:
wordpress is serving me the main index page. seems like it's not using the &q=$matches[2] part of the rule; and
the locale still doesn't get set properly. i checked if the variable l is getting fed, so i added echo 'l: $l'; after $l = get_query_var('l'); and interestingly i get two echoes: one l: and another l: fr right after that. is the locale filter being run twice? seems like the first time it doesn't see the value of the queryvar, and then there seems to be a second time that it outputs l: with fr already in there.. at the end of the day the locale still doesn't get changed.
aaaah help.
FINAL UPDATE: in my last breath of frustration i searched again, and found this plugin qtranslate. does what i need. thanks y'all, esp to #relu's epic effort and stayin pow'r.
Add this to your functions.php
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['^([a-z]{2})/(.*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['^([a-z]{2})/(.*)$'] = 'index.php?l=$matches[1]&q=$matches[2]';
return $newrules + $rules;
}
// Adding the l var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'l');
return $vars;
}
Haven't tested this, it may need some tweaking, but this is the way to do it.
I've basically adapted the version found here.
It also registers the "l" query var so you can get it's value by calling: get_query_var
add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
$l = get_query_var('l');
if ("fr" == $l) {
// set language to french
return "fr_FR";
} else {
// return original locale
return $lang;
}
}
UDATE: Ignore the above!
Remove the my_insert_query_vars() filter and replace it with:
function register_rewrite_tag() {
add_rewrite_tag('%l%', '([a-z]{2})');
}
add_action('init', 'register_rewrite_tag');
Here are also the updated rewrites for pages and posts, I've looked at WordPress' $wp_rewrite->rules and adapted them to fit this special case:
function pmgtut_add_rewrites() {
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
add_rewrite_rule( '^([a-z]{2})/(.?.+?)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&pagename=$matches[2]&page=$matches[3]', 'top' ); // Pages
add_rewrite_rule( '^([a-z]{2})/([^/]+)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&name=$matches[2]&page=$matches[3]', 'top' ); // Posts
}
maybe out of topic but here's another rewrite rule for languages of the form; en_us/ or jp/
add_rewrite_rule( '^([a-z]{2}_[A-Z]{2}|^[a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );

Resources