Wie binde ich ein Kontrollkästchen in das Widget-Backend-Formular ein?


17

Ich versuche, ein Kontrollkästchen in mein Widget-Backend aufzunehmen. Ich kann den Wert jedoch nicht abrufen (ein oder aus), nachdem der Benutzer ihn übermittelt hat. Ich dachte, der Wert würde in "esc_attr ($ check)" gespeichert (wie bei einer Texteingabe), aber ich kann ihn nicht abrufen.

Das versuche ich jetzt:

public function form( $instance ) {
    $check = isset( $instance[ 'check' ] ) ? $instance[ 'check' ] : 'off';
    echo esc_attr( $check ); // If the input is type text it outputs the value
    ?>
    <input class="widefat" id="<?php echo $this->get_field_id( 'check' ); ?>" name="<?php echo $this->get_field_name( 'check' ); ?>" type="checkbox" />
    <?php 
}

Wie kann ich das zum Laufen bringen? Wie erhalte ich auch den Wert des Kontrollkästchens im Frontend?

Antworten:


22

Zunächst zum Funktions-Widget :

function widget( $args, $instance ) {
    extract( $args );
    // Add this line
    $your_checkbox_var = $instance[ 'your_checkbox_var' ] ? 'true' : 'false';
    // Change 'your_checkbox_var' for your custom ID
    // ...
}

Bei Funktionsaktualisierung :

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    // Add this line
    $instance[ 'your_checkbox_var' ] = $new_instance[ 'your_checkbox_var' ];
    // Change 'your_checkbox_var' for your custom ID
    // ...
    return $instance;
}

Fügen Sie schließlich in der Funktionsform Folgendes hinzu:

<p>
    <input class="checkbox" type="checkbox" <?php checked( $instance[ 'your_checkbox_var' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'your_checkbox_var' ); ?>" name="<?php echo $this->get_field_name( 'your_checkbox_var' ); ?>" /> 
    <label for="<?php echo $this->get_field_id( 'your_checkbox_var' ); ?>">Label of your checkbox variable</label>
</p>
<!-- Remember to change 'your_checkbox_var' for your custom ID, as well -->

BEARBEITEN: Sehen Sie sich den vollständigen Code eines Widgets "Über uns" an, indem Sie ein Kontrollkästchen verwenden, um ein Avatar-Bild anzuzeigen / auszublenden:

class about_us extends WP_Widget {

function about_us() {
    $widget_ops = array( 'classname' => 'about_us', 'description' => __( 'About Us', 'wptheme' ) );
    $this->WP_Widget( 'about_us', __( 'About Us', 'wptheme' ), $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
    extract( $args );
    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
    $text = $instance[ 'text' ];
    // The following variable is for a checkbox option type
    $avatar = $instance[ 'avatar' ] ? 'true' : 'false';

    echo $before_widget;

        if ( $title ) {
            echo $before_title . $title . $after_title;
        }

        // Retrieve the checkbox
        if( 'on' == $instance[ 'avatar' ] ) : ?>
            <div class="about-us-avatar">
                <?php echo get_avatar( get_the_author_meta( 'user_email' ), '50', '' ); ?>
            </div>
        <?php endif; ?>

        <div class="textwidget">
            <p><?php echo esc_attr( $text ); ?></p>
        </div>

        <?php 
    echo $after_widget;
}

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
    $instance[ 'text' ] = strip_tags( $new_instance[ 'text' ] );
    // The update for the variable of the checkbox
    $instance[ 'avatar' ] = $new_instance[ 'avatar' ];
    return $instance;
}

function form( $instance ) {
    $defaults = array( 'title' => __( 'About Us', 'wptheme' ), 'avatar' => 'off' );
    $instance = wp_parse_args( ( array ) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
        <input class="widefat"  id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance[ 'title' ] ); ?>" />
    </p>
    <!-- The checkbox -->
    <p>
        <input class="checkbox" type="checkbox" <?php checked( $instance[ 'avatar' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'avatar' ); ?>" name="<?php echo $this->get_field_name( 'avatar' ); ?>" /> 
        <label for="<?php echo $this->get_field_id( 'avatar' ); ?>">Show avatar</label>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>">About Us</label>
        <textarea class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" rows="10" cols="10" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_attr( $instance[ 'text' ] ); ?></textarea>
    </p>
<?php
}

}
register_widget( 'about_us' );

Getestet und funktioniert.

Bearbeiten (08. September 2015): Wichtig! In diesem Widget-Beispiel werden Konstruktoren im PHP4-Stil verwendet, WordPress 4.3 wechselt jedoch zu PHP5, sodass Sie auch die Konstruktoren wechseln sollten. Weitere Informationen finden Sie hier .

Wenn Sie das Plugin 'Theme-Check' verwenden, sehen Sie einen Hinweis , der Sie zur Verwendung __construct()anstelle von auffordert WP_Widget. Entfernen Sie die erste Funktion und fügen Sie die folgende hinzu:

function __construct() {
    parent::__construct(
        'about_us', // Base ID
        __( 'About US', 'wptheme' ), // Name
        array( 'description' => __( 'About Us', 'wptheme' ), ) // Args
    );
}

Ja, ich benutze es in einem Widget, um das Avatar-Bild zu aktivieren / deaktivieren. Es funktioniert sehr gut für mich.
— Gerard

In Ordnung. Ich denke für mehr Klarheit solltest du in der Antwort die Standardzuordnung für eine $instance['your_checkbox_var']Funktionsform hinzufügen .
— gmazzap

Die Standardzuweisung ist "Avatar" anstelle von "your_checkbox_var". Ich habe 'your_checkbox_var' geschrieben, um es klarer zu machen. Wie auch immer, ich werde meine Antwort mit dem Standardbeispiel ändern. Vielen Dank für den Rat :)
— Gerard

@Gerard, versuche den Avatar standardmäßig einzuschalten und du kannst ihn nicht ausschalten
— Benn

Dies funktionierte für mich, als ich 'ein' und 'aus' anstelle von TRUe und Falso in der Funktion verwendete. Ich habe auch einen einfachen Check für die if-Anweisung verwendet. If ('on' = $ myinstance) {... my code ...}
— The Skilled Family
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.