插件中的复选框未定义索引错误

时间:2015-01-27 作者:Huw Rowlands

在我创建的简单插件中尝试创建复选框时,我遇到了一些错误。我现在有一个简单的设置页面。错误包括:

非法字符串偏移量“single\\u images”

未定义索引:single\\u images\\u zoom

下面是复选框代码。任何帮助都将不胜感激。

<input id="iwmp_settings[\'single_images\']" name="iwmp_settings[\'single_images\']" type="checkbox" value="1" <?php checked(1, $iwmp_options[\'single_images\'], false); ?> />
<label class="description" for="iwmp_settings[\'single_images\']"><?php _e(\'Use with Single Images\', \'iwmp_domain\'); ?></label>



<input id="iwmp_settings[\'single_images_zoom\']" name="iwmp_settings[\'single_images_zoom\']" type="checkbox" value="1" <?php checked(1, $iwmp_options[\'single_images_zoom\'], false); ?> />
                    <label class="description" for="iwmp_settings[\'single_images_zoom\']"><?php _e(\'Use Zoom effect with Single Images\', \'iwmp_domain\'); ?></label>
非法字符串偏移量问题似乎是由以下代码段引起的:

<?php checked(1, $iwmp_options[\'single_images\'], false); ?>
谢谢

1 个回复
SO网友:bonger

您不应该在HTML名称的索引周围加引号,否则最终会导致\'single_images\' (包括单引号)作为PHP名称(因此必须对其进行寻址$iwmp_options["\'single_images\'"], 有些人可能会觉得困惑)。。。例如:

add_action( \'admin_init\', function () {
    register_setting( \'iwmp_settings\', \'iwmp_settings\' );

    add_settings_section( \'single_images\', __( \'Single Images\', \'iwmp_domain\' ) , function () {
        $iwmp_options = get_option( \'iwmp_settings\' );
        // Note quotes around the PHP index, no quotes around the HTML index.
        // (Also id\'s should basically be alphanumeric with underscores/hyphens (for pre-HTML5 compatibility anyway).)
        ?>
        <input id="iwmp_settings_single_images_" name="iwmp_settings[single_images]" type="checkbox" <?php checked( isset( $iwmp_options[\'single_images\'] ) ); ?> />
        <label class="description" for="iwmp_settings_single_images_"><?php _e(\'Use with Single Images\', \'iwmp_domain\'); ?></label>
        <br />
        <input id="iwmp_settings_single_images_zoom_" name="iwmp_settings[single_images_zoom]" type="checkbox" <?php checked( isset( $iwmp_options[\'single_images_zoom\'] ) ); ?> />
        <label class="description" for="iwmp_settings_single_images_zoom_"><?php _e(\'Use Zoom effect with Single Images\', \'iwmp_domain\'); ?></label>
        <?php
    }, \'iwmp_settings\' );
} );

add_action( \'admin_menu\', function () {
    add_options_page( __( \'IWMP Settings\', \'iwmp_domain\' ), __( \'IWMP\', \'iwmp_domain\' ), \'manage_options\', \'iwmp_settings\', function() {
        ?>
        <div class="wrap">
            <h2><?php _e( \'IWMP Settings\', \'iwmp_domain\' ); ?></h2>
            <form method="post" action="options.php">
                <?php settings_fields( \'iwmp_settings\' ); ?>
                <?php do_settings_sections( \'iwmp_settings\' ); ?>
                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php _e( \'Save Changes\', \'iwmp_domain\' ) ?>" />
                </p>
            </form>
        </div>
        <?php
    } );
} );
还有,只是检查一下isset() 在这里,复选框值使事情变得简单。如果你想操纵它们,那么设置一个变量,例如$single_images = isset( $iwmp_options[\'single_images\'] ); 或使用包装函数。(作为对短代码兼容性的补充说明,我发现1/0 那么更方便true/false.)

结束

相关推荐