So I've been making a plugin for wordpress that adds a custom post type for products, which was fairly simple and i made that work.
Afterwards I wanted to make a settings page sub menu to the custom post type in the admin menu and then create some options.
So the first problem i encountered was that a warning showed up that said
Warning: Illegal string offset 'option1' in C:/blablablal on line 96
And from what I understand that means that the option doesn't know that it's supposed to save the options in an array but tries to put it in a string instead. So I found a way to make the warning go away by adding brackets to the variable.
$kns_options[] = get_option('kns_products_options');
(I don't know if this is a solution or not, but I couldn't find anything about it on loads of google searches)
So after the warning went away, I started to try and add options via the form I've created, but the options just don't want to save. I've tried to var_dump the variable, but it comes out empty.. Is there a typo somewhere, because i sure as hell can't find it!
I'll post the code below, please tell me if i should repost it in pastebin or something!
<?php
add_action( 'init', 'kns_register_products_post_type' );
function kns_register_products_post_type() {
// Registrerar en custom post type som heter produkter
register_post_type('produkter',
array(
'labels' =>
array(
'name' => 'Produkter',
'singular_name' => 'Produkt',
'add_new' => 'Lägg till ny',
'add_new_item' => 'Lägg till ny Produkt',
'edit_item' => 'Ändra Produkt',
'new_item' => 'Ny Produkt',
'view_item' => 'Visa Produkt',
'search_items' => 'Sök Produkter',
'not_found' => 'Inga produkter hittades',
'not_found_in_trash' => 'Inga produkter hittades i papperskorgen',
'parent_item_colon' => 'Produkt Förälder:',
'menu_name' => 'Produkter'
),
'public' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'taxonomies' => array('category'),
'menu_position' => 20,
)
);
}
add_action( 'admin_menu', 'kns_products_create_settings_submenu' );
function kns_products_create_settings_submenu() {
// Skapar sub menu för Produkter-fliken
add_submenu_page('edit.php?post_type=produkter', 'Inställningar', 'Inställningar', 'manage_options', 'product_settings', 'kns_products_settings_page' );
// Kallar på register_setting funktionen
add_action( 'admin_init', 'kns_products_register_settings');
}
function kns_products_register_settings() {
register_setting( 'kns_products_settings_group', 'kns_products_options', 'kns_sanitize_options');
}
function kns_sanitize_options( $input ) {
$input['option1'] = sanitize_text_field( $input['option1'] );
$input['option2'] = sanitize_text_field( $input['option2'] );
}
function kns_products_settings_page() {
?>
<div class="wrap">
<h2>Produktinställningar</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'kns_products_settings_group' );
$kns_options[] = get_option('kns_products_options');
var_dump($kns_options);
?>
<table class="form-table">
<h3>Utseende</h3>
<tr valign="top">
<th scope="row"><label for="option1">Blajja</label></th>
<td>
<input type="text" id="option1" name="kns_options[option1]" value="<?php echo esc_attr( $kns_options['option1'] ); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="option2">Annan Blajja</label></th>
<td>
<input type="text" id="option2" name="kns_options[option2]" value="<?php echo esc_attr( $kns_options['option2'] ); ?>" />
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="Spara inställningar" />
</p>
</form>
</div>
<?php
}
?>
I really need help, I've been at this for hours now..
EDIT:
var_dump results are
array(1) { [0]=> string(0) "" }
kns_option[option1]
, the letters
is missing inkns_option
.