Cum să incluzi un checkbox în formularul backend al unui widget?

30 aug. 2013, 17:40:35
Vizualizări: 23.2K
Voturi: 21

Încerc să includ un checkbox în backend-ul widget-ului meu. Dar nu pot obține valoarea (pornit sau oprit) după ce utilizatorul îl trimite. Am crezut că valoarea va fi stocată în "esc_attr( $check )" (așa cum este când se folosește un input text), dar nu o pot recupera.

Iată ce încerc acum:

public function form( $instance ) {
    $check = isset( $instance[ 'check' ] ) ? $instance[ 'check' ] : 'off';
    echo esc_attr( $check ); // Dacă input-ul este de tip text, afișează valoarea
    ?>
    <input class="widefat" id="<?php echo $this->get_field_id( 'check' ); ?>" name="<?php echo $this->get_field_name( 'check' ); ?>" type="checkbox" />
    <?php 
}

Cum pot face să funcționeze acest lucru? De asemenea, cum obțin valoarea checkbox-ului în frontend?

0
Toate răspunsurile la întrebare 1
5
25

Mai întâi, în funcția widget:

function widget( $args, $instance ) {
    extract( $args );
    // Adaugă această linie
    $your_checkbox_var = $instance[ 'your_checkbox_var' ] ? 'true' : 'false';
    // Schimbă 'your_checkbox_var' cu ID-ul tău personalizat
    // ...
}

În funcția update:

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    // Adaugă această linie
    $instance[ 'your_checkbox_var' ] = $new_instance[ 'your_checkbox_var' ];
    // Schimbă 'your_checkbox_var' cu ID-ul tău personalizat
    // ...
    return $instance;
}

În final, în funcția form, adaugă acest cod:

<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' ); ?>">Eticheta pentru variabila checkbox</label>
</p>
<!-- Nu uita să schimbi 'your_checkbox_var' cu ID-ul tău personalizat -->

EDIT: Să vedem codul complet al unui widget 'Despre Noi' care folosește un checkbox pentru a afișa/ascunde o imagine avatar:

class about_us extends WP_Widget {

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

function widget( $args, $instance ) {
    extract( $args );
    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
    $text = $instance[ 'text' ];
    // Următoarea variabilă este pentru un tip de opțiune checkbox
    $avatar = $instance[ 'avatar' ] ? 'true' : 'false';

    echo $before_widget;

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

        // Preluăm checkbox-ul
        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' ] );
    // Actualizarea pentru variabila checkbox
    $instance[ 'avatar' ] = $new_instance[ 'avatar' ];
    return $instance;
}

function form( $instance ) {
    $defaults = array( 'title' => __( 'Despre Noi', 'wptheme' ), 'avatar' => 'off' );
    $instance = wp_parse_args( ( array ) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>">Titlu</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>
    <!-- Checkbox-ul -->
    <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' ); ?>">Arată avatar</label>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>">Despre Noi</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' );

Testat și funcționează.

Editare (2015-Sept-08): Important! Acel exemplu de widget folosește constructori în stil PHP4, însă WordPress 4.3 trece la PHP5, așa că ar trebui să schimbi și constructorii. Mai multe informații, aici.

Dacă folosești plugin-ul 'theme-check' vei vedea o notificare care sugerează să folosești __construct() în loc de WP_Widget. Șterge prima funcție și adaugă următoarea:

function __construct() {
    parent::__construct(
        'about_us', // ID de bază
        __( 'Despre Noi', 'wptheme' ), // Nume
        array( 'description' => __( 'Despre Noi', 'wptheme' ), ) // Argumente
    );
}
2 sept. 2013 16:29:27
Comentarii

Da, îl folosesc pe un widget pentru a activa/dezactiva imaginea de avatar. Funcționează foarte bine pentru mine.

Unix Unix
2 sept. 2013 19:25:39

Ok. Cred că pentru mai multă claritate ar trebui să adaugi în răspuns atribuirea implicită pentru $instance['your_checkbox_var'] în funcția form.

gmazzap gmazzap
2 sept. 2013 19:55:27

Atribuirea implicită este 'avatar' în loc de 'your_checkbox_var'. Am scris 'your_checkbox_var' de fapt pentru a face lucrurile mai clare. Oricum, voi modifica răspunsul meu cu exemplul implicit. Mulțumesc pentru sugestie :)

Unix Unix
2 sept. 2013 20:20:52

@Gerard, încearcă să setezi avatarul activat implicit și nu vei putea să îl dezactivezi

Benn Benn
6 mar. 2017 17:19:46

Această soluție a funcționat pentru mine când am folosit 'on' și 'off' în loc de TRUE și FALSE în funcție. De asemenea, am folosit o verificare simplă pentru instrucțiunea if.. if ('on' == $myinstance) { ...codul meu... }

DJ_Davey DJ_Davey
6 apr. 2019 21:40:57