I have a link http://www.example.com/my-account/view-pet/?pet-page=2

I want to change it to either

Here's what I have tried to so far

function wpse12065_init() {
add_action( 'init', array( $this, 'wpse12065_init' ) );

I am able to get the ?pet-page=2 using get_query_var( 'pet-page' )

I have also had a look through the THIS and its still not working

Any help would be greatly appreciated

1 Answer 1


Try this rewrite rule in your functions.php

function string_url_rewrite() {
    global $wp_rewrite;
    add_rewrite_tag('%pet-page%', '([^&]+)');
    //In the rewrite rule you need to enter page slug and page id
    add_rewrite_rule('^Enter_Page_slug/([0-9]+)/?', 'index.php?page_id=Enter_Page_ID&pet-page=$matches[1]', 'top');

add_action('init', 'string_url_rewrite', 10, 0);

Now you also need to change the format of URL in anchor tag. For example, your previous URL


Change this URL into this


To fetch value of pet-page use following method

//Storing the value of 'pet-page' in variable
$value = get_query_var( 'pet-page' );
if($value){  //checking if variable has any value or not
    echo 'This is the value '.$value;
} else {
    echo 'No Values';

Note: If this don't work then flush your permalinks. For that, go to setting -> permalink and save changes.

  • No changes. I tired but butget_query_var( 'pet-page' ); is not retruning
    – bagpipper
    Oct 4, 2016 at 8:53
  • @bagpipper Have you enter page_slug and page_ID in code that I gave for your functions.php? If yes then show exact code that you enter in your functions.php from my answer so that I can find out any mistake/error.
    – Rishabh
    Oct 4, 2016 at 9:17
  • @bagpipper And also, while fetching data with get_query_var( 'pet-page' ); is it showing else statement message (No Values) or its showing error 404 (page not found). I am assuming you are using url like this http://www.example.com/my-account/view-pet/2 in anchor tag. And also don't forget to flush permalink as I shown in Note in my answer.
    – Rishabh
    Oct 4, 2016 at 9:20
  • I actually need this in a plugin rather than the themes functions.php
    – bagpipper
    Oct 5, 2016 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.