1

I am developing a WP plugin in which I have a WP page called my-account, but I have some more child pages of its like account-edit, account-misc etc... so I would like to call these pages in this way mydomain.com/my-account/account-edit and mydomain.com/my-account/account-misc I don't know how to do this without creating WP pages, I have tried some add_rewrite_rule but that doesn't work what I expected.

Here is my updated code:

add_filter('query_vars', 'add_account_edit_var', 0, 1);
function add_account_edit_var($vars){
    $vars[] = 'account-edit';
    return $vars;
}

add_action( 'init', 'add_account_edit_rule' );
function add_account_edit_rule() {
    add_rewrite_rule(
        '^my-account/account-edit([^/]*)/?',
        'index.php?pagename=my-account&account-edit',
        'top'
    );
}

its working totally fine, I called the url my-account/account-edit/ Page template result: Array ( [pagename] => my-account [account-edit] => )

Again I added one more rewrite:

add_filter('query_vars', 'add_account_misc_var', 0, 1);
function add_account_misc_var($vars){
    $vars[] = 'account-misc';
    return $vars;
}

add_action( 'init', 'add_account_misc_rule' );
function add_account_misc_rule() {
    add_rewrite_rule(
        '^my-account/account-misc([^/]*)/?',
        'index.php?pagename=my-account&account-misc',
        'top'
    );
} 

Its not working in template page, I called the url my-account/account-misc/ page loaded but result is Array ( [pagename] => my-account ) not returning "account-misc"

So I cannot check which page etc...

  • Show us what you have tried. – Milo May 4 '17 at 16:08
  • @Milo I just updated the question, can you please check now? Thanks!!! – Riyas Muhammed May 4 '17 at 16:25
2

You're close, the query var for passing page slug is pagename:

add_filter('query_vars', 'add_account_edit_var', 0, 1);
function add_account_edit_var($vars){
    $vars[] = 'account-edit';
    return $vars;
}

add_action( 'init', 'add_account_edit_rule' );
function add_account_edit_rule() {
    add_rewrite_rule(
        '^my-account/([^/]*)/?',
        'index.php?pagename=my-account&account-edit',
        'top'
    );
}

To capture the value in your rule, you need to pass the value in $matches[1]:

add_rewrite_rule(
    '^my-account/([^/]*)/?',
    'index.php?pagename=my-account&account-edit=$matches[1]',
    'top'
);
  • I'm not sure what you're asking. Since account-edit has no value in your rewrite rule, you need to check if that array key either exists or not. – Milo May 5 '17 at 12:56
  • Milo, It's working, but I cannot manage other slugs from page template: for first query_var I called page url like this: my-account/account-edit and inside this print_r( $wp->query_vars ); to get current page pagename query, its working fine the result is: Array ( [pagename] => my-account [account-edit] => ) But I tried with other query_var I called page url like this: my-account/account-misc its not returning query_vars, only just returning: Array ( [pagename] => my-account ) I tried with some other slugs too everytime first query working fine not remaining, any guess? – Riyas Muhammed May 5 '17 at 13:00
  • See edit above. – Milo May 5 '17 at 13:05
  • no I didn't mean query values, actually, I am not getting query string(pagename) I just updated the questions with my updated code, could you please check? – Riyas Muhammed May 5 '17 at 13:34
  • I modified a small thing and its working perfectly now!!!! '^my-account/([^/]*)/?' changed to '^my-account/account-edit([^/]*)/?' final code updated in question Thanks for your time!!!!! – Riyas Muhammed May 5 '17 at 14:05

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.