Support » Allgemeine Fragen » add_settings_field Checkbox ändert Wert nicht

  • Gelöst wootimes

    (@wootimes)


    Hey, ich versuche eine Checkbox auf einer Plugin Seite einzubauen. Leider funktioniert hier das deaktivieren der Checkbox nach erfolgreichem aktivieren (Häkchen setzen) nicht. Hat jemand eine Idee woran das liegen kann?

    public function register_page_options() {
     
    		add_settings_field( 'admin_theme_user_box', 'User Box', array( $this, 'admin_theme_user_box_settings' ), __FILE__, 'admin_theme_section' ); // id, title, display cb, page, section
    
    		
    		// Register Settings
    		register_setting( __FILE__, 'wp_admin_theme_settings_options', array( $this, 'validate_options' ) ); // option group, option name, sanitize cb
    	}
    public function validate_options( $fields ) {
     
    		$valid_fields = array();
    
    		$user_box = trim( $fields['user_box'] );
    		$valid_fields['user_box'] = ( isset( $valid_fields['user_box'] ) && true == $valid_fields['user_box'] ? true : false );
    
    return apply_filters( 'validate_options', $valid_fields, $fields);
    	}
    
    public function admin_theme_user_box_settings() {
    		
         	$checked = isset( $this->options['user_box'] ) ? $this->options['user_box'] : '' ;
    		
    		echo '<input type="checkbox" id="user_box" name="wp_admin_theme_settings_options[user_box]" value="1" '. checked( $checked, $this->options['user_box'], false ).'/>';	
    		
    	}
Ansicht von 1 Antwort (von insgesamt 1)
  • Thread-Starter wootimes

    (@wootimes)

    Ich habe mittlerweile eine Lösung gefunden. Mit folgender Validierung:

    $user_box = trim( $fields['user_box'] );
    		$valid_fields['user_box'] = strip_tags( stripslashes( $user_box ) );

    Und dieser Checkbox Ausgabe:

    if( $this->options['user_box'] ) { 
    			$checked = ' checked="checked" '; 
    		}
    		
    		echo '<input type="checkbox" ' . $checked . ' id="user_box" name="wp_admin_theme_settings_options[user_box]" />';

    hat es geklappt 😉

Ansicht von 1 Antwort (von insgesamt 1)
  • Das Thema „add_settings_field Checkbox ändert Wert nicht“ ist für neue Antworten geschlossen.