0

I have a widget that is not passing checkbox state to $new_instance, specifically $instance[ 'posts' ]; if I dump $new_instance in the update function it is empty? All the other instance variables are fine and as expected?

  /**
     * Ouputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form( $instance ) {

        if ( isset( $instance[ 'type' ] ) ) {
            $type = $instance[ 'type' ];
        }
        else {
            $type = __( 'Select Type', 'text_domain' );
        }

        if ( isset( $instance[ 'icon' ] ) ) {
            $icon = $instance[ 'icon' ];
        } else {
            $icon = __( 'Select Icon', 'text_domain' );
        }

        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        } else {
            $title = __( 'New title', 'text_domain' );
        }

        if ( isset( $instance[ 'posts' ] ) ) {
            $theposts['posts'] = $instance[ 'posts' ];
        }

        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php _e( 'Type:' ); ?></label> 
        <select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>" class="widefat" style="width:100%;">
            <?php
           $types = array('Select Type', 'rental' => 'Rental', 'cloud' => 'Cloud', 'konnect' => 'Connect', 'support' => 'Support');
            foreach($types as $type => $v) { ?>

            <?php if ( isset( $instance[ 'type' ] ) ) { ?>
                <option <?php if ( $instance['type'] == $type ) echo 'selected="selected"'; ?> value="<?php echo $type; ?>"><?php echo $v; ?></option>
            <?php } else { ?>
                <option  value="<?php echo $type; ?>"><?php echo $v; ?></option>
            <?php } ?>

        <?php } ?>      
        </select>
        </p>

        <p>
        <label for="<?php echo $this->get_field_id( 'icon' ); ?>"><?php _e( 'icon:' ); ?></label> 
        <select id="<?php echo $this->get_field_id('icon'); ?>" name="<?php echo $this->get_field_name('icon'); ?>" class="widefat" style="width:100%;">
            <?php
        $icons = array('Select Icon', 'monitor', 'users', 'switch', 'cloud', 'arrows-ccw', 'user', 'user-add', 'link', 'lock', 'thumbs-up', 'upload-cloud','login','database','install','lifebuoy','signal','network');
            foreach($icons as $ico) { ?>

            <?php if ( isset( $instance[ 'icon' ] ) ) { ?>
                <option <?php if ( $instance['icon'] == $ico ) echo 'selected="selected"'; ?> value="<?php echo $ico; ?>"><?php echo $ico; ?></option>
            <?php } else { ?>
                <option  value="<?php echo $ico; ?>"><?php echo $ico; ?></option>
            <?php } ?>

            <?php } ?>       
       </select>
        </p>


        <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( '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( $title ); ?>" />
        </p>


            <p>
            <label for="<?php echo $this->get_field_id( 'posts' ); ?>"><?php _e( 'Post to show:' ); ?></label><br />

            <?php
            if ($instance ['type'] == 'konnect') {$instance ['type'] == 'konect';}
            $newargs = array('posts_per_page' => -1, 'post_type' => $instance[ 'type' ], 'post_parent' => 0,);
            $specificposts = get_posts( $newargs ); ?>

            <?php foreach($specificposts as $name => $specificpost) {  ?>


                <?php if ( in_array( $specificpost->ID, $instance[ 'posts' ] ) ) { ?>
                    <p class="<?php echo $specificpost->ID; ?>"><input type="checkbox" name="<?php echo $instance[ 'posts' ][$specificpost->ID]; ?>" value="<?php echo $specificpost->ID; ?>" checked="checked" /> <label><?php echo $specificpost->post_title; ?></label></p>
                <?php  } else { ?>
                    <p class="<?php echo $specificpost->ID; ?>"><input type="checkbox" name="<?php echo $this->get_field_name('posts'). '['.$specificpost->ID.']'; ?>" value="<?php echo $specificpost->ID; ?>" /> <label><?php echo $specificpost->post_title; ?></label></p>
                <?php }  ?>
          <?php } ?>

        </p>            

    <?php }

1 Answer 1

0

I suspect this is mostly happening because down towards the bottom, you've got an input that's not using $this->get_field_name() (the first of the two checkbox inputs).

Try using $this->get_field_name() as you did in the "then" clause of that section of code and it should be OK.

Not much else wrong that I can see.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.