我正在构建一个小插件,基本上有一个设置页面,其中包含四个文本字段和两个复选框。一切正常,包括复选框,只要其中一个或两个都打勾。如果没有,我会收到一条错误消息,说:
警告:array\\u merge():参数#2不是。。。
这将是minevalg\\u hent\\u innstillinger函数中的第二个参数;
function minevalg_hent_innstillinger() {
$defaults = array(); // define this somewhere; reference it here
return array_merge( $defaults, get_option( \'minevalg\', array() ) );
}
是否有人知道如何使复选框正常工作,而不会在每次取消选中这两个复选框时都出现此错误?提前谢谢。
完整代码:
<?php
add_action(\'admin_menu\', \'my_plugin_menu\');
function my_plugin_menu() {
add_menu_page(\'Fire valgfrie bilder\', \'EKK-innstillinger\', \'administrator\', \'djj-fbpt-innstillinger\', \'djj_fbpt_innstillinger\', \'dashicons-admin-generic\');
}
function djj_fbpt_innstillinger() {
?>
<div class="wrap">
<h2>Fire valgfrie bilder</h2>
<form method="post" action="options.php">
<?php settings_fields( \'djj-fbpt-innstillinger-gruppe\' ); ?>
<?php do_settings_sections( \'djj-fbpt-innstillinger-gruppe\' ); ?>
<table class="form-table">
<?php
function minevalg_hent_innstillinger() {
$defaults = array(); // define this somewhere; reference it here
return array_merge( $defaults, get_option( \'minevalg\', array() ) );
}
$options = minevalg_hent_innstillinger();
?>
<tr valign="top">
<th scope="row">Skal bildene vises?</th>
<td>
<input type="checkbox" name="minevalg[valg_1]" value="1" <?php checked( $options[\'valg_1\'], 1 ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row">Kun på forsiden?</th>
<td>
<input type="checkbox" name="minevalg[valg_2]" value="1" <?php checked( $options[\'valg_2\'], 1 ); ?> />
</td>
</tr>
<tr valign="top">
<th scope="row">Bilde 1</th>
<td><input type="text" name="bilde1" value="<?php echo esc_attr( get_option(\'bilde1\') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Bilde 2</th>
<td><input type="text" name="bilde2" value="<?php echo esc_attr( get_option(\'bilde2\') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Bilde 3</th>
<td><input type="text" name="bilde3" value="<?php echo esc_attr( get_option(\'bilde3\') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Bilde 4</th>
<td><input type="text" name="bilde4" value="<?php echo esc_attr( get_option(\'bilde4\') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
add_action( \'admin_init\', \'my_plugin_settings\' );
function my_plugin_settings() {
register_setting( \'djj-fbpt-innstillinger-gruppe\', \'minevalg\' );
register_setting( \'djj-fbpt-innstillinger-gruppe\', \'bilde1\' );
register_setting( \'djj-fbpt-innstillinger-gruppe\', \'bilde2\' );
register_setting( \'djj-fbpt-innstillinger-gruppe\', \'bilde3\' );
register_setting( \'djj-fbpt-innstillinger-gruppe\', \'bilde4\' );
}
$djj_minevalg;
function djj_fbpt_til_theme() {
$djj_bilde1 = esc_attr( get_option(\'bilde1\'));
$djj_bilde2 = esc_attr( get_option(\'bilde2\'));
$djj_bilde3 = esc_attr( get_option(\'bilde3\'));
$djj_bilde4 = esc_attr( get_option(\'bilde4\'));
$djj_bilder[\'1\'] = $djj_bilde1;
$djj_bilder[\'2\'] = $djj_bilde2;
$djj_bilder[\'3\'] = $djj_bilde3;
$djj_bilder[\'4\'] = $djj_bilde4;
return $djj_bilder;
}
// funksjonen kjøres hvis fire bilder skal vises
$options = get_option( \'minevalg\' );
if(isset( $options[\'valg_1\'])) :
add_action( \'wp_enqueue_scripts\', \'djj_firebilder_css_synlig\' );
else : // ellers kjøres en css-fil som gjemmer bildene
add_action( \'wp_enqueue_scripts\', \'djj_firebilder_css_usynlig\' );
endif;
function djj_firebilder_css_synlig() {
wp_enqueue_style( \'firebilder\', plugin_dir_url( __FILE__ ) . \'/css/firebilder.css\' );
}
function djj_firebilder_css_usynlig() {
wp_enqueue_style( \'firebilder\', plugin_dir_url( __FILE__ ) . \'/css/firebilder_skjult.css\' );
}
?>