I use the next code in order to save the options of my theme (by using the Wp Settings API), but when I debug it, I get the 'Notice: Undefined index: action' error/notification.

In order to fix it, I added "isset()" before the first $_REQUEST. Now the notification is not apperaring, but the options are not saved when I click the 'SAVE' button. What I'm doing wrong? How can I fix it? Thank you in advance.

function mytheme_add_admin() {
global $themename, $options;
if ( isset( $_GET['page'] ) && $_GET['page'] === basename(__FILE__) ) {
    if(isset( $_REQUEST["action"] ) && $_REQUEST["action"] === 'save') {
            foreach ($options as $value) {
                update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
            header("Location: themes.php?page=theme-options.php&saved=true");
    } else if(isset( $_REQUEST["action"] ) && $_REQUEST["action"] === 'reset') {
        foreach ($options as $value) {
            delete_option( $value['id'] ); }
        header("Location: themes.php?page=theme-options.php&reset=true");
add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
  • 1
    It looks like there is no 'action' key in $_REQUEST. Add var_dump($_REQUEST); at the beginning of your function. What does it output? Nov 19 '13 at 21:26
  • At the beginning and inside, isn't it? Well, the output is: array(1) { ["page"]=> string(19) "seozeta-options.php" }
    – Unix
    Nov 19 '13 at 22:00
  • Oh sorry, clicking "save" appears more info: pastebin.com/23tFcfaP
    – Unix
    Nov 20 '13 at 1:07

A little bit of simple debugging and everything is clear.

$REQUEST['action'] is set in this case, so my first guess wasn't true, but... Its value is 'save' and not 'page' nor 'reset'. So your code does exactly nothing, because there is no if part for such value;

  • Yes, you are right. Thanks. I was doing a lot of changes and I had that mistake when I posted the question. Now the code is working. Btw, I fixed the 'save' value and I still have a Notice in the line #997: if (isset($_REQUEST[ $value['id'] ])) { update_option( $value['id'], $_REQUEST[ $value['id'] ] ) ; } else { delete_option( $value['id'] ); } }. The Notice says: "Notice: Undefined index: id". Thanks for your patience.
    – Unix
    Nov 20 '13 at 13: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.