-1

I am making my theme options so I am using the code from the WordPress Codex. I have taken this code below. Creating_Options_Pages

$options = array( 
        array("name" => "Header Customization",
                "type" => "sub-section-3",
                "category" => "header-styles",
        ),
        array("name" => "Header Image",
                "desc" => "Set the image to use for the header background. ",
                "id" => $shortname."_header_background_image",
                "type" => "text",
                "parent" => "header-styles",
                "std" => ""),
        array("name" => "Body Background Settings",
                "type" => "sub-section-3",
                "category" => "body-styles",
        ),
        array("name" => "Body Background Color",
                "desc" => "Set the color of the background on which the page is. ",
                "id" => $shortname."_body_background_color",
                "type" => "color-picker",
                "parent" => "body-styles",
                "std" => "444444"),
        array("name" => "Sidebar Setup",
                "type" => "sub-section-3",
                "category" => "sidebar-setup",
        ),
        array("name" => "Sidebar Position",
                "id" => $shortname."_sidebar_alignment",
                "type" => "radio",
                "desc" => "Which side would you like your sidebar?",
                "options" => array("left" => "Left", "right" => "Right"),
                "parent" => "sidebar-setup",
                "std" => "right"),
        array("name" => "Navigation Bar Setup",
                "type" => "sub-section-3",
                "category" => "nav-setup",
        ),
        array("name" => "Pages to show in Navigation Bar",
                "desc" => "Select the pages you want to include. All pages are excluded by default",
                "id" => $shortname."_nav_pages",
                "type" => "multi-select",
                "options" => mnt_get_formatted_page_array($shortname."_nav_pages"),
                "parent" => "nav-setup",
                "std" => "none"
        ),
        array("name" => "Analytics",
                "type" => "sub-section-3",
                "category" => "analytics-setup",
        ),
        array("name" => "Custom Google Analytics Tracking Code",
                "desc" => "Enter your tracking code here for Google Analytics",
                "id" => $shortname."_custom_analytics_code",
                "type" => "textarea",
                "parent" => "analytics-setup",
                "std" => ""
        ),
        array("name" => "category posts to show on the front page",
                "desc" => "Select the category you want to include. All pages are excluded by default",
                "id" => $shortname."_front_page_first_section",
                "type" => "select",
                "options" => mnt_get_category_array($shortname."_nav_pages"),
                "parent" => "nav-setup",
                "std" => mnt_get_category_array($shortname."_nav_pages")
        ),
        array("name" => "category posts to show on the front page below",
                "desc" => "Select the category you want to include. All pages are excluded by default",
                "id" => $shortname."_front_page_second_section",
                "type" => "select-2",
                "options" => mnt_get_category_array($shortname."_nav_pages"),
                "parent" => "nav-setup",
                "std" => mnt_get_category_array($shortname."_nav_pages")
        ),
     );

    function create_form($options) { 
        echo "<form id='options_form' method='post' name='form' >\n";
        foreach ($options as $value) {
            switch ( $value['type'] ) {
                case "sub-section-3":
                    create_suf_header_3($value);
                    break;

                case "text";
                    create_section_for_text($value);
                    break;

                case "textarea":
                    create_section_for_textarea($value);
                    break;

                case "multi-select":
                    create_section_for_multi_select($value);
                    break;

                case "radio":
                    create_section_for_radio($value);
                    break;

                case "color-picker":
                    create_section_for_color_picker($value);
                    break;
                case "select":
                    create_section_for_category_select('first section',$value);
                    break;
                case "select-2":
                    create_section_for_category_select('second section',$value);
                    break;
            }
        }

        ?> 
        <input name="save" type="button" value="Save" class="button" onclick="submit_form(this, document.forms['form'])" />
        <input name="reset_all" type="button" value="Reset to default values" class="button" onclick="submit_form(this, document.forms['form'])" />
        <input type="hidden" name="formaction" value="default" />

     <script> function submit_form(element, form){ 
                 form['formaction'].value = element.name;
                 form.submit();
             } </script>

        </form>
    <?php }  ?>



    <?php

