【问题标题】:Checkboxes Not Saving to Database复选框未保存到数据库
【发布时间】:2016-05-19 06:55:04
【问题描述】:

在下面的函数中,文本输入中的数据正按预期保存到数据库中。但是,复选框并未保持选中状态,并且似乎没有将任何内容保存到数据库中。 (代码来自我正在修改的 WordPress 插件,以根据用户的位置显示或隐藏菜单项。)

我做错了什么?

function option( $fields, $item_id ) {
    ob_start(); ?>
        <p class="field-visibility description description-wide">
            <label for="edit-menu-item-visibility-<?php echo $item_id; ?>">
                <?php _e('Enter country code(s) separated by commas') ?>:
                <input
                    type="text" 
                    class="widefat code" 
                    id="edit-menu-item-visibility-<?php echo $item_id; ?>" 
                    name="menu-item-visibility[<?php echo $item_id; ?>]" 
                    value="<?php echo esc_html( get_post_meta( $item_id, 'locations', true ) ); ?>" /></br>
                <input
                    type="radio"
                    id="edit-menu-item-visibility-<?php echo $item_id;?>"
                    name="menu-item-show-hide[<?php echo $item_id; ?>]" 
                    value="hide" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'hide', true ); ?>
                />Hide from these locations.</br>
                <input
                    type="radio"
                    id="edit-menu-item-visibility-<?php echo $item_id; ?>"
                    name="menu-item-show-hide[<?php echo $item_id; ?>]"
                    value="show" <?php checked( get_post_meta( $item_id, 'hide_show', true ), 'show', true ); ?>
                />Only show to these locations.</br>
            </label>
        </p>
    <?php
    $fields[] = ob_get_clean();
    return $fields;
}

这是我用来更新数据库中数据的内容:

function update_option_text( $menu_id, $menu_item_db_id, $args ) {
    $meta_value = get_post_meta( $menu_item_db_id, 'locations', true );
    $new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] );

    if( '' == $new_meta_value ) {
        delete_post_meta( $menu_item_db_id, 'locations', $meta_value );
    }
    elseif( $meta_value !== $new_meta_value ) {
        update_post_meta( $menu_item_db_id, 'locations', $new_meta_value );
    }
}

    function update_option_hide_show( $menu_id, $menu_item_db_id, $args ) {
    $meta_value = get_post_meta( $menu_item_db_id, 'hide_show', true );
    $new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] );

    if( '' == $new_meta_value ) {
        delete_post_meta( $menu_item_db_id, 'hide_show', $meta_value );
    }
    elseif( $meta_value !== $new_meta_value ) {
        update_post_meta( $menu_item_db_id, 'hide_show', $new_meta_value );
    }
}

还有构造函数:

function __construct() {
    if( is_admin() ) {
        add_filter( 'wp_edit_nav_menu_walker', array( &$this, 'edit_nav_menu_walker' ) );
        add_filter( 'wp_nav_menu_item_custom_fields', array( &$this, 'option' ), 12, 2 );
        add_action( 'wp_update_nav_menu_item', array( &$this, 'update_option_text' ), 10, 3 );
        add_action( 'wp_update_nav_menu_item', array( &$this, 'update_option_hide_show' ), 10, 3 );
        add_action( 'delete_post', array( &$this, 'remove_visibility_meta' ), 1, 3);
    } else {
        add_filter( 'wp_get_nav_menu_items', array( &$this, 'visibility_check' ), 10, 3 );
        add_action( 'init', array( &$this, 'clear_gantry_menu_cache' ) );
    }
}

【问题讨论】:

  • 插入数据库的代码在哪里?
  • @Thomas:刚刚添加。

标签: php wordpress radio-button


【解决方案1】:

$new_meta_value = stripcslashes( $_POST['menu-item-visibility'][$menu_item_db_id] ); 更改为$new_meta_value = stripcslashes( $_POST['menu-item-show-hide'][$menu_item_db_id] ); 就成功了。它现在运行良好!

感谢@Thomas 让我重新思考它是如何发布到数据库的。

【讨论】:

    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多