• Resolved yessoftmk

    (@yessoftmk)


    Good afternoon everyone! Guys, tell me how to save the option, namely the checkbox, simple text fields work, but the checkbox is not written, the value is always empty:

    // Add the option page to the WordPress admin menu {echo:get_option('my_option_fb')}
    function my_plugin_options() {
      add_options_page(
    	'Site Variables', // Page title
    	'Variables', // Menu title
    	'manage_options', // Capability required to access the page
    	'my-plugin', // Menu slug
    	'my_plugin_options_page' // Callback function to display the options page
      );
    }
    add_action('admin_menu', 'my_plugin_options');
    
    // Display the options page
    function my_plugin_options_page() {
      ?>
      <div class="wrap">
    	<h2>Site Variables</h2>
    	<form method="post" action="options.php">
    	  <?php settings_fields('my_plugin_options'); ?>
    	  <?php do_settings_sections('my_plugin_options'); ?>
    
    ?>
    
    	  <table class="form-table">
    		<tr valign="top">
    		  <th scope="row">facebook</th>
    		  <td><input type="text" name="my_option_fb" value="<?php echo esc_attr(get_option('my_option_fb')); ?>" /></td>
    		</tr>
    		<tr valign="top">
    		  <th scope="row">Linked IN</th>
    		  <td><input type="text" name="my_option_insta" value="<?php echo esc_attr(get_option('my_option_insta')); ?>" /></td>
    		</tr>
    
     <tr valign="top">
    		  <th scope="row">Maintenance mode</th>
    		  <td><input type="checkbox" name="my_option_maintenance" value="<?php echo esc_attr(get_option('my_option_maintenance')); ?>" /></td>
    		</tr>
    
    	  </table>
    
    	  <?php submit_button(); ?>
    	</form>
      </div>
      <?php
    }
    
    // Register the settings
    function my_plugin_register_settings() {
      register_setting('my_plugin_options', 'my_option_fb');
      register_setting('my_plugin_options', 'my_option_insta');
      register_setting('my_plugin_options', 'my_option_maintenance');
    }
    add_action('admin_init', 'my_plugin_register_settings');

    input type=”checkbox” name=”my_option_maintenance – in this place problem

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • With a checkbox you need to set a value e.g. 1 or checked or abc and if it is checked it used that value – you then also need to compare the value and add or not the attriribute “checked” – there is a helper function checked()

    Like this

    <input type="checkbox" name="my_option_maintenance" value="1" <?php checked('1',get_option('my_option_maintenance')); ?> />
    Thread Starter yessoftmk

    (@yessoftmk)

    @alanfuller super, work, Thanks!!!

    I hope my explanation made sense too.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add an options page (checkbox)?’ is closed to new replies.