[resolved] Odd issue with if/else statement (3 posts)

  1. Doodlebee
    Posted 5 years ago #

    Hello all -

    I have a custom widget I've written, and it's worked for quite a while now. But since 3.1, it's no longer functioning. The part that isn't working is a checkbox option to show or hide the title of the widget - and simple if/else statement to see if the box is checked.

    <p><input id="<?php echo $this->get_field_id('hide'); ?>" name="<?php echo $this->get_field_name('hide'); ?>" type="checkbox" <?php checked(isset($instance['hide']) ? $instance['hide'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('hide'); ?>"><?php _e('Hide?'); ?></label></p>

    the <?php checked(isset($instance['hide']) ? $instance['hide'] : 0); ?> is what determines if the box is checked or not, and I lifted this spot of code directly from the default-widgets.php file back when I first wrote it (I think it might have been version 2.7 or something.) However, now, it doesn't work. If I check the box, no matter what I do, it remains unchecked.

    I've also changed it to this: <?php checked(isset($instance['hide']) ? true : false); ?> and this <?php checked(isset($instance['hide'])); ?> and this <?php checked($instance['hide']) ?> and the latter two are the only ones that allow the checkbox to remain checked or unchecked based on what you do - it seems to retain what your decision is just fine BUT it returns nothing. Checked or not, a var_dump ($hide) gives you NULL every time, which is odd to me.

    Did something change in 3.1 for this? Does anyone know why it no longer works, and perhaps how to fix it?

    Thanks :)

  2. Doodlebee
    Posted 5 years ago #

    Okay, got it sorted. first off, I stupidly forgot to add the $hide = $instance['hide']; line in the function widget() area. but still, the only thing that worked was <?php checked(isset($instance['hide'])); ?> - adding the '? true : false' (or other) still made it not work. Which is really weird.

  3. subiectiv
    Posted 5 years ago #

    have tried to add the variable to the function you use?

    you have in function widget() this:
    $hide = $instance['hide'];
    and in function update()
    $instance['hide'] = $new_instance['hide']

    but is the $hide variable in the function that makes your widget work? as in:

    function myfunction($variable1, $variable2, $hide)

    It happens :))

Topic Closed

This topic has been closed to new replies.

About this Topic