Shallini
Shallini

Reputation: 1

create setting option in admin panel of a plugin in wordpress

I am trying to make a plugin , for that I want to create a menu item in admin section. It will open a page and I want to add the options into the database using a form.

If anyone has code for it please provide it.

Thanks in advance.

Upvotes: 0

Views: 760

Answers (1)

Pim Schaaf
Pim Schaaf

Reputation: 486

See the documentation on https://codex.wordpress.org/Creating_Options_Pages

There are multiple code examples under "See it all together"

The below example uses the new Settings API to create and save plugin options:

<?php
// create custom plugin settings menu
add_action('admin_menu', 'my_cool_plugin_create_menu');

function my_cool_plugin_create_menu() {
    //create new top-level menu     
    add_menu_page(
        'My Cool Plugin Settings',
        'Cool Settings',
        'administrator',
        __FILE__,
        'my_cool_plugin_settings_page',
        plugins_url('/images/icon.png', __FILE__)
    );  
    //call register settings function   
    add_action( 'admin_init', 'register_my_cool_plugin_settings' );
}

function register_my_cool_plugin_settings() {   
    //register our settings     
    register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );     
    register_setting( 'my-cool-plugin-settings-group', 'some_other_option' );   
    register_setting( 'my-cool-plugin-settings-group', 'option_etc' ); 
}

function my_cool_plugin_settings_page() { ?>
<div class="wrap">
    <h1>Your Plugin Name</h1>
    <form method="post" action="options.php">
        <?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
        <?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
        <table class="form-table">
            <tr valign="top">
                <th scope="row">New Option Name</th>
                <td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row">Some Other Option</th>
                <td><input type="text" name="some_other_option" value="<?php echo esc_attr( get_option('some_other_option') ); ?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row">Options, Etc.</th>
                <td><input type="text" name="option_etc" value="<?php echo esc_attr( get_option('option_etc') ); ?>" /></td>
            </tr>
        </table>
        <?php submit_button(); ?>
    </form>
</div>

Upvotes: 1

Related Questions