add_action('admin_menu', 'mynewtheme_add_admin');   
function mynewtheme_add_admin() { 
    global $themename, $shortname, $options, $spawned_options;

    if ( $_GET['page'] == basename(__FILE__) ) {
        if ( 'save' == $_REQUEST['formaction'] ) {
            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value['id'] ] ) ) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
                }
                else {
                    delete_option( $value['id'] );
                }
            }

            foreach ($spawned_options as $value) {
                if( isset( $_REQUEST[ $value['id'] ] ) ) {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
                }
                else {
                    delete_option( $value['id'] );
                }
            }
            header("Location: themes.php?page=options.php&saved=true");
            die;
        }
        else if('reset_all' == $_REQUEST['formaction']) {
            foreach ($options as $value) {
                delete_option( $value['id'] );
            }

            foreach ($spawned_options as $value) {
                delete_option( $value['id'] );
            }
            header("Location: themes.php?page=options.php&".$_REQUEST['formaction']."=true");
            die;
        }
  }

add_theme_page($themename." Theme Options", "".$themename." Theme Options", 
        'edit_themes', basename(__FILE__), 'mynewtheme_admin'); }

function mynewtheme_admin() { 
    global $themename, $shortname, $options, $spawned_options, $theme_name;

    if ($_REQUEST['saved']) {
        echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved for this page.</strong></p></div>';
    }
    if ($_REQUEST['reset_all']) {
        echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
    }
    ?>
<div class="wrap">
    <h1>Settings for <?php echo $themename; ?></h1>
    <div class="mnt-options">
<?php
    create_form($options);
?>
    </div><!-- mnt-options -->
</div><!-- wrap -->
<?php } // end function mynewtheme_admin()
?>

it is working fine as it should be

So what's the problem

The data is saving but except checkboxes and select options.

Here is the function code from the codex for checkboxes and select options.

create_section_for_multi_select() in this function I have not changed any thing.

function create_section_for_multi_select($value) { 
        create_opening_tag($value);
        echo '<ul class="mnt-checklist" id="'.$value['id'].'" >'."\n";
        foreach ($value['options'] as $option_value => $option_list) {
            $checked = " ";
            if (get_option($value['id']."_".$option_value)) {
                $checked = " checked='checked' ";
            }
            echo "<li>\n";
            echo '<input type="checkbox" name="'.$value['id']."_".$option_value.'" value="true" '.$checked.' class="depth-'.($option_list['depth']+1).'" />'.$option_list['title']."\n";
            echo "</li>\n";
        }
        echo "</ul>\n";
        create_closing_tag($value);
     }

create_section_for_category_select() in this function I have changed some little things like post_form to oth_post_form, all to oth_all and level-0 to dr-cat-list

function create_section_for_category_select($page_section,$value) { 
        create_opening_tag($value);
        $all_categoris='';
            echo '<div class="wrap" id="'.$value['id'].'" >'."\n";
            echo '<h1>Theme Options</h1> '."\n" .'
                <p><strong>'.$page_section.':</strong></p>';
                echo "<select id='".$value['id']."' class='post_form' name='".$value['id']."' value='true'>\n";
                echo "<option id='all' value=''>All</option>";
            foreach ($value['options'] as $option_value => $option_list) {
                $checked = ' ';
                echo 'value_id=' . $value['id'] .' value_id=' . get_option($value['id']) . ' options_value=' . $option_value;
            if (get_option($value['id']) == $option_value) {
                $checked = ' checked="checked" ';
            }
            else if (get_option($value['id']) === FALSE && $value['std'] == $option_value){
                $checked = ' checked="checked" ';
            }
            else {
                $checked = '';
            }
                echo '<option value="'.$option_list['name'].'" class="level-0" '.$checked.' number="'.($option_list['number']).'" />'.$option_list['name']."</option>\n";
                //$all_categoris .= $option_list['name'] . ',';
            }   
            echo "</select>\n </div>";
            //echo '<script>jQuery("#all").val("'.$all_categoris.'")</\script>';
        create_closing_tag($value);
     }

Note: The function name's are also changed.

