0

I'm making a Wordpress Plugin, and I'm starting with the Admin Settings page, I have the page setup and in the menu bar, i have also added sections and fields in to the page and they all work fine, when i change values of the fields and save the page and refresh the settings are the same, which tells me they are being saved. Correct?!

Now, when i try to call the settings, i cannot get a proper value! On the settings page i have 1 checkbox and one text field, i cannot get the value for either one!

The code i am using to get the value is this:

<?php
if(get_option( 'main_dashboard_check' ) ? get_option( 'main_dashboard_check' ) : 'no'){echo 'yes';}else{echo 'no';};
?>

When i use this for either field, i get the same value:

var_dump(get_option('main_text')); This shows 'bool(false)' for both the checkbox and the text field.

On the admin page, here are the fields:

<?php
function admin_init() {

register_setting( $this->settings_field, $this->settings_field, array($this, 'sanitize_theme_options') );
add_option( $this->settings_field, Media_Press_Settings::$default_settings );

add_settings_section('mediapress_main', '',  
array($this, 'main_section_text'), 'main_settings_page');

add_settings_field('main_text', 'Main Text', 
array($this, 'render_main_text'), 'main_settings_page', 'mediapress_main');

add_settings_field('main_dashboard_check', 'Title', 
array($this, 'render_main_checkbox'), 'main_settings_page', 'mediapress_main', 
array('id' => 'main_dashboard_check', 'value' => 'yes', 'text' => 'Test Checkbox') );

}

function render_main_checkbox($args) {
$id = 'mediapress_options['.$args['id'].']';
?>
<input name="<?php echo $id;?>" type="checkbox" value="<?php echo $args['value'];?>" <?php echo isset($this->options[$args['id']]) ? 'checked' : '';?> /> <?php echo " {$args['text']}"; ?>
<?php 
}
?>

So thats some of the code rendering the admin page, its the checkbox and text field, when i try to get the value for either one, nothing happens, this is the code I'm using to get the value

<?php
if(get_option( 'main_dashboard_check' ) ? get_option( 'main_dashboard_check' ) : 'yes'){echo 'yes';}else{echo 'no';};
?>

Nothing happens! Its always printing out the text 'no'. A var dump just shows bool(false)

Can anyone help me? Would really appreciated

Many thanks and sorry if i sound dumb, I'm new to PHP!

0

You can just use the get_option() function like this:

<?php

$main_dashboard_check = get_option( 'main_dashboard_check' );
var_dump( $main_dashboard_check ); /* outputs false if the value does not exist */

$main_dashboard_check = get_option( 'main_dashboard_check', 'default_value' );
var_dump( $main_dashboard_check ); /* outputs 'default_value' if the value does not exist */

?>
| improve this answer | |
  • Thank you but this still outputs 'bool(false)'.. Anything I do its the same result!!! I can't figure it out, if the checkbox is being rendered on the settings page AND its settings are being saved, why is it not retrieving that info!!!! If I use the 2nd code you gave (with the default value), I get the following: 'string(13) "default_value"' and changing the checkbox value doesn't change this!! – pouyar Jun 16 '15 at 12:46

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.