Set checkbox state based on value

I am creating a taxonomy filter for WordPress archives. I have the checkboxes working so when selected they redirect the page to the correct URL based on the taxonomy slug. My problem is that after page reload the checkbox state resets to unchecked.

Here is my code:

Read More
<?php
$workoutmoves = get_terms('Exercise-Types', array('hide_empty' => 0));
?>

<?php if ($workoutmoves): ?>
<h5>Show Me</h5>
<ul class="no-bullet">
    <?php foreach($workoutmoves AS $workoutmove): ?>
    <li>
        <input class="check_box" type="checkbox" value="<?php echo $workoutmove->slug; ?>"><?php echo $workoutmove->name; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

I thought of adding a php if statement into the input field and echoing ‘checked=”checked”‘ but I don’t know how to grab the correct data to identify if it should be set.

Any insights would be a huge help. Thanks.

Related posts

Leave a Reply

3 comments

  1. You just need to get the current taxonomy term using get_queried_object() wp function and change your loop a like this:

        <?php $term_id = get_queried_object()->term_id; #getting current term_id ?>
        <?php foreach($workoutmoves AS $workoutmove): ?>
        <li>
            <input class="check_box" type="checkbox" 
            <?php $workoutmove->term_id==$term_id?'checked':''; ?>
            value="<?php echo $workoutmove->slug; ?>">
            <?php echo $workoutmove->name; ?>
        </li>
        <?php endforeach; ?>
    
  2. I am not 100% sure what you are doing. However, I believe you want to highlight the current page a user is on. You are going to need an if() to do this. I do not know what data you have available, presumably WP will have some way of giving you the page url, something like:

    if( $workoutmove->slug == $currentSlug )
        echo 'checked="checked";
    

    If you cannot find the url from WP, it can be found in PHP as $_SERVER[‘REQUEST_URI’];

    #

    Part 2:

    $bodyParts = explode( ",", $_GET['types'] );
    

    This will give you an array like:

    array( ‘arms’,’legs’,’back’ );

    Then, I think (I am not familiar with WP), change the line in the code provided by Tom:

    <?php $workoutmove->term_id==$term_id?'checked':''; ?>
    

    To:

    <?php in_array( $workoutmove->term_id, $bodyParts )  ? 'checked' : ''; ?>
    

    Or to give you all of it:

    $bodyParts = explode( ",", $_GET['types'] );
    <?php $term_id = get_queried_object()->term_id; #getting current term_id ?>
    <?php foreach($workoutmoves AS $workoutmove): ?>
    <li>
        <input class="check_box" type="checkbox" 
        <?php in_array( $workoutmove->term_id, $bodyParts )  ? 'checked' : ''; ?>
        value="<?php echo $workoutmove->slug; ?>">
        <?php echo $workoutmove->name; ?>
    </li>
    <?php endforeach; ?>
    
  3. I’d try the following.

    <?php
        $types = ( ! empty( $_GET['types'] ) ) ? explode( ',', $_GET['types'] ) : false;
        $workoutmoves = get_terms('Exercise-Types', array('hide_empty' => 0)); 
    ?>
    
    <?php if ($workoutmoves): ?>
        <h5>Show Me</h5>
        <ul class="no-bullet">
    <?php foreach($workoutmoves AS $workoutmove): ?>
            <li>
                <input class="check_box" type="checkbox" <?php ( $types && in_array( $workoutmove->slug, $types ) ) ? 'checked' : ''; ?> value="<?php echo $workoutmove->slug; ?>"> <?php echo $workoutmove->name; ?>
            </li>
    <?php endforeach; ?>
        </ul>
    <?php endif; ?>