Make WordPress show the same page on two different url patterns - wordpress

I need to make WordPress show the same page on two different URLs:
https://sitename.com/%postname%/
https://sitename.com/%postname%/tail/
* I can only use functions.php

I used a few similar cases and guides and here is code which worked out for me:
function tail_rewrite()
{
add_rewrite_rule('^([^/]*)/tail?', 'index.php?name=$matches[1]', 'top');
// first parameter for posts: p or name, for pages: page_id or pagename
}
add_action('init', 'tail_rewrite');
don't forget to flush the rules by visiting Settings > Permalinks
OR use flush_rewrite_rules() in the plugin activation (don't execute it at every page load).

Related

How to create custom route to a page in WordPress

I want to set a new Route in WordPress that will be redirected to the desired path by entering a specific pattern.
for example:
When I enter any url like below:
http://mywebsite.com/audio/Everything
http://mywebsite.com/audio/**********
http://mywebsite.com/audio/0123456789
These routes and similar ones redirects to sound.php
http://mywebsite.com/wp-content/sounds/sound.php
You can use add_rewrite_rule function, Here is official WP reference - https://developer.wordpress.org/reference/functions/add_rewrite_rule/
As per your example above, You can do like below,
add_action('init', 'custom_rewrite_rule', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('^audio/([^/]*)/?','wp-content/sounds/sound.php','top');
}
Try above code as per your requirement and add into your theme's
function.php OR plugin file.
After code added, You must Go to Settings > Permalinks page on your site admin panel and do click on Save Changes button.

Home page url rewriting Wordpress

I want to do a URL rewrite in the WordPress home page
I want to change my URL http://mysite.loc/?pays=senegal to look like http://mysite.loc/senegal.
The problem is that I am on the WordPress home page, so it will be confused with the URL of another page like http://transfert.loc/page-example.
I have already tried several optins but am completely blocked.
Here is my code example:
public function rewrite_urls(){
add_rewrite_tag( '%pays%','([^&]+)' );
add_rewrite_rule(
'([^/]+)',
'index.php?pays=$matches[1]',
'top'
);
}
Can someone help me please!
Thanks
Two problems I see- rewrite rules need to set query vars that will result in a successful main query. Setting just a custom var like slide doesn't parse to anything WordPress can load. Additionally, slide needs to be added to the recognized query vars for it to get parsed within a rule.
So, what would a rule look like that would load the front page posts in the main query? That's a good question- the posts page is a special case, the absence of any other query vars. I haven't found a way to do that with a rule, though it may exist.
An easier way to do this is with a rewrite endpoint:
function wpd_endpoint(){
add_rewrite_endpoint( 'page-example', EP_ROOT );
}
add_action( 'init', 'wpd_endpoint' );
Keep in mind that if you have code accessing values via $_GET, this still won't work, because WordPress doesn't put query vars there when rules are parsed. You can change the code to use get_query_var, or just assign it before the code tries to access it:
$_GET['page-example'] = get_query_var('page-example');

Wordpress Url Rewrite with Multiple Parameters and Templates - Dynamic Path/Parameter Placement

We're having some trouble figuring out WP URL rewrite rules when we have tokens separated by parameters. Very briefly, we'd like to have the following common format for user pages and video libraries:
/users/steve/videos/2
...where "users" is the PHP page template that displays details on a user. So if we go to:
/users/steve/
...we see Steve's home page. If we go to:
/users/steve/videos/
...we hit the page template for the video library, and we see Steve's list of videos. And if we go to:
/users/steve/videos/2
...we hit a third page template, the video player page, where we see video id 2 displayed.
So we have three separate page templates, and we would like the child / parent relationship between them to be user page -> video library page -> video player page, for our breadcrumbs display.
The trick we can't figure out is that "steve" and "2" are really params inserted into the url, interrupting the parent/child token relationship. And second, for the video player, there really isn't a token there at all that maps to the video player page template. It's just the presence of the "videos" token plus the additional numerical parameter that tells us this maps to the video player page template.
As alternative, we've figured out how to get /users/videos/steve/2 working. We've added this rewrite rule to our child theme's functions.php file:
add_rewrite_rule(
'^users/videos/([^/]*)/([^/]*)/?',
'index.php?pagename=users/videos&users_filter_name=$matches[1]&user_video_id=$matches[2]',
'top');
(we've also defined the variables "users_filter_name" and "user_video_id" and we are successfully parsing them in the template)
But since what we really would like is /users/steve/videos/2 we tried updating the rule to:
add_rewrite_rule(
'^users/([^/]*)/videos/([^/]*)/?',
'index.php?pagename=users/videos&users_filter_name=$matches[1]&user_video_id=$matches[2]',
'top');
That didn't seem to work (it just loaded the '/users/' page) so we're obviously doing it incorrectly. Is what we want achievable with this structure?
Thanks!
The link structure of /users/steve/videos/2/ format is not a problem for Wordpress, and you actually wrote all the right code, but there is a small mistake somewhere.
Most likely you have a problem in the code section where you add new variables to query_vars.
Perhaps you are adding a second variable in too late action.
When Wordpress sees an obscure variable, it often behaves as you described.
Try removing all of your code and copy this to functions.php top
add_rewrite_rule(
'^users/([^/]*)/videos/([^/]*)/?',
'index.php?pagename=users/videos&users_filter_name=$matches[1]&user_video_id=$matches[2]',
'top');
add_filter( 'query_vars', function( $vars ){
$vars[] = 'users_filter_name';
$vars[] = 'user_video_id';
return $vars;
} );
If it doesn't work, try changing the code to this
add_rewrite_rule(
'^users/([^/]*)/videos/([^/]*)/?',
'index.php?page_id=54&users_filter_name=$matches[1]&user_video_id=$matches[2]',
'top');
add_filter( 'query_vars', function( $vars ){
$vars[] = 'users_filter_name';
$vars[] = 'user_video_id';
return $vars;
} );
Where 54 = your page ID
After each change you must go to the settings and turn off/on permalinks to reset the cache
I tested this code on my demo it works 100%.

Creating pretty profiles in Wordpress

I have been working with wordpress for a while now, but one aspect I never tried, until now, is the rewrite rules. I can create a profiles page by using a template and catching a user's ID via GET, but I want to do something better.
That is, I want to rewrite the URLs to something like http://www.example.com/profiles/username
and this should hold for all the themes chosen. I think that's how Buddypress does it. Any ideas?
To change the author base, add the following to your functions.php file:
add_action( 'init', 'so16194116_new_author_base' );
function so16194116_new_author_base()
{
global $wp_rewrite;
$author_slug = 'profiles';
$wp_rewrite->author_base = $author_slug;
}
Visit the permalinks admin page after you implemented this, to flush the rewrite rules.

Wordpress custorm URL variables causes strange redirect

I'm trying to have custom variables in my URL for Wordpress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it - say www.mydomain.com/?mrdrct=myVarable - I am not directed to my homepage of my Wordpress site which is set to a static page with a template on it - I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening - any ideas? Hopefully I've explained this well enough.
Thanks.
When WP sees a query string (? after the URL) it will attempt to display matching posts using it's rewrite rules. If no posts match it will show a 404 error - I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the Wordpress Template Hierarchy for more details on that).
I'm not 100% sure what you want to achieve, but I'd suggest that you need to look at changing the query when $wp_query->query_vars['mrdrct'] is set. See the WP Codex for query_posts() for a good place to start, if you are not already familier with it.

Resources