Using custom rewrite rules in Wordpress - php

I am trying to create custom rewrite rules in WordPress so that I can pass data to a page based on what is in the query string.
My page works fine:
/retailers-a-z/?merchant=some_merchant
and then also
/retailers-a-z/?merchant=some-merchant&offer=some-offer
I first tried to create a rewrite rule for this in .htaccess but since realised WordPress has it's own internal redirection database.. so after a lot of research I managed to come up with the following code... However, it is still not working.. Whenever I try to access
/retailers-a-z/some-retailer or /retailers-a-z/some-retailer/some-offer it just loads the home page.
functions.php:
function wp_raz_rules()
{
add_rewrite_rule(
'retailers-a-z/([^/]+)/?$',
'index.php?merchant=$matches[1]',
'top'
);
add_rewrite_rule(
'retailers-a-z/([^/]+)/([^/]+)/?$',
'index.php?merchant=$matches[1]&offer=$matches[2]',
'top'
);
add_rewrite_tag('%merchant%', '([^/]+)');
add_rewrite_tag('%offer%', '([^/]+)');
}
add_action('init', 'wp_raz_rules');
function wp_raz_vars($vars)
{
$vars[] = 'merchant';
$vars[] = 'offer';
return $vars;
}
add_filter('query_vars', 'wp_raz_vars');
I then believe I can access them with get_query_var('merchant') and get_query_var('offer') instead of $_GET[]
Any ideas? Thanks:)

Managed to fix it by explicitly defining the page name in the query string.
index.php?pagename=retailers-a-z&merchant=$matches[1]&offer=$matches[2]
Not 100% sure if it is the correct way of doing it.

Related

Wordpress add parameters with friendly url structure

