• Resolved apg1912

    (@apg1912)


    I’ve been learning about Plugin development using lynda.com. For simplicity and ease of checking I’ve put all the code in a single plugin file which has been successfully activated. I have spent over a day repeatedly going over the course chapters and checking everything but I just can’t get this to display anything on the settings page other than the title and the submit button, which when I press says ERROR: options page not found.

    Can anyone please tell me what I am missing or where I have gone wrong. Thanks.

    
    <?php
    /**
    * Plugin Name: MyPlugin
    * Plugin URI:
    * Description: Test Plugin.
    * Version: 1.0
    * Author:
    * Author URI:
    * License: GPL v2+
    * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    * Text Domain: myplugin
    * Domain Path:
    */
    if (!defined('ABSPATH')) exit;
    
    function myplugin_add_sublevel_menu() {
    
      add_submenu_page(
        'options-general.php',
        'MyPlugin Settings',
        'My Plugin',
        'manage_options',
        'myplugin',
        'myplugin_display_settings_page'
      );
    }
    add_action('admin_menu','myplugin_add_sublevel_menu');
    
    function myplugin_display_settings_page() {
    ?>
    <div class="wrap">
      <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
      <form action="options.php" method="post">
        <?php
        // output security fields
        settings_fields('myplugin_options');
        // output setting Sections
        do_settings_sections('myplugin');
        //submit button
        submit_button();
        ?>
      </form>
    </div>
    <?php
    }
    
    // register plugin settings
    function myplugin_register_settings() {
    
      register_setting(
        'myplugin_options',
        'myplugin_options',
        'myplugin_validate_options'
      );
    
      add_settings_section(
        'myplugin_page',
        'MyPlugin Page Settings',
        'myplugin_callback_section_page',
        'myplugin'
      );
    
      add_action('admin_init','myplugin_register_settings');
    }
    
    function myplugin_validate_options($input) {
      // validate options
    
      return $input;
    }
    
    function myplugin_callback_section_page() {
      echo '<p>This section enables various pages to be re-configured.</p>';
    }
    
    ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Options in the admin page for a plugin’ is closed to new replies.