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;
}
Related
I need to add some of my own php files to an existing Wordpress site (from Bitnami) so it can be accessed from a 3rd-party service. I don't need to access anything from Wordpress in my code, but I need my code to be accessible from a url for 3rd-party services.
Like this:
https://myWordPressSite.com. <- normal WP site
https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- my code
How can I add my directory of code to the Wordpress file structure, and how to access it from a URL?
You can add a $_GET OR $_POST to wp in the plug side
if you want I would add a code to help with not getting hack
Your url https://myWordPressSite.com?name=yes
function getdata(){
if(($_GET['name'] == "yes") || ($_POST['name'] == "Run")){
}
}
This is one way
Good Luck
Karl K
Create a folder in your theme's root folder called myCustomDirectory and place your php file inside that.
then add to your functions.php the following:
This will handle the Rewrite Rules:
add_action('init', 'custom_add_rewrite_rule');
function custom_add_rewrite_rule() {
add_rewrite_rule('^myCustomDirectory/generateSerial.php', 'index.php?generateSerial=true', 'top');
}
This will handle the Wordpress Query Vars:
function add_query_vars_filter( $vars ){
$vars[] = 'generateSerial';
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
This will tell wordpress to pull the correct file:
function load_custom_template($template){
if(get_query_var('generateSerial') ){
$template = get_template_directory() ."/myCustomDirectory/generateSerial.php";
}
return $template;
}
add_filter('template_include', 'load_custom_template');
make sure to flush your rewrite rules when you are done by going to: Settings > Permalinks and clicking 'save changes'
It turns out it was much simpler than I thought, or any of the other answers presented here.
The answer is to just add a directory of my own code inside the Wordpress home folder.
In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:
/opt/bitnami/wordpress
So I created a directory and put my code in it..
/opt/bitnami/wordpress/myDirectory
/opt/bitnami/wordpress/myDirectory/generateSerial.php
/opt/bitnami/wordpress/myDirectory/doSomething.php
And it was then accessible from a url like:
https://myWordPressSite
https://myWordPressSite/generateSerial.php //etc
One small catch is that some Wordpress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation.
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.
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);
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.
I have links in my site that pass queries to pages that query from an external database. This works fine e.g.
mysite.com/catalog/?tl=flooring
however i want to rewrite this url to appear as
mysite.com/catalog/flooring
Ive tried modifying the rewrite rules in wordpress but it always displays the index page
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['(catalog)/([a-zA-Z0-9 ]+)$'] = '/catalog/?tl=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
array_push($vars, 'id');
return $vars;
}
Rewrite rules in WordPress don't quite work like how you're expecting them to. All rewrite rules map to a file handler (almost always index.php), not another URL.
Here is a shortened code example;
$rules['catalog/(.*)/?'] = 'index.php?pagename=catalog&tl=$matches[1]';
array_push($query_vars, 'tl'); // note query var should match your query string
This would map catalog/whatever to the WordPress page 'catalog', and pass along the query var 'tl'. You could create a page template for 'catalog', then pick up the value of 'tl' using get_query_var('tl').
Also avoid using query vars like id - use something more unique (like 'tl') to avoid clashes with WordPress.
And don't flush rules on every init! It's bad practice, and will write to .htaccess and call database updates on every page load!
WordPress will always flush permalinks whenever you update a post or your permalink structure (simply update your permalinks when you make changes to your code).
This article over at Raazorlight goes into a bit more detail on the process of grabbing querystrings and URL rewriting in WP.
Also, check the comments there to see why calling flushRules() may not be necessary if you re-save permalinks. Optionally, flushRules() can be called just once during plugin activation callback.
Digging deeper, commenter 'pmdci' links over to an instructive post/saga on the related topic of passing a query to a custom post type using a custom taxonomy.