0

I have a problem with get_option function. I created options page with one field for testing in form. Everything saving data to database, but when i try display it i getting "Undefined index", but this index exists in the get_option array:

Array
(
    ['turn_on'] => 1
)

When i trying:

print_r($variable);

i getting something like up, but when i trying:

print_r($variable['turn_on']);

i getting Undefined index, do someone know why this working like that and how can i fix it?

// Edit

Class

if (!class_exists('GoToTopOrBottom')) {
    class GoToTopOrBottom {
        private $options;

        public function __construct()
        {
            $this->parent_slug = 'wtyczki';
            $this->slug = 'go-to-top-or-bottom';

            add_action('admin_menu', [$this, 'addSubpage']);
            add_action('admin_init', [$this, 'registerFields']);
        }

        public function addSubpage ()
        {
            add_submenu_page(
                $this->parent_slug,
                'Przejście w górę lub dół',
                'Góra lub dół',
                'manage_options',
                $this->slug,
                [
                    $this,
                    'view'
                ]
            );
        }

        public function view()
        {
            require_once(plugin_dir_path(__DIR__) . '/views/go-to-top-or-bottom/go-to-top-or-bottom.php');
        }

        public function registerFields()
        {
            register_setting(
                'go_to_top_or_bottom_group',
                'go_to_top_or_bottom'
            );

            add_settings_field(
                'turn_on',
                null,
                null,
                $this->slug
            );
        }
    }

    if (is_admin()) {
        new GoToTopOrBottom;
    }
}

And View Form on Admin Option Page

<div class="mt-3">
    <h3>Powrót do góry lub dołu strony</h3>
    <HR />
    <form method="post" action="options.php">
        <?php
        $this->options = get_option('go_to_top_or_bottom');
            settings_fields('go_to_top_or_bottom_group');
        ?>
        <pre>
            <?php
             print_r($this->options['turn_on']);
            ?>
        </pre>
        <div class="row">
            <div class="col-8">
                <div class="row">
                    <div class="col-12 col-sm-6">
                        <div class="form-group">
                            <label class="custom-control custom-checkbox">
                                <input type="checkbox" class="custom-control-input" name="go_to_top_or_bottom['turn_on']" value="1">
                                <span class="custom-control-indicator"></span>
                                <span class="custom-control-description">Włączyć plugin?</span>
                            </label>
                        </div>
                    </div>

                    <div class="col-12 col-sm-6">
                        test
                    </div>
                </div>
            </div>

            <div class-"col-4">
                <button type="submit" name="submit" id="submit" class="btn btn-primary">Zapisz zmiany</button>
            </div>
        </div>
    </form>
</div>
3
  • Can you try $option = get_option( 'turn_on' ); Commented Nov 23, 2017 at 12:12
  • Can you update your code with the full context. Including your use of get_option(). Commented Nov 23, 2017 at 12:17
  • I updated main post, @Drupalizeme this not working. Commented Nov 23, 2017 at 13:32

1 Answer 1

1

You inserting the value of the get_option('go_to_top_or_bottom');as a property in the $this->options so when you actual try to print it like this print_r($this->options['turn_on']);you get the index error because this is a property!!!

Correct way will be $this->options['turn_on']=get_option('go_to_top_or_bottom');

Update

I see that the $options is a private meaning you cannot access it outside of the class.

A solution will change the type to public Or introduce another property as public.

If you don't want to change the Class you can create a variable to use it in the view file like this :

$options=get_option('go_to_top_or_bottom');

And access them with $options["'turn_on'"]; Mind the single quotes ( This is due to the serialization with the quoted names).

9
  • Ok so what now: Array ( ['turn_on'] => 1 ['anything'] => 1 ) Commented Nov 23, 2017 at 13:45
  • Your questions were about the undefined index. So this is answered. What do you think is missing? Commented Nov 23, 2017 at 13:48
  • In the database it look like https://i.imgur.com/aeIVaKx.png, i need display turn_on or anything, with your solution i can't do this. Just can only display array with all options inserted in go_to_top_or_bottom option name. Array ( [go_to_top] => Array ( ['turn_on'] => 1 ['anything'] => 1 ) ) So when i now try $this->options['go_to_top'] it show me array, but when i try check value for turn_on or anything, i getting undefined index. PS. I did it with WordPress Codex. Commented Nov 23, 2017 at 13:49
  • The Database has the serialize values. For example $option_turn_on['turn_on'] will have the value of 1. Commented Nov 23, 2017 at 13:56
  • Can you rephrase your question and what the expected behavior will be to better understand and help you? Commented Nov 23, 2017 at 13:58

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.