• Hi,

    I’ve googled a lot but I just can’t see the error in my code.
    I am developing a plugin that has two options with checkboxes, “remove_comment_ips” and “show_branding”. However, I cannot disable the checkboxes in the settings – after saving, they become enabled again.

    Any hint on what I am missing here would be appreciated 🙂

    This is the (reduced) code of my plugin:

    <?php
    /*
    Plugin Name: HootProof Website Check
    Plugin URI: https://hootproof.de
    Description: A brief description of my plugin
    Version: 1.0
    Author: Michelle Retzlaff
    Author URI: https://hootproof.de/about
    License: GPLv2 //TODO
     */
    
    class HootProof_Website_Check {
    
        public function __construct() {
    
            register_activation_hook( __FILE__, array( &$this, 'activate' ) );
            register_deactivation_hook( __FILE__, array( &$this, 'deactivate' ) );
    
            add_action( 'admin_init', array( &$this, 'register_settings' ) );
            add_action( 'admin_menu', array( &$this, 'add_menu_items' ) );
            add_action( 'admin_print_styles', array( &$this, 'admin_styles' ) );
            add_action( 'admin_notices', array( &$this, 'admin_notices' ) );
            add_filter( 'plugin_row_meta', array( &$this, 'plugin_links' ), 10, 2 );
    
            //prepare for translation
            load_plugin_textdomain( 'hootproof-website-check', false, 'hootproof-website-check/languages' );
    
            $options = get_option( 'hpwc_hootproof_options' );
            define( 'HPWC_HOOTPROOF_WP_VERSION', '3.3.1' );
            define( 'HPWC_HOOTPROOF_VERSION', '0.4' );
            define( 'HPWC_EDITION', 'PREMIUM' );
            define( 'HPWC_HOOTPROOF_TIMEZONE', get_option( 'timezone_string' ) ? get_option( 'timezone_string' ) : date_default_timezone_get() );
            define( 'HPWC_HOOTPROOF_URL', plugins_url( '/', __FILE__ ) );
            define( 'HPWC_HOOTPROOF_SETTINGS', get_admin_url() . 'admin.php?page=hootproof_menu_settings' );
            define( 'HPWC_HOOTPROOF_FRONT', get_home_url( null, '', 'http' ) );
        }
    
        public function activate() {
    $role = get_role( 'administrator' );
            $role->add_cap( 'access_hootproof_website_check' );
    
            $options = get_option( 'hpwc_hootproof_options' );
            $options['front_url'] = isset( $options['front_url'] ) ? $options['front_url'] : 'wp';
            $options['remove_comment_ips'] = 'on'; //TODO
            $options['show_branding'] = 'on'; //TODO
            update_option( 'hpwc_hootproof_options', $options );
        }
    
        public function deactivate() {
            //Do whatever it takes
        }
    
        public function plugin_links( $links, $file ) {
            if ( $file == plugin_basename( __FILE__ ) ) {
                return array_merge( $links, array( sprintf( '<a href="%1$s">%2$s</a>', HPWC_HOOTPROOF_SETTINGS, 'Settings' ) ) );
            }
            return $links;
        }
    
        public function add_menu_items() {
    
           add_menu_page('HootProof',
                         'HootProof ' . __( 'Check Results', 'hootproof-website-check' ),
                         'manage_options',
                         'hootproof_menu',  // slug
                           array( $this, 'dashboard_page')
                         //plugins_url( '/images/wp-icon.png', __FILE__ ), //TODO
                        );
    
           $this->dashboard_page_hook = add_submenu_page( 'hootproof_menu', //parent slug
                            'HootProof ' . __( 'Check Results', 'hootproof-website-check' ),
                           __( 'Check Results', 'hootproof-website-check' ),
                          'manage_options',
                          'hootproof_menu',
                           array( $this, 'dashboard_page') );
    
           $this->settings_page_hook =  add_submenu_page( 'hootproof_menu', //parent slug
                           'HootProof ' . __( 'Settings', 'hootproof-website-check' ),
                           'General Settings',
                           'manage_options',
                           'hootproof_menu'.'_settings',
                           array($this, 'settings_page') );
        }
    
        public function admin_notices() {
            $notice = get_transient( 'admin_notice' );
            if ( $notice ) {
                echo $this->set_notice( $notice );
                delete_transient( 'admin_notice' );
            }
        }
    
        public function register_settings() {
    
            register_setting( 'hpwc_hootproof_options_group', 'hpwc_hootproof_options', array( &$this, 'sanitize_settings' ) );
    
            //basic settings
            add_settings_section( 'general_options_section', '', array( &$this, 'section_text' ), 'hpwc_hootproof_settings' );
            add_settings_field( 'front_url', 'Front page URL', array( &$this, 'set_front_url' ), 'hpwc_hootproof_settings', 'general_options_section' );
            add_settings_field( 'show_branding', // id
    		                    __('Show branding','hootproof-website-check'), //label
    							array(&$this, 'set_show_branding'), //Callback
    							'hpwc_hootproof_settings',
    							'general_options_section');
    
            //privacy settings
            add_settings_section( 'privacy_options_section', '', array( &$this, 'section_text' ), 'hpwc_hootproof_settings' );
            add_settings_field( 'remove_comment_ips', 'Remove comment IPs', array( &$this, 'set_remove_comment_ips' ), 'hpwc_hootproof_settings', 'privacy_options_section' ); 
    
        }
    
        public function sanitize_settings( $input ) {
    
            $valid = array( );
            $options = get_option( 'hpwc_hootproof_options' );
    
            $valid['front_url'] = isset( $input['front_url'] ) ? $input['front_url'] : $options['front_url'];
            $valid['remove_comment_ips'] =  isset( $input['remove_comment_ips'] ) ? $input['remove_comment_ips'] : (isset( $options['remove_comment_ips'] ) ? $options['remove_comment_ips'] : 'on');
    
            $valid['show_branding'] =  isset( $input['show_branding'] ) ? $input['show_branding'] : (isset( $options['show_branding'] ) ? $options['show_branding'] : 'on');
    
            return $valid;
        }
    
        public function section_text() {
            // Placeholder for settings section (which is required for some reason)
            //TODO: maybe add some useful explanation here
        }
    
        protected function set_notice( $message, $class = 'updated' ) {
            return '<div class="' . $class . '"><p>' . $message . '</p></div>';
        }
    
        /*
           Functions for options fields
        */
        public function set_front_url() {
            $options = get_option( 'hpwc_hootproof_options' );
    
            echo '<p><select name="hpwc_hootproof_options[front_url]" id="front_url">';
            foreach ( array( 'wp' => 'WordPress Address (' . site_url() . ')', 'site' => 'Site Address (' . home_url() . ')' ) as $key => $value ) {
                echo '<option value="' . $key . '" ' . selected( $options['front_url'], $key, false ) . '>' . $value . '</option>';
            }
            echo '</select></p>';
        }
    
        public function set_remove_comment_ips() {
    
        	$options = get_option( 'hpwc_hootproof_options' );
        	var_dump($options);
        	$remove_comment_ips = $options['remove_comment_ips'];
    
    	    echo "<input id='remove_comment_ips' name='hpwc_hootproof_options[remove_comment_ips]' type='checkbox' ". checked( $remove_comment_ips, 'on', false ). " />";
        }
    
        public function set_show_branding() {
    
    		$options = get_option( 'hpwc_hootproof_options' );
    		var_dump($options);
    		$show_branding = $options['show_branding'];
    
    		echo "<input id='hpwc_hootproof_show_branding' name='hpwc_hootproof_options[show_branding]' type='checkbox' ". checked( $show_branding, 'on', false ). " />";
    }
    
        // END Functions for options fields
    
        public function settings_page() {
    
           if (isset($_GET['hpwc_remove_comment_ips_nonce']) && wp_verify_nonce($_GET['hpwc_remove_comment_ips_nonce'], 'remove_comment_ips')) {
                $this->remove_comment_ips();
                echo $this->set_notice( __('Comment IPs masked', 'hootproof-website-check'));
           } 
    
    		global $screen_layout_columns;
    		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
    		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
    
    		//main
    		add_meta_box( 'hpwc-general-options-meta-box', 'General Options', array( &$this, 'general_options_meta_box' ), $this->settings_page_hook, 'normal', 'core' );
    		add_meta_box( 'hpwc-privacy-options-meta-box', 'Privacy Options', array( &$this, 'privacy_options_meta_box' ), $this->settings_page_hook, 'normal', 'core' );
    
    		?>
    		<div class="wrap gfw">
    			<div id="gfw-icon" class="icon32"></div>
    			<h2>HootProof  &raquo; Settings</h2>
    			<?php settings_errors( 'hpwc_hootproof_options', false ); ?>
    			<div id="poststuff" class="metabox-holder has-right-sidebar">
    				<div id="side-info-column" class="inner-sidebar">
    					<p>something else here...</p>
    				</div>
    				<div id="post-body" class="has-sidebar">
    					<div id="post-body-content" class="has-sidebar-content">
    						<form method="post" action="options.php">
    							<?php
    							wp_nonce_field( 'update-options' );
    							settings_fields( 'hpwc_hootproof_options_group' );
    							do_meta_boxes( $this->settings_page_hook, 'normal', 0 );
    							submit_button( 'Save Changes', 'primary', 'submit', false );
    							?>
    						</form>
    					</div>
    				</div>
    			</div>
    		</div>
    		<?php
    
        } // end settings_page
    
       public function dashboard_page() {
    
          //...
    
        } // end dashboard_page
    
       /*
          META BOXES
       */
       public function general_options_meta_box() {
                echo '<table class="form-table">';
                do_settings_fields( 'hpwc_hootproof_settings', 'general_options_section' );
                echo '</table>';
        }
    
       public function privacy_options_meta_box() {
                echo '<table class="form-table">';
                do_settings_fields( 'hpwc_hootproof_settings', 'privacy_options_section' );
                echo '</table>';
    
                $comments_count = 2; //TODO: get real comment count
                echo '<p><strong>' . sprintf( _n( 'Still %s comment with an actual IP address.', 'Still %s comments with actual IP addresses.', $comments_count, 'hootproof-website-check' ), $comments_count) . '</strong></p>';
    			echo '<a href="' . wp_nonce_url( HPWC_HOOTPROOF_SETTINGS, 'remove_comment_ips', 'hpwc_remove_comment_ips_nonce' ) .'" class="button-secondary">'. __('Remove IP addresses now', 'hootproof-website-check') . '</a>';
    
        }
    
       //END META BOXES
    
    } // end class HootProof_Website_Check
    
        $hpwc_hootproof = new HootProof_Website_Check();

The topic ‘Plugin Development: Settings with Checkboxes’ is closed to new replies.