Not 100% sure if this one classes as a php question or is specifically Wordpress...

I'm implementing the Wordpress Settings API with a tabbed settings page. I've added a 'reset tab' button, but since the validation is done on a separate page (options.php) I can't seem to figure out which tab I'm on - without adding the tab as a hidden field, which I want to avoid.

I've found the wp_get_referer() function, but that gives me the full URL - I'm not sure how to pull the tab=tab1 out as a variable.

Update - Answer:

$url = parse_url(wp_get_referer());
parse_str($url['query'], $path);
$tab = $path['tab'];

As said in the answers, some validation is then required.

  • You are doing this without Javascript? – s_ha_dum Feb 4 '13 at 14:48
  • Yep. My form is located at wordpress/wp-admin/options-general.php?page=967slug_settings_page&tab=tab3 (notice the tab parameter) The settings_API then submits the form to options.php (Wordpress core) which then calls a validation function (written by me). I'm trying to get the value of the tab parameter within the validation function. The wordpress function wp_get_referer() gives me the full URL (URI?) - exactly as above - but I'm not sure how to extract the value of 'tab' from that. – Jamie Feb 4 '13 at 16:41

If tab=tab is in the URL then:

global $_GET;
var_dump($_GET); // debugging only; You should be able to see what you need.

That is pure PHP. If your really need to process that string (I don't know why your would) use parse_url then use parse_str on the query part of it.

$path = parse_url($url);
$path = parse_str($path, $output);
var_dump($output); // again, just debugging

For completeness, please don't use any of those values without validating them.

Did I misunderstand you?

  • Unfortunately, I can't use $_GET as the URL I'm trying to take the parameter from is the referer not the current URL. However, given that I can get access to the referer through the wp_get_referer function, I think I can pull together something that uses parse_url and parse_str to get by tab parameter. Thanks for the help – Jamie Feb 4 '13 at 17:23
  • Just to close this down properly, I've been playing around with your suggestions to get an answer, which I've added to my original quesiton. @s_ha_dum - thanks for the help. – Jamie Feb 5 '13 at 12:27

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.