Add rewrite_rule not working - wordpress

I am making a master/detail pattern sort of page in my theme.
When a person clicks a username, they will be taken to a page with that user's complete information. I did this by putting a link on the master page and then using $_GET to include said user's id in the url.
When i click the user name however, I am being redirected to index.php. I have tried add_rewrite_rule() but I'm still being redirected to index.In my functions.php I have the below code:
add_action( 'init', 'fwwp_rewrites_init' );
function fwwp_rewrites_init(){
add_rewrite_rule(
'user-profile/([0-9]+)/?$',
'index.php?pagename=user-profile&id=$matches[1]',
'top'
);
}
Then in my user-profile.php I have:
add_filter( 'query_vars', 'fwwp_query_vars' );
function fwwp_query_vars( $query_vars ){
$query_vars[] = 'id';
return $query_vars;
}
I even tried remove_filter('template_redirect','redirect_canonical');, so that i could at least get a 404, but I'm still being redirected.
What am I missing here?

Related

How to remove the original link of a post in Wordpress after using post_link and add_rewrite_rule to change the link?

I changed the permalinks of my posts using post_link, adding "/test/" to it.
So for example www.website.com/this-is-fun/ is now having the permalink www.website.com/test/this-is-fun/
This permalink is correctly showing in the admin, when editing a post, and also on the website the link to the post is the new one.
However, when I open this link it is not working, I get a 404. I was able to fix that with a add_rewrite_rule, but now my problem is that BOTH urls exist. I want only the new one to exist.
Am I making a mistake in the post_link part, is there a way you can automatically disable the original link and make the new one work? Or should I use add_rewrite_rule to disable the original? Or is there another way?
This is my code:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
$permalink = home_url('/test/' . $post->post_name .'/' );
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'test/([^/]+)(?:/([0-9]+))?/?$',
'index.php?name=$matches[1]',
'top'
);
}
add_action( 'init','flush_rewrite_rules' );
I know I can change the permalink in the settings btw, but this new permalink will not be for all posts, so I need to do it this way.

WP add_rewrite_rule with custom field and post name

I have been trying to get a WP url rewrite to work for the past few hours, but I can't seem to find what's wrong with it.
I have the below code which changes the post URLs to include a custom field value ('custom_id') and the post slug/name. The title change is working fine.
add_filter('post_type_link', 'change_post_type_link', 1, 3);
function change_post_type_link( $link, $post = 0 ){
if ($post->post_type == 'estate'){
return home_url('pand/'.get_field('custom_id',$post->ID).'/'.$post->post_name);
} else {
return $link;
}
}
Next, a custom WP rewrite rule is added that checks for the right post to be returned, but I always get a 404 page not found.
add_action( 'init', 'change_rewrites_init' );
function change_rewrites_init(){
add_rewrite_rule(
'pand/([0-9]+)/([a-z-]+)?$',
'index.php?post_type=estate&name=$matches[2]',
'top' );
}
Anyone have any clues why this isn't working? Thanks!

WordPress custom post type rewrite rule

I'm trying to add a rule for a custom post type. By default, the URL to view post is www.mydomain.com/job/post-slug
What I'd like, is the post also accessible with the following URL:
www.mydomain.com/j/postid
I've tried this in my functions.php file and I also refresh permalinks in admin settings:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&id=$matches[1]', 'top' );
flush_rewrite_rules( true );
}
add_action( 'init', 'rewrite_short_job_url' );
Doesn't work for me, I'm trying to understand the Rewrite API but cannot find a solution.
I believe you should be using the following:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&p=$matches[1]', 'top' );
}
add_action( 'init', 'rewrite_short_job_url' );
It's important to keep the post_type var, so that only job posts use that redirect.
You can view a list of WordPress query variables in the Codex.

Wordpress URL rewrite with GET variables

I have tried to search a solution but was unable to find one. In my wordpress website i've got a custom page that retrieves data from the database. Now i have this url:
http://www.domain.com/party/?title=nameoftheparty&id=4
I need to rewrite it to:
http://www.domain.com/party/nameoftheparty/4/
I tried to add the rewrite url in .htaccess but i get an 404 page.
What do i need to do?
Add both the code snippets to functions.php
1.We are telling WordPress that /party/nameoftheparty/4/ should be internally mapped to /party/?title=nameoftheparty&id=4
add_action( 'init', 'so27053217_init' );
function so27053217_init()
{
add_rewrite_rule(
'^party/([^/]*)/([^/]*)/?',
'index.php?pagename=party&title=$matches[1]&id=$matches[2]',
'top' );
}
This is optional and only required if you need to make use of title and id variables in your party page. They can be accessed using get_query_var("title")
add_filter( 'query_vars', 'so27053217_query_vars' );
function so27053217_query_vars( $query_vars )
{
$query_vars[] = 'title';
$query_vars[] = 'id';
return $query_vars;
}
Remember to re-save your permalinks to flush rewrite rules.

Utilizing WordPress's permalink structure on custom post types

I need to rewrite some URLs to custom templates. For example, I have a page at domain.com/page, and when a user clicks 'remove' on an element, it goes to domain.com/page/?remove=54. I'd like it to rewrite to domain.com/page/remove/54.
Help is appreciated, thank you in advance!
This exact code is untested, but it's derived from another instance of a similar situation that I did solve recently:
add_action( 'init', 'ss_permalinks' );
function ss_permalinks() {
add_rewrite_rule(
'page/remove/([^/]+)/?',
'index.php?pagename=page&service=$matches[1]',
'top'
);
}
add_filter( 'query_vars', 'ss_query_vars' );
function ss_query_vars( $query_vars ) {
$query_vars[] = 'removeid';
return $query_vars;
}
Re-save your permalink settings once after implementing. page is the slug of the page to point to when the user access this URL (domain.com/page/remove/432), and $matches[1] should be the number after remove/ in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';/ $removeid on the target page's template will be the number in the URL, if specified.

Resources