Problem with display data from get_option
-
05-03-2021 - |
Вопрос
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>
Решение
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).