2

Here's my code:

add_action('admin_menu', 'test_plugin_setup_menu');                                                                                                     /**/
function test_plugin_setup_menu(){                                                                                                                      /**/
        add_submenu_page('options-general.php','Forhandler options side','Forhandler Options', 'manage_options', 'mine-første-options', 'test_init');   /**/
}                                                                                                                                                       /**/
function test_init(){                                                                                                                                   /**/
        //echo "<h1>Hello World!</h1>";
        ?>
        <div>
        <h1>Codehero Dealers</h1>
        <form action="options.php" method="post">
        <?php settings_fields('mine_plugin_options'); ?>
        <?php do_settings_sections('mine-første-options'); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
        </form>
        </div>
        <?php
}
function mine_plugin_section_text() {
    echo '<p>Her finder du indstillinger til forhandler delen.</p>';
}
// add the admin settings and such
add_action('admin_init', 'mine_plugin_admin_init');
function mine_plugin_admin_init(){
register_setting( 'mine_plugin_options', 'mine_plugin_options', 'mine_plugin_options_validate' );
add_settings_section('mine_plugin_main', 'Main Settings', 'mine_plugin_section_text', 'mine-første-options');
add_settings_field('mine_plugin_text_string', 'Forhandler checkout indstilling', 'mine_plugin_setting_string', 'mine-første-options', 'mine_plugin_main');
}


function mine_plugin_setting_string() {

$options = get_option('mine_plugin_options');
echo "<input id='plugin_checkbox' name='mine_plugin_options[plugin_checkbox]' type='checkbox' value='true' />";

}

// validate our options
function mine_plugin_options_validate($input) {
    /*
$options = get_option('mine_plugin_options');
$options['text_string'] = trim($input['text_string']);
if(!preg_match('/^[a-z0-9]{}$/i', $options['text_string'])) {
$options['text_string'] = '';
}
return $options; */
}

It says that the settings have been saved, but the checkbox reverts back to not being clicked.

I'm pretty new to the whole "create your own options" thing, so any help would be appreciated. I followed this tutorial to make the code: http://ottopress.com/2009/wordpress-settings-api-tutorial/

2

Don't comment out your validation code remember you are using it to validate the data, so its returning nothing right now, so its saving nothing, try this:

add_action('admin_bar_menu', 'make_parent_node', 999);

function make_parent_node($wp_admin_bar) {
    $args = array(
        'id' => 'test1234', // id of the existing child node (New > Post)
        'title' => 'test', // alter the title of existing node
        'parent' => 'new-content', // set parent to false to make it a top level (parent) node
        'href' => admin_url('admin.php?page=enter_timesheet')
    );
    $wp_admin_bar->add_node($args);
}

add_action('admin_menu', 'test_plugin_setup_menu');

function test_plugin_setup_menu() { /**/
    add_submenu_page('options-general.php', 'Forhandler options side', 'Forhandler Options', 'manage_options', 'mine-første-options', 'test_init');
}

/**/

function test_init() { /**/
    //echo "<h1>Hello World!</h1>";
    ?>
    <div>
        <h1>Codehero Dealers</h1>
        <form action="options.php" method="post">
            <?php settings_fields('mine_plugin_options'); ?>
            <?php do_settings_sections('mine-første-options'); ?>

            <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
        </form>
    </div>
    <?php
}

function mine_plugin_section_text() {
    echo '<p>Her finder du indstillinger til forhandler delen.</p>';
}

// add the admin settings and such
add_action('admin_init', 'mine_plugin_admin_init');

function mine_plugin_admin_init() {
    register_setting('mine_plugin_options', 'mine_plugin_options', 'mine_plugin_options_validate');

    add_settings_section('mine_plugin_main', 'Main Settings', 'mine_plugin_section_text', 'mine-første-options');

    add_settings_field('mine_plugin_checkbox', 'Forhandler checkout indstilling', 'mine_plugin_setting_string', 'mine-første-options', 'mine_plugin_main');

}

function mine_plugin_setting_string() {

    $options = get_option('mine_plugin_options');

    echo "<input id='mine_plugin_checkbox' name='mine_plugin_options[checkbox]' type='checkbox' value='1'" . checked( 1, $options['checkbox'], false ) . " />";
}

// validate our options
function mine_plugin_options_validate($input) {

    $newinput['checkbox'] = trim($input['checkbox']);
    return $newinput;
}

Rather than reading the option, setting up a conditional, and checking for the presence or absence of a value, we can use the WordPress checked function.

| improve this answer | |
  • Thanks a lot! It saves it now! A minor problem came from the code however - before the checkbox it displays "array(1) { ["checkbox"]=> string(1) "1" }" when it is checked, and "array(1) { ["checkbox"]=> string(0) "" }" when it is not checked. However I'm confident I can remove that pretty easily, so thanks a lot! – Kristoffer M Feb 21 '17 at 14:49
  • If you don't mind me asking, what does the make_parent_node function do for the code? It works without it? edit: Ahh, top bar. – Kristoffer M Feb 21 '17 at 14:55
  • sorry i left my debug code there, i updated removing it – David Lee Feb 21 '17 at 15:06
0

It looks like you're missing the checked="checked" value from your input...

Try something like this:

$options = get_option('mine_plugin_options');
$checked = ($options == 'true' ? ' checked="checked"' : '');
echo "<input id='plugin_checkbox' name='mine_plugin_options[plugin_checkbox]' type='checkbox' value='true' {$checked} />";
| improve this answer | |
  • Thanks, but it doesn't seem to do the trick, the code works the same as it did before even with that – Kristoffer M Feb 21 '17 at 14:07
  • Right-click and view source - check if checked="checked" is being added to your code (sometimes {$checked} might not do the trick) – Phoenix Online Feb 21 '17 at 15:38
  • ...also, there was a minor bug, change options to $options. Sorry :-/ – Phoenix Online Feb 21 '17 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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