Search code examples
wordpresscheckboxradio-buttonthemes

How can I make the checkboxes keep their option?


I have built a WP theme with an options panel, and I have a small issue.

If you are looking at the code below, I have two checkboxes (type="radio") The problem is that these checkboxes are not keeping their option after I refresh the options page, but they are added to the database because I see the changes in front-end.

public static function optionsframework_slider_function($id,$std,$oldorder,$order,$int){

    $data = get_option(OPTIONS);
    
    $slider = '';
    $slide = array();
    $slide = $data[$id];
    
    if (isset($slide[$oldorder])) { $val = $slide[$oldorder]; } else {$val = $std;}
    
    //initialize all vars
    $slidevars = array('type', 'title', 'desc', 'content');
    
    foreach ($slidevars as $slidevar) {
        if (!isset($val[$slidevar])) {
            $val[$slidevar] = '';
        }
    }
    
    //begin slider interface    
    if (!empty($val['title'])) {
        $slider .= '<li><div class="slide_header"><strong>'.stripslashes($val['title']).'</strong>';
    } else {
        $slider .= '<li><div class="slide_header"><strong>Slide '.$order.'</strong>';
    }
    
    $slider .= '<input type="hidden" class="slide pixy-input order" name="'. $id .'['.$order.'][order]" id="'. $id.'_'.$order .'_slide_order" value="'.$order.'" />';

    $slider .= '<a class="slide_edit_button" href="#">Edit</a></div>';
    
    $slider .= '<div class="slide_body">';
    
    $slider .= '<label>Slide Type</label>';
    $slider .= '
    <div class="slide pixy-checkbox">
    <input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="html"><div>HTML Slide</div>
    <input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="video"><div>Video/ Image Wide Slide</div>
    </div>
    ';
    
    
    $slider .= '<label>Slide Title</label>';
    $slider .= '<input class="slide pixy-input pixy-slider-title" name="'. $id .'['.$order.'][title]" id="'. $id .'_'.$order .'_slide_title" value="'. stripslashes($val['title']) .'" />';
    
    $slider .= '<label>Slide Description</label>';
    $slider .= '<input class="slide pixy-input" name="'. $id .'['.$order.'][desc]" id="'. $id .'_'.$order .'_slide_desc" value="'. $val['desc'] .'" />';
    
    $slider .= '<label>Slide Content</label>';
    $slider .= '<textarea class="slide pixy-input" name="'. $id .'['.$order.'][content]" id="'. $id .'_'.$order .'_slide_content" cols="8" rows="8">'.stripslashes($val['content']).'</textarea>';

    $slider .= '<a class="slide_delete_button" href="#">Delete</a>';
    $slider .= '<div class="clear"></div>' . "\n";

    $slider .= '</div>';
    $slider .= '</li>';

    return $slider;
}

The code for checkboxes is (*it can be found in the code above):

$slider .= '<label>Slide Type</label>';
        $slider .= '
        <div class="slide pixy-checkbox">
        <input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="html"><div>HTML Slide</div>
        <input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="video"><div>Video/ Image Wide Slide</div>
        </div>
        ';

Here is the code that prints the slider in the frontend:

<?php
            $slides = $data['pixy_pixyGallery_slider'];
            if($slides) {
                foreach ($slides as $slide) {
                    echo '<li data-type="'.$slide['type'].'" data-title="'.$slide['title'].'" data-caption="'.$slide['desc'].'">';
                    echo do_shortcode( $slide['content'] );
                    echo '</li>';
               }
            }
            ?>

Solution

  • something like?

    $slider .= '<label>Slide Type</label>';
    $slider .= '<div class="slide pixy-checkbox">';
    
    $slider .= '<input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="html"';
    if($order=='html'){ $slider .= 'checked="checked">'; }else{ $slider .= '>' }
    $slider .="<div>HTML Slide</div>";
    
    $slider .= '<input class="slide pixy-input pixy-slider-type" name="'. $id .'['.$order.'][type]" type="radio" value="video"';
    if($order=='video'){ $slider .= 'checked="checked">'; }else{ $slider .= '>' }
    $slider .= '<div>Video/ Image Wide Slide</div>';
    $slider .= '</div>';
    

    this depends on the value of $order to see if its set to a preset value? you need to know this value in-order to check it against!

    or set it to check the option value(if you have set 1!)? which ever is easier.

    Marty