I have a link like this:
site.com/?wt_region_by_default=New+York
site.com/?wt_region_by_default=Berlin
How can I make such a link using the add_rewrite_rule hook?
site.com/newyork
site.com/berlin
I tried like this:
add_action( 'init', 'rewrite_rule_my' );
function rewrite_rule_my(){
add_rewrite_rule( '^([^/]+)?', 'index.php?wt_region_by_default=$matches[1]', 'top' );
add_rewrite_tag( '%wt_region_by_default%', '([^&]+)' );
}
Related
I accepted a new challenge making a custom Wordpress plugin.
After doing some research I started creating clean urls for the plugin and collected the code below.
When I visit the url https://mysite/estates/list/test/baarle/
The first custom var/rewrite_tag "jebamodus" is displayed with a value "list" in the $wp_query->query_vars array. This one's good.
The second and/or third var/rewrite_tag isn't displayed/registered?
Do I need to init each var/rewrite_tag with rewrite url seperated or are there some faults in my code?
I feel like I almost got it to work, but can't find what's wrong.
Some fresh eyes needed.
function wpse_205120_query_vars( $qvars ) {
$qvars[] = 'jebamodus';
$qvars[] = 'jebatest';
$qvars[] = 'jebaregion';
return $qvars;
}
add_filter( 'query_vars', 'wpse_205120_query_vars' );
add_action( 'init', function() {
add_rewrite_tag( '%jebamodus%', '([^/]+)' );
add_rewrite_tag( '%jebatest%', '([^/]+)' );
add_rewrite_tag( '%jebaregion%', '([^/]+)' );
add_rewrite_rule( '^estates/([^/]+)', 'index.php?jebamodus=$matches[1]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]&jebaregion=$matches[3]', 'top' );
flush_rewrite_rules(true);
} );
add_filter('template_include', function($template){
global $wp_query;
if( isset($wp_query->query_vars['jebamodus'])) {
echo "<pre>";
print_r($wp_query->query_vars);
wp_die();
}
return $template;
});
I have a wordpress theme template page accessible through "www.myDomain.com/documents".
The slug of the page is therefor "documents".
But i can't seem to find a way to add a ressource to the URL.
How can i add a ressource like "123" at the end and still have it go to my "documents" page?
Like so "www.myDomain.com/documents/123" with the last part being the id to get in my wordpress theme page code... right now it just ends up as a 404.
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'documents/([A-Za-z0-9_-]+)/?$',
'index.php?pagename=documents&documents_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'documents_id';
return $query_vars;
}
add_filter('init','flushRules');
function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }
I need to add rewrite rules for pagination on my endpoint with a query using wl_paged variable.
I want to achieve this: https://example.com/wishlist/?wl_paged=2
So I tried using add_rewrite_rule, but it doesn't work the link is still https://example.com/wishlist.
function add_endpoint() {
add_rewrite_endpoint( 'wishlist', EP_ROOT | EP_PAGES );
add_rewrite_rule( 'wl_paged/([a-z0-9-]+)[/]?$', 'index.php?wl_paged=$matches[1]', 'top');
}
add_action( 'init', 'add_endpoint' );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'wl_paged';
return $query_vars;
});
Can anybody help me do it?
I've been trying to add an endpoint for the shop page using this code:
add_action( 'init', 'add_city_endpoint' );
function add_city_endpoint() {
add_rewrite_endpoint( 'city', EP_PAGES );
}
to get products filtered when url is something like: /shop/city/new-york/
But I get page with no products.
How can I make it work?
Alright, I'll answer my own question.
Since the shop page is not an actual wordpress page, but an archive page for "product" post type, the endpoints don't work well for my purpose. And the proper way would be to use add_rewrite_rule instead:
add_action( 'init', 'city_add_rewrite_rules' );
function city_add_rewrite_rules() {
$shop_page = ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';
add_rewrite_tag( '%sr_city%', '([^&]+)' );
add_rewrite_rule(
"^$shop_page/city/([^/]+)/?$", 'index.php?post_type=product&sr_city=$matches[1]', 'top'
);
//pagination
add_rewrite_rule(
"^$shop_page/city/([^/]+)/page/([0-9]+)?$", 'index.php?post_type=product&sr_city=$matches[1]&paged=$matches[2]', 'top'
);
}
I'm creating a custom url rewrite structure for my WordPress site.
First I had the following code to have url: /objekt/<GID> which loads objekt page and have query var GID.
add_action( 'init', 'objekt_url_rewrites_init' );
function objekt_url_rewrites_init(){
add_rewrite_rule(
'objekt/([A-Za-z_0-9]+)/?$',
'index.php?pagename=objekt&GID=$matches[0]',
'top' );
}
add_filter( 'query_vars', 'objekt_url_query_vars' );
function objekt_url_query_vars( $query_vars ){
$query_vars[] = 'GID';
return $query_vars;
}
Now, I need to change URL structure to /en/objekt/<GID>. So, I change
objekt/([A-Za-z_0-9]+)/?$
to
en/objekt/([A-Za-z_0-9]+)/?$
But its not working!
When I visit /en/objekt/<GID> it just redirects to /en/objekt/ and not getting any query var named 'GID'