WP URL Re-writing plugin - php

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.

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.

Setting up custom permalink on a wordpress page with url variables, getting 404 with variable set

I have tried alot of answers for the same question here on stackoverflow but I cannot get it to work.
Im going to make a custom profile plugin using the permalink /profile/user. So far I have made a page called "profile" with a shortcode to my plugin, which is currently just printing the username from the url. If I go to website.com/profile?username=test without any changes to functions.php it will load the plugin and display the username.
When I try to rewrite this to a permalink it will give me a 404 error, however I can access $wp->query_vars["username"] so atleast that works. Here is the code I use located in functions.php.
add_filter('rewrite_rules_array','mycode_add_rewrite_rules');
function mycode_add_rewrite_rules($rules){
$newrules = array();
$newrules['profile/([^/]+)/?'] = 'index.php?pagename=profile&username=$matches[1]';
return $newrules + $rules;
}
add_filter('query_vars','mycode_add_rewrite_query_vars');
function mycode_add_rewrite_query_vars($vars){
array_push($vars, 'username');
return $vars;
}
After reading more about it I also tried adding a add_rewrite_endpoint on the profile
add_action('init', 'mycode_add_endpoints');
function mycode_add_endpoints()
{
add_rewrite_endpoint('profile', EP_PAGES);
}
I also tried an endpoint on username aswell without success. I have flushed permalinks everytime I made a change.
Worth noting is that after adding the code to functions.php both the permalink and normal url will give a 404.
What am I missing?
I ended up using this guide: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule#Using_Custom_Templates_with_custom_querystring
However, it still did not work, I then changed the 'username' to another name and it worked. I do not know if that would have worked on my original code but I guess it would. Here is the code I ended up with:
function custom_rewrite_tag() {
add_rewrite_tag('%unique_username%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('^profile/([^/]*)/?','index.php?page_id=2634&unique_username=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

Best way to have script urls in WordPress

I am developing my own theme for private use and i have a lot of scripts working with urls with extra parameters.
I'm finding my self adding a lot of empty pages to my site just to be able to connect some php file to specific url so i will be able to send parameters to that specific address.
Is there a better way i can have urls that can be associated with specific php files without adding pages or other things to the site. just like work out of the box when installing my theme?
You should try like this code:
//page url: http://www.example/myurl
add_action( 'init', 'my_rewrite' );
function my_rewrite() {
global $wp_rewrite;
//wp-content/plugins/myplugin/myurl.php
add_rewrite_rule('myurl/$', WP_PLUGIN_URL . '/myplugin/myurl.php', 'top');
$wp_rewrite->flush_rules(true);
}
More info:
https://developer.wordpress.org/reference/classes/wp_rewrite/
https://codex.wordpress.org/Class_Reference/WP_Rewrite

Using custom rewrite rules in Wordpress

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.

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