• Resolved Roshy10

    (@roshy10)


    I’m trying to create an options page for my plugin, however when I use add_options_page, it creates the page, but my add_settings_section/field /register_setting don’t latch onto it. I’ve gotten all of these examples from the wordpress/codex website, so I presume it’s correct.

    The submenu item settings > Roshan's functions is appearing fine, but it only contains the title and text from below. Not the form or text input.

    They do however work perfectly fine if I set them to go onto page general as in the example below.
    Wherever it says general below, if replaced with roshans_settings causes the settings to stop appearing (on any page).

    add_action( 'admin_menu', 'roshans_custom_admin_settings' );
    
    function roshans_custom_admin_settings() {
    
        add_options_page(
            'Roshan\'s Functions',
            'Roshan\'s Functions',
            'manage_options',
            'roshans_settings',
            'roshans_function_options'
        );
    }
    
    function roshans_function_options() {
        ?>
        <div class="wrap">
            <h2>My Plugin Options</h2>
            your form goes here
        </div>
        <?php
    }
    
    /*
     * Add all your sections, fields and settings during admin_init
     */
    
    function roshans_settings_api_init() {
    // Add the section to roshans_settings settings so we can add our fields to it
    add_settings_section(
    'roshans_setting_section',
    'Menu Settings',
    'roshans_setting_section_callback_function',
    'general'
    );
    
    // Add the field with the names and function to use for our new settings, put it in our new section
    add_settings_field(
    'roshans_menu_loc',
    'Menu Location',
    'roshans_setting_callback_function',
    'general',
    'roshans_setting_section'
    );
    
    // Register our setting in the "roshans_settings" settings section
    register_setting( 'general', 'roshans_menu_loc' );
    }
    
    add_action( 'admin_init', 'roshans_settings_api_init' );
    
    /*
     * Settings section callback function
     */
    
    function roshans_setting_section_callback_function() {
    echo '<p>Settings for Roshan\'s functions</p>';
    }
    
    /*
     * Callback function for our example setting
     */
    
    function roshans_setting_callback_function() {
    $setting = esc_attr( get_option( 'roshans_menu_loc' ) );
    echo "<input type='text' name='roshans_menu_loc' value='$setting' />";
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Roshy10

    (@roshy10)

    anyone got any ideas?

    Moderator bcworkz

    (@bcworkz)

    Mainly, you need to add form markup to roshans_function_options() so your options page knows what settings to use. You’ve defined them, but have not specified where to use them. Something like this:

    <form action="options.php" method="post">
    <?php settings_fields('roshans_settings1'); ?>
    <?php do_settings_sections('roshans_settings2'); ?>
    <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
    </form>

    The other thing is ‘roshans_settings’ is used for multiple purposes. It might work, even if so it’s confusing. I’ve added 1 & 2 in the above code to show that the tags are from different functions. 1 would be from register_setting() and 2 from add_settings_section(). All the various tags need to be thoughtfully named and carefully coordinated. It’s actually a bad idea to use 1 & 2 unless they convey some meaning.

    Thread Starter Roshy10

    (@roshy10)

    Thank you! This worked ??

    Thread Starter Roshy10

    (@roshy10)

    About the naming, I got the impression from the documentation that they were meant to be the same.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘add_options_page slug not working with add_settings_section’ is closed to new replies.