1

I'm not great with PHP. I've just created a WordPress admin options page and it works OK:

// Admin Menu
add_action('admin_menu', 'my_cool_plugin_create_menu');
function my_cool_plugin_create_menu() {

    $parent_slug = 'test-slug';
    $capability = 'administrator';

    // sub menus
    add_submenu_page( $parent_slug, 'Test', 'Test', $capability, 'test', 'hp_settings_page');

    //call register settings function
    add_action( 'admin_init', 'cm_register_settings' );
}

function cm_register_settings() {

    // title
    register_setting( 
        'mytheme_group',                 // Option group 
        'mytheme_title',                 // Option name
        'admin_options_sanitize_text_1' //sanitize callback
    );

    // IDs
    register_setting( 
        'mytheme_group', 
        'mytheme_ids', 
        'admin_options_sanitize_text_2' 
    );   
}

// Admin Page
function hp_settings_page() { ?>

    <div class="wrap">

        <h2>Test</h2>

        <form method="post" action="options.php">
            <?php settings_fields( 'mytheme_group' ); ?>
            <?php do_settings_sections( 'mytheme_group' ); ?>
            <table class="form-table">                 
                <tr>
                    <th scope="row">Title</th>
                    <td>
                        <textarea name="mytheme_title" rows="10" cols="100"><?php echo get_option('mytheme_title'); ?></textarea>
                    </td>
                </tr>

                <tr>
                    <th scope="row">IDs</th>
                    <td>
                        <textarea name="mytheme_ids" rows="10" cols="100"><?php echo get_option('mytheme_ids'); ?></textarea>
                    </td>
                </tr>                   
            </table>

            <?php submit_button(); ?>

        </form>

    </div><?php 
}


// sanitize 1
function admin_options_sanitize_text_1($input) {    
    $new_input = sanitize_text_field( $input );     
    return $new_input;
}


// sanitize 2
function admin_options_sanitize_text_2($input) {    
    $new_input = preg_replace("/[^0-9\,]/", "", $input );       
    return $new_input;
}

This creates two rows in the database and I can display the values like this:

echo get_option('mytheme_ids');
echo get_option('mytheme_title');

What changes do I need to make to this code so it saves it to one row in the database as an array? So I can then display the values something like this:

$options = get_option('mytheme_options');
echo = $options['ids'];
echo = $options['title'];

Any help appreciated.

2

You only need to register one setting and then just modify your form inputs to save the values into an array. Here's an example of registering the setting:

register_setting( 
    'mytheme_settings', 
    'mytheme_settings', 
    'admin_options_sanitize' 
);

$mytheme_settings = get_option( 'mytheme_settings' );

and the field markup:

<textarea name="mytheme_settings[title]">
    <?php echo esc_textarea( $mytheme_settings['title'] ); ?>
</textarea>   

You'll also need to combine your sanitize functions, modifying them to work with the values in the new array.

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.