WordPress.org

Ready to get started?Download WordPress

Forums

VERY wired problem with switch in meta box (1 post)

  1. michaelkk
    Member
    Posted 1 year ago #

    Hi
    I created a new custom meta box and everything work well (saving, outputting) but I don't get any change when I try to add some lines of code to the switch statement
    For example: when I delete few 'cases' from the 'switch' statement no changes taking place... or even when I add new class to an input inside the switch, still I get no changes

    Here is the full code:

    $prefix = 'ks-';
    $header_meta = array(
        array(
            'name' => 'header-options',
            'id' => $prefix . 'header-options',
            'type' => 'radio',
            'options' => array(
                array('name' => 'Show Page Title', 'value' => 'title-true'),
                array('name' => 'Hide Page Title', 'value' => 'title-false'),
                array('name' => 'Fancy Header', 'id' => 'fancy-header-setting', 'value' => 'fancy-header'),
                array('name' => 'Slideshow', 'value' => 'slideshow')
            )
        ),
        array(
            'name' => 'header-effect',
            'id' => $prefix . 'header-effect',
            'type' => 'radio',
            'options' => array(
                array('name' => 'Normal', 'value' => 'normal'),
                array('name' => 'Overlaping', 'value' => 'overlaping')
            )
        ),
        array(
            'name' => 'Text box',
            'desc' => 'Enter something here',
            'id' => $prefix . 'text',
            'type' => 'text',
            'std' => 'Default value 1'
        ),
        array(
            'name' => 'Textarea',
            'desc' => 'Enter big text here',
            'id' => $prefix . 'textarea',
            'type' => 'textarea',
            'std' => 'Default value 2'
        ),
        array(
            'name' => 'Select box',
            'id' => $prefix . 'select',
            'type' => 'select',
            'options' => array('Option 1', 'Option 2', 'Option 3')
        ),
        array(
            'name' => 'Radio',
            'id' => $prefix . 'radio',
            'type' => 'radio',
            'options' => array(
                array('name' => 'Name 1', 'value' => 'Value 1'),
                array('name' => 'Name 2', 'value' => 'Value 2'),
                array('name' => 'Name 3', 'value' => 'Value 3')
            )
        ),
        array(
            'name' => 'Checkbox',
            'id' => $prefix . 'checkbox',
            'type' => 'checkbox'
        ),
        array(
            'name' => 'Colorpicker',
            'id' => $prefix . 'colorpicker',
            'type' => 'text',
            'default' => '#ffffff',
            'class' => 'ks-display-colorpicker',
        )
    );
    
    // Meta box output html
    function ks_header_settings_display( $post ) {
        global $header_meta, $post;
        echo '<table id="fancy-header-setting">';
        foreach ($header_meta as $field) {
            // get current post meta data
            $meta = get_post_meta($post->ID, $field['id'], true);
            echo '<tr>',
                    '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                    '<td>';
            switch ($field['type']) {
                case 'header-options':
                    echo '<input type="radio" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                    break;
                case 'header-effect':
                    ?><div id="fancy-header-setting-container"><?php
                    echo '<input type="radio" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                    break;
                case 'text':
                    echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
                    break;
                case 'textarea':
                    echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
                    break;
                case 'select':
                    echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                    foreach ($field['options'] as $option) {
                        echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                    }
                    echo '</select>';
                    break;
                case 'radio':
                    foreach ($field['options'] as $option) {
                        echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                    }
                    break;
                case 'checkbox':
                    echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                    break;
                case 'colorpicker':
                    echo '<div class="', $field['class'],'"></div>';
                    echo '<div type="text" class="', $field['class'], '" name="', $field['id'],'" data-default-color="', $field['default'],'" id="', $field['id'],'"', $meta ? '' : '', ' ></div>';
                    break;
                ?></div><?php
            }
            echo     '</td><td>',
                '</td></tr>';
        }
        echo '</table>';
    }
    
    // Save metaboxes
    function ks_save_pagedata( $post_id ) {
        global $header_meta;
    
        // Verify the nonce before proceeding
        if ( !isset( $_POST['ks_page_header_nonce'] ) || !wp_verify_nonce( $_POST['ks_page_header_nonce'], basename( __FILE__ ) ) )
            return $post_id;
    
        // If this is an autosave, our form has not been submitted, so we don't want to do anything
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;
    
        // Check the user's permissions
        if ( 'page' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $post_id ) )
                return $post_id;
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) )
                return $post_id;
        }
    
        // Save data
        foreach ($header_meta as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new && $old) {
                delete_post_meta($post_id, $field['id'], $old);
            }
        }
    
    }
    add_action( 'save_post', 'ks_save_pagedata' );

    image of the meta box

    Thanks
    Michael K.

Topic Closed

This topic has been closed to new replies.

About this Topic