Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I cant figure out how to register all of my setting fields into 1 database option.

Code for my menu page:

add_menu_page('Custom options', 'Custom Options', 'manage_options', 'rm_options', 'display_options_page');

then my main function to add and register section, fields etc.

function rm_register_custom_settings_fields() {
    add_settings_section('rm_contact_info', 'Contact Info', 'rm_contact_info_callback', 'rm_options');

    add_settings_field('rm_number', 'Number:', 'rm_input_fields_callback', 'rm_options', 'rm_contact_info', array('rm_number', 'Description'));
    add_settings_field('rm_adress', 'Adress:', 'rm_input_fields_callback', 'rm_options', 'rm_contact_info', array('rm_adress', 'Description'));
    add_settings_field('rm_about_us', 'About us:', 'rm_textarea_fields_callback', 'rm_options', 'rm_contact_info', array('rm_about_us', 'Description'));

    register_setting('rm_options_group', 'rm_number', 'esc_attr');
    register_setting('rm_options_group', 'rm_adress', 'esc_attr');
    register_setting('rm_options_group', 'rm_about_us', 'esc_attr');
}

then im displaying the options like this:

function rm_display_options_page() { ?>
    <div class="wrap">
        <h2><?php echo 'Custom options for:' . wp_get_theme(); ?></h2>
        <div class="description">Description</div>

        <?php settings_errors(); ?>

        <form method="post" action="options.php">
            <?php
                settings_fields('rm_options_group');
                do_settings_sections('rm_options');

                submit_button();
            ?>
        </form>
    </div>
<?php

What I want to do, instead of having 3 different options in the database and registering them separately, as it is now, I want to have them saved as an array, I tried doing something like:

register_setting('rm_options', 'rm_options', 'esc_attr');

but nothing worked, how can I do this? Basically instead of:

echo get_option('rm_number');

It will be:

$options = get_option('rm_options');
echo $options['rm_number'];

And also, how can I set defaults for the get_option? If the value is empty, I want to echo some default message. It should work with get_option('rm_options', 'default'); but I noticed that even when the fields are empty, they are saved in the database, so this default will never work. How to make this work?

And last thing, to get the default thing to work and to clean database, how can I delete options when there is nothing entered in the field? How to control this, when wordpress is updating and doing all the work by itself?

I will be really grateful when someone can help me with this, please. Thanks in advance :)

share|improve this question
    
Have you seen Settings API with arrays example? –  toscho Feb 5 at 20:53
    
@toscho I just checked it and its over my head. Sorry, Im trying to understand it but i just need to know, how to make this works. There is just too much going on and its confusing me, any simpler example? –  Richard Mišenčík Feb 5 at 21:36
    
If you need it simple, stop using the so called Settings API. –  toscho Feb 5 at 21:40
    
@toscho then what am I supposted to use? I just want to create few options, I dont think wordpress offers any other way to create custom options. Any way you can help me? –  Richard Mišenčík Feb 5 at 22:04
    
Use admin-post.php. Example –  toscho Feb 5 at 22:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.