0

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) "" } 
6
  • you have a typo in your form: kns_option[option1], the letter s is missing in kns_option.
    – Milo
    Sep 11, 2013 at 15:44
  • No, don't post to pastebin. Code should be posted here but it would help to identify the line that that error references.
    – s_ha_dum
    Sep 11, 2013 at 15:44
  • @Milo Thanks, i corrected it but the options still wont save..
    – Jake Snake
    Sep 11, 2013 at 15:50
  • @s_ha_dum When i remove the brackets and the error shows again, it points to both the rows where the input text fields are.
    – Jake Snake
    Sep 11, 2013 at 15:51
  • Are the options not saving or not printing. There is a difference. Look in the database and check.
    – s_ha_dum
    Sep 11, 2013 at 15:54

1 Answer 1

1

You aren't returning data from the sanitize function:

function kns_sanitize_options( $input ) {
    $input['option1'] = sanitize_text_field( $input['option1'] );
    $input['option2'] = sanitize_text_field( $input['option2'] );
}

There is therefore nothing to save. Try:

function kns_sanitize_options( $input ) {
    $input['option1'] = sanitize_text_field( $input['option1'] );
    $input['option2'] = sanitize_text_field( $input['option2'] );
    return $input;
}

Secondly, you've registered the name of your options as kns_products_options

register_setting( 'kns_products_settings_group', 'kns_products_options', 'kns_sanitize_options');

But you are using kns_options in the form.

<input type="text" id="option1" name="kns_options[option1]" //...

The API doesn't know how to find the data so $input in your sanitization callback is always null. Make that name attribute match the name you registered.

<input type="text" id="option1" name="kns_products_options[option1]" //...
9
  • Okay, so now it seems to add an array to the database instead of being empty, however it doesn't print any values in the input fields.
    – Jake Snake
    Sep 11, 2013 at 16:19
  • Don't use the square brackets when you retrieve the data.
    – s_ha_dum
    Sep 11, 2013 at 16:20
  • In the input field value or when defining the kns_options variable? I removed the brackets from where i defined the kns_options variable and the error didnt show, but still no print in the input text fields
    – Jake Snake
    Sep 11, 2013 at 16:21
  • So I just saw that the arrays actually are empty. So it manages to create the options at least but the options wont save the strings.
    – Jake Snake
    Sep 11, 2013 at 16:31
  • Seems like you have a few bugs. I am going to need to install this and test it but I don't have the time right now. If you don't get an answer remind me later by putting @s_ha_dum in a comment.
    – s_ha_dum
    Sep 11, 2013 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.