7
  • :( it is written right there at the top of the codex page to avoid usng this method and use the settings api ;) – Mark Kaplun Jul 22 '17 at 10:43
  • @MarkKaplun so does it is bad method to use it like this – Owaiz Yusufi Jul 22 '17 at 10:58
  • @MarkKaplun it is safe to use or not – Owaiz Yusufi Jul 22 '17 at 11:00
  • Do you want to add your options to the customizer or where are you saving this? I mean customizer.php is prob the way to go for custom theme options like i have – CompactCode Jul 22 '17 at 11:54
  • @SwAt.Be I have made a custom page in the admin area where these options are present to customize and all the options are saving except checkboxes and select options. – Owaiz Yusufi Jul 22 '17 at 12:05
1

I think you are not using customizer.php settings to add them and this can cause complications.

So what you want to do is create a customizer.php in your themes root folder.

There you need to define different levels where you want the settings to be displayed and how they are defined. (more about this later). Then you can use the get_theme_mod function to retrieve them in your layout files.

So what you want to do is read this : Theme Customizatoin ApI

So there you need to define panels (first level) , sections (second level) and controls (the settings itself). You can skip panels if you want to only have 1 menu where you store the settings in.

Step 1 : create customizer.php within theme/inc/ folder

Step 2 : Create function like :

        function DesignitMultistore_customize_register($wp_customize)
        {
    // here comes your code to define panels , sections and controls and remove them
        }
add_action('customize_register', 'DesignitMultistore_customize_register');

How to remove default controls :

$wp_customize->remove_control('header_textcolor');
$wp_customize->remove_control('display_header_text');
$wp_customize->remove_control('blogdescription');

How to add panels example :

// Slider Panel
$wp_customize->add_panel('Designit_slider', array(
    'title' => __('Slider', 'DesignitMultistore'),
    'description' => sprintf(__('Settings for slider on Homepage. Here you can upload up to 3 images with text to display on the home page.', 'DesignitMultistore')),
    'priority' => 150,
));

Note : Panel and sections only shows if you have controls in place.

enter image description here

How to add sections :

// Add General settings
    $wp_customize->add_section('Designit_slider_general', array(
        'title' => __('General settings', 'DesignitMultistore'),
        'description' => sprintf(__('Setup up your General settings', 'DesignitMultistore')),
        'priority' => 1,
        'panel' => 'Designit_slider',
    ));

enter image description here

How to add a control :

//General Settings : slider radio checkbox

$wp_customize->add_setting('slider_radio', array(
    'default' => 'slider',
    'sanitize_callback' => 'designit_slider_radio_sanitizer',
));
$wp_customize->add_control('slider_radio', array(
    'type' => 'radio',
    'description' => __('Slider : Use the slider on the home page. Header image : Use the header image on the home page.', 'DesignitMultistore'),
    'label' => __('Enable slider', 'DesignitMultistore'),
    'section' => 'Designit_slider_general',
    'priority' => 1,
    'choices' => array(
        'slider' => __('Slider', 'DesignitMultistore'),
        'static' => __('Header Image', 'DesignitMultistore'),
    ),
));

enter image description here

note : Every control needs a setting so the 2 parts here form 1 control. The sanitize_callback is to check the data before it gets sent to the database. You can use one of wordpress its sanitize or create a function in the same customize.php where you define them like :

//sanitizer switch
function designit_fade_radio_sanitizer($input)
{
    $valid = array(
        'fade' => __('Fade', 'DesignitMultistore'),
        'slide' => __('Slidee', 'DesignitMultistore'),
    );

    if (array_key_exists($input, $valid)) {
        return $input;
    } else {
        return '';
    }
}

And thats it! Now you can call the setting in the frontend by using get_theme_mod('slider_radio', 'slider');

3
  • sorry but I am not making it like this – Owaiz Yusufi Jul 22 '17 at 12:20
  • This is the only and correct way to do this... You really should consider. Why are you not ? – CompactCode Jul 22 '17 at 16:54
  • Unless you want to create your own admin page with those options wich is 5x the work and 2x less efficient , i can tell you how to do that. There is no other and correct way then this – CompactCode Jul 22 '17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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