Archivio

Posts Tagged ‘add_submenu_page’

How to add_submenu_page() passing a $variable (parameter=value) with called function

agosto 21, 2013 1 commento

Since in WordPress you cannot pass directly a parameter into the callback function called by the add_submenu_page, I have figured out another simple way to do it.

My problem was to pass a simple parameter into a sub-menu page, something like:
myblog.com/wp-admin/page=my-sub-menu1?parameter=value

You cannot pass value into the callback function, so you CAN’T do this:

add_submenu_page(parent_slug, page_title2, menu_title2, capability, my-sub-menu2, myfunction1(parameter=value )  );

I’ve tried also a secondary function with a wp_redirect, but was not working because headers were alreay set.
Instead I found this solution:

add_submenu_page( parent_slug, page_title, menu_title, capability, my-sub-menu1, myfunction1 );

add_submenu_page(parent_slug, page_title2, menu_title2, capability, my-sub-menu2, myfunction2 );

function myfunction1() {
// Do something
}

function myfunction2() {

     $_REQUEST[‘parameter’] = “value”;

myfunction1(); // Call the original function, but with your parameter set.

}

You just call a secondary function, set the $_REQUEST[‘parameter’] and then call the original function. That’s all !