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:

    ['turn_on'] => 1

When i trying:


i getting something like up, but when i trying:


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

// Edit


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 ()
                'Przejście w górę lub dół',
                'Góra lub dół',

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

        public function registerFields()


    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">
        $this->options = get_option('go_to_top_or_bottom');
        <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>

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

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

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');


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 :


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

  • Ok so what now: Array ( ['turn_on'] => 1 ['anything'] => 1 ) – Adrian Nov 23 '17 at 13:45
  • Your questions were about the undefined index. So this is answered. What do you think is missing? – Drupalizeme Nov 23 '17 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. – Adrian Nov 23 '17 at 13:49
  • The Database has the serialize values. For example $option_turn_on['turn_on'] will have the value of 1. – Drupalizeme Nov 23 '17 at 13:56
  • Can you rephrase your question and what the expected behavior will be to better understand and help you? – Drupalizeme Nov 23 '17 at 13:58

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.