1

I have a Plugin with 3 submenu_pages. In one page is a formular (in one some text and in one a list of items). when I submit the form, there will be done some processing. after that I don't want to get back to the form-page, but WP shell redirect me to one of the other submenu_pages (in this case a list).

how can I achive this?

add_menu_page( 'page1 main page', '0', 'manage_options', 'mg-pi', 'mg_backend');    
add_submenu_page( 'mg-pi', 'list smthing', '0', 'manage_options', 'mg-pi-sub-list', 'mg_list');
add_submenu_page( 'mg-pi', 'create', '0', 'manage_options', 'mg-pi-sub-create', 'mg_new_item');   

so in mg_new_item() is the processing and then it shell redirect to mg_list(). but if I do the php call to mg_list() the url is still the one of mg_new_item().

I know I could do some header("Location: the-hardcoded-list-url"); but I am looking for the WordPress common way of doing it, with generic Url generation.

5
  • possible duplicate of Wordpress PHP plugin - Settings page error
    – s_ha_dum
    Commented Aug 26, 2013 at 15:14
  • I don't think it's a dublicate @s_ha_dum. But the answer might help sombody anyway. But my problem with it (that other answer) is, that it's not a redirect. If I use that solution, I will have to put the logical code of the formular to the list-code ... which I would like to avoid. Additionaly I want to consider: That other Answer also is not accepted so far, and as you can read in the comments there are still problems with that solition.
    – helle
    Commented Aug 26, 2013 at 15:19
  • 1
    You have not posted enough of your code for me to tell what you are doing, much less to try to debug and test.
    – s_ha_dum
    Commented Aug 26, 2013 at 15:22
  • Also, the answer is not accepted but it does work. I do this all the time. You cannot always account for other people's confusion.
    – s_ha_dum
    Commented Aug 26, 2013 at 15:23
  • There is nothing to debug so far. I am looking for a way to redirect to a sub-menu-page with a wordpress given function, where I get the Url maybe via the slug or something, like it is possible in theme templates as well...
    – helle
    Commented Aug 26, 2013 at 15:32

2 Answers 2

1

Simply use add_query_arg(), remove_query_arg() and get_query_var().

On your page link, you use a simple query var like step=one.

$query_arg = add_query_arg( 'step', 'one', $_SERVER['REQUEST_URI'] );

Then, when the user processed the form, you just reload the page. Then simply check if

0 !== get_query_var( 'step' )
AND 'one' !== get_query_var( 'step' )

and process further. If you want to go back, just use remove_query_arg(), etc.

0
1

Well after some more research I fond what I was actually looking for. Not for a workaround but for a wordpress compatible way.

There is a function called: menu_page_url which expects the defined slug. and there is wp_redirect

so we have 2 wordpress functions to call. In this case:

$foo = menu_page_url("mg-pi-sub-list");
wp_redirect($foo);

that's it. And the good thing, I observe all Wordpress rules and the plugin will be compatible in future versions.

http://codex.wordpress.org/Function_Reference/wp_redirect http://codex.wordpress.org/Function_Reference/menu_page_url

UPDATE

and if you have troubles with your headers you can help you out with this parameter:

<form action="?page=your-sub_page-slug&noheaders=true">
1
  • i think you should add second parameter too, as you no need to echo-ing the url. $foo = menu_page_url("mg-pi-sub-list", false); Commented Nov 6, 2016 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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