0

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");
            die;
    } 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");
        die;
    }
}
add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
}
3
  • 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? Commented Nov 19, 2013 at 21:26
  • At the beginning and inside, isn't it? Well, the output is: array(1) { ["page"]=> string(19) "seozeta-options.php" }
    – Unix
    Commented Nov 19, 2013 at 22:00
  • Oh sorry, clicking "save" appears more info: pastebin.com/23tFcfaP
    – Unix
    Commented Nov 20, 2013 at 1:07

1 Answer 1

2

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;

1
  • 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
    Commented Nov 20, 2013 at 13:11

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.