I build a Wordpress website with a page called 'Nieuwsberichten'.
The url looks like
https://www.example.com/nieuwsberichten
This page is dynamic and I need an extra parameter called 'news_page'
https://www.example.com/nieuwsberichten/?news_page=recent
The URL above is not friendly so I create a rewrite rule for this and I add the variable 'news_page'.
functions.php
function add_query_vars($vars){
$vars[] = 'news_page';
return $vars;
}
add_filter( 'query_vars', 'add_query_vars', 10, 1 );
function rewrite_paged(){
add_rewrite_rule('^nieuwsberichten/([0-9]+)/?', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');
After this I flushed the permalinks
menu -> settings -> permalinks -> save
If I browse to
https://www.example.com/nieuwsberichten/recent/
It will redirect to
https://www.example.com/nieuwsberichten/
What do I miss in my code?
Hope somebody can help me with this
In the regex part, you specify an entry for numbers only, but you pass text like recent in your url. Maybe the error is here. Please check if it works with number input, edit your expression if that's the problem. Also, the url structure you specified does not seem to be quite correct.
Anyway, I'm not sure of my answer so I'd appreciate it if you could confirm after checking.
https://developer.wordpress.org/reference/functions/add_rewrite_tag/
This may be the function you should use for the parameters you add. This function will also not require the query_var function. So I think you won't need to use the other function.
I'm not in a position to check or test right now. If your problem is not solved, I will look into it in detail.
You can use it like this:
function rewrite_paged(){
add_rewrite_tag('%news_page%', '([^/]+)');
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');
Also if you are pointing to a page you will need to use page id or pagename. A blog or a product etc. If it is, there will still be parameters you need to use and you should redirect this url to index.php. I'm leaving an example for you to understand what I'm saying, and for your review, you can take a look at the rewrites on your site as follows:
Review examples:
global $wp_rewrite;
print_r($wp_rewrite);
Examples:
Match Pagename:
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?pagename=nieuwsberichten&news_page=$matches[1]', 'top');
Match Page ID:
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?page_id=10000&news_page=$matches[1]', 'top');
If you have an installation outside of wordpress your example is correct but if it will be inside wordpress all requests should be directed to index.php. Therefore, you may need to edit the code I have given like these examples.

pass variable through url in wordpress

I have wordpress custom template page test.php in which I want to pass variable throuh url like,
http://localhost/wordpress/test?date=2017&postid=11&title=some-title
in this format,
http://localhost/wordpress/test/2017/11/some-title
and grab the parameter from the url.
I have followed this link, but couldn't produce the result as I have desired.
Is there another way to achieve the desired result as I have mentioned??? I am pretty newbie to wordpress theme development.
Well, as suggested by #vl4d1m1r4 , I have used the web server(in my case apache) rewrite rule to rewrite the path. Some-what I have achieved the desired result.
In function.php
I have written the following code to update the rewrite rule in wordpress(.htaccess)
add_filter( 'query_vars', 'add_custom_query_vars' );
function add_custom_query_vars( $vars )
{
array_push($vars, "test_year", "test_postid", "test_title");
return $vars;
}
function custom_rewrite_rule()
{
add_rewrite_rule('^test/([^/]*)/([^/]*)/([^/]*)?','index.php?pagename=test&test_year=$matches[1]&test_postid=$matches[2]&test_title=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule');
After adding the above code, I have re-saved the permalink settings(setting>>permalink)
To Get the parameter in custom-template-page i.e test.php. I have used the following query.
echo get_query_var('test_title');
echo get_query_var('test_postid');
echo get_query_var('test_year');

Wordpress add_rewrite_rule Redirects without parameter

I'm want to redirect a custom URL same like example in docs here
EX: http://domain.com/find/324 to http://domain.com/?text=324
This is the code , for some reason it keeps redirect me to homepage without the parameter "text" in the URL..
function custom_rewrite_basic() {
add_rewrite_rule('find/(.+)/?', 'index.php?text=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
The "text" parameter is not a build in WoprPress function, i'm going to use it with custom code of mine.
I flushed that links cache, still redirects to homepage without the parameters.
What i'm missing?
You cannot add custom query vars directly. Custom query vars have to be added using the query_vars filter to be parsed within rules. You can add custom query vars as seen below.
function wpd_add_query_vars( $qvars ) {
$qvars[] = 'text'; //I used the one in your example
return $qvars;
}
add_filter( 'query_vars', 'wpd_add_query_vars' );
Then you get get the query vars like this:
get_query_var('text')
check https://wordpress.stackexchange.com/questions/271931/add-rewrite-rule-parameter-is-not-received-by-the-page?rq=1 for more detail.

WP URL Re-writing plugin

I am trying to integrate a few non-wordpress PHP pages into an existing Wordpress site. Ideally, I want to rewrite anything request that looks like this: 'domain.com/books/bookname' to 'domain.com/catalog.php?title=bookname'. I have been messing around with the WP Rewrite API for several hours, but I just can't get anything to work. Here is my current plugin code:
register_activation_hook( __FILE__, 'wbm_catalog_activate' );
function wbm_catalog_activate() {
wbm_catalog_add_rules();
flush_rewrite_rules();
}
// Flush when deactivated
register_deactivation_hook( __FILE__, 'wbm_catalog_deactivate' );
function wbm_catalog_deactivate() {
flush_rewrite_rules();
}
add_action( 'init', 'wbm_catalog_add_rules' );
function wbm_catalog_add_rules() {
add_rewrite_rule( 'books/([^/]+)/?',
'catalog.php?title=$matches[1]', 'top' );
}
I can even inspect the $wp_rewrite object and see my new rule added, but whenever I try to visit a rewritten url, wordpress acts as though the rule were not there.
I have been banging my head up against a brick wall, and I would appreciate any help you could give!
The problem is:
The statement flush_rewrite_rules is executed before add_rewrite_rule and therefore the new cache generated does not include your stuffs in add_rewrite_rule.
You can verify with the value in your DB
select option_name, option_value from wp_options where option_name = 'rewrite_rules';
I was unable to ever resolve this issue. The site that I was working on was part of an old WordPressMU setup (2.8) with a number of sites. I did not have access to the .htaccess that the MU install was using, and it was not passing any requests through to the .htaccess file that I could edit.

Conditional URL Rewrites in WordPress

I'm hosting an event and using WordPress to manage the site. We have a separate table (not WP user table) of attendees and I'd like for each of them to have a page on our site:
example.com/user1, example.com/user2, etc.
I've been reading up about WP_Rewrite but am a bit confused about how to conditionally redirect if the user exists, otherwise go about the normal flow.
Any help in achieving this would be much appreciated, thank you!
Edit:
There is a single one-off page that I'm trying to forward to. Right now, it's accessed with:
example.com/user?username=user1
If i understood your problem, you're going to create a page for each user, so in the permalink structure add tag %postname%, and maybe install no-category base plugin.
Check if the .htaccess it's created with the configuration that wp write for that structure, if it's not you can just copy and paste.
I ended up doing this by prepending a user's url with a tilde (~). The rewrite code is as follows:
// Uncomment only when new rewrites have been made
//add_filter('init', 'wds_flush');
// Leave uncommented
add_filter('rewrite_rules_array', 'wds_user_rewrite');
add_filter('query_vars','wds_query_vars');
function wds_flush()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function wds_user_rewrite($rules)
{
global $wp_rewrite;
$newRule = array(
'^~(.+)$' => 'index.php?pagename=user&uid=$matches[1]&act=show',
);
$newRules = $newRule + $rules;
return $newRules;
}
function wds_query_vars($vars)
{
array_push($vars, 'uid');
array_push($vars, 'act');
return $vars;
}

Categories