3

I need to add dynamic prefix in page url.

You can help with how can I achieve this with Rewrite_API

Problem

In my WordPress website, I've few pages which I want to access using prefix before the page name in URL. For example, normally page is accessible using

http://example.com/page-name

I want to add dynamic prefix(location) before the page name. So I want to access the page like below

http://example.com/country/state/page-name

What I tried so far:

I tried to use below filter to add rewrite tag just before the page name in url.

add_action( 'init', array( $this, 'custom_rewrite_rules' ), 1, 0 ); 
 /*
* Rewrite page urls
* adding prefix to page urls
* @doc: http://stackoverflow.com/questions/17613789/wordpress-rewrite-add-base-prefix-to-pages-only
*/
function custom_rewrite_rules()
{
    global $wp_rewrite;
    // add rewrite tag for location
    $wp_rewrite->add_rewrite_tag( "%region%", '([^/]+)', 'region=' );

    $wp_rewrite->page_structure = $wp_rewrite->root . '%region%/%pagename%';        


}// end func

Above action hook allows me to have dynamic page url in frontend and in backend. For example, I can see page urls are now

http://example.com/%region%/page-name

What I need

As I have dynamic url's generated now, I want to replace %region% part of the url with the location string I want, It could be just country name or country/state name combination.

Thanks in Advance.

aakash
  • 376
  • 1
  • 5
  • 18
  • Did you manage to resolve this? I am trying to solve pretty much the same thing. – RobM Apr 13 '16 at 09:17
  • Not yet, I've tried @user5200704 answer, but I stuck with the problem. I just need to change url's in the frontend, in backend url shoud be with %region% rewrite tag. Also it should load the same page doesn't matter what the prefix is. currently all the solution I tried gives me 404 error. – aakash Apr 13 '16 at 11:09

1 Answers1

1

Change page link using wordpress filter

add_filter( '_get_page_link', 'custom_region_page_link', 10, 2 );

function custom_region_page_link( $link, $post_id ){
    //$slug = get_post_meta( $post_id, 'key', true );
    $link = str_replace('%region%', 'india', $link);
    return $link;
}
user5200704
  • 1,689
  • 11
  • 10
  • It also replaces urls in backend, it should remain http://example.com/%region%/page-name in backend. Only in front end if want to change it with region(country or state) we can replace it with a string. we might need rewrite rule as well. – aakash Apr 13 '16 at 06:50
  • you can check conditionally rewrite rule is very well. if( ! is_admin() ){ // replace code here $link = str_replace('%region%', 'india', $link); } – user5200704 Apr 13 '16 at 13:14
  • Ah ok. I've just found this post which may help: http://stackoverflow.com/questions/16251287/wordpress-prefix-dynamic-value-to-custom-taxonomy-url – RobM Apr 13 '16 at 14:37
  • @user5200704 I can successfully replace the string, But that gives me 404 because WordPress thinks its a different page. I want to load the same page. For example, example.com/india/test-page should show the content of example.com/test-page. prefix should be disregarded. We need rewrite rule for that. I cant make it work. – aakash Apr 15 '16 at 06:44
  • 1
    I test in my local environment it's working. I think need flush rules to update wordpress new page structure. $wp_rewrite->page_structure after $wp_rewrite->flush_rules( false ); https://codex.wordpress.org/Rewrite_API/flush_rules – user5200704 Apr 15 '16 at 07:14
  • Can you help me with how can i write rewrite rule once I have http://example.com/country/state/pagename and I just want to show same page [pagename]. Thanks for helping me so far. Bounty is yours. – aakash Apr 18 '16 at 23:17