Adding management page functions

The management page that we will create is going to add an option to wp-admin. This uses the existing space in the WordPress options table in the database, so no database modifications are required. The name of this new option must be unique. I'm going to call the new option doctype_styles_new_supportedtypes.

There are six functions we need to add to the plugin, so that an admin page can be added to wp-admin. Let's take a look at the functions, one by one:

  1. The first function adds the new doctype_styles_new_supportedtypes option when the plugin is activated, and sets the default value as follows:
      function set_supportedtypes_options() { 
          add_option("doctype_styles_new_supportedtypes",
             "pdf,doc,mp3,zip"); 
      }
  1. The second function removes the new option when the plugin is deactivated, as follows:
      function unset_supportedtypes_options () { 
        delete_option("doctype_styles_new_supportedtypes"); 
      }
  1. Let's look at the new third function, as follows:
      function modify_menu_for_supportedtypes() { 
        add_submenu_page( 
        'options-general.php',    //The new options page will be added as 
                                  //a submenu to the Settings menu.  
        'Document Type Styles',   //Page <title> 
        'Document Type Styles',   //Menu title 
        'manage_options',         //Capability 
        'add_doctype_styles_new', //Slug 
        'supportedtypes_options'  //Function to call 
        );   
      }

This function adds a new item to the Settings menu in wp-admin using the add_submenu_page() function call. This takes six arguments, namely: where the options page should be placed, page title, menu link text, the user at the maximum level who can access the link, what file to open (none, in this case), and the function to call, supportedtypes_options().

  1. The supportedtypes_options() function is, in fact, the fourth new function we are adding:
      function supportedtypes_options() { 
        echo '<div class="wrap"><h2>Supported Document 
           Types</h2>'; 
        if (isset($_POST['submit'])) { 
          update_supportedtypes_options(); 
        } 
        print_supportedtypes_form(); 
        echo '</div>'; 
      }

This function actually displays our new page. It prints a title and checks to see whether someone has clicked on the submit button; if the submit button has been clicked on, the supportedtypes_options() function updates the options and then prints the form.

  1. The new fifth function we have to add is responsible for updating options if the submit button has been clicked on, as follows:
      function update_supportedtypes_options() { 
        $updated = false; 
        if ($_POST['doctype_styles_new_supportedtypes']) {  
          $safe_val = addslashes(strip_tags($_POST
             ['doctype_styles_new_supportedtypes']));     
             update_option('doctype_styles_new_supportedtypes', 
             $safe_val);  
          $updated = true; 
        } 
       
        if ($updated) { 
          echo '<div id="message" class="updated fade">'; 
          echo '<p>Supported types successfully updated!</p>'; 
          echo '</div>'; 
        } else { 
          echo '<div id="message" class="error fade">'; 
          echo '<p>Unable to update supported types!</p>'; 
          echo '</div>'; 
        } 
      }
  1. The last function we need to add, which is the new sixth function, prints the form that the users will see. Make sure there are no spaces before or after the closing tag (EOF;), as follows:
      function print_supportedtypes_form() { 
        $val_doctype_styles_new_supportedtypes = 
           stripslashes(get_option('
           doctype_styles_new_supportedtypes')); 
        echo <<<EOF 
      <p>Document types supported by the Add Document Type Styles New plugin are listed       as follows.<br />To add a new type to be linked, take the following steps, in this order: 
      <ol> 
        <li>Upload the icon file for the new doctype to <i>wp-
           content/plugins/add_doctype_styles_new/</i></li>  
        <li>Add a line for the new doctype to the stylesheet at 
           <i>wp-content/plugins/add_doctype_styles_new/
           doctype_styles_new.css</i></li>  
        <li>Add the extension of the new doctype to the following list, keeping with the comma-separated format.</li> 
      </ol> 
      </p> 
      <form method="post"> 
        <input type="text" name=
           "doctype_styles_new_supportedtypes" size="50" 
           value="$val_doctype_styles_new_supportedtypes" />  
           <input type="submit" name="submit" value="Save Changes" 
           /> 
      </form> 
      EOF; 
      }

These six functions together take care of adding a link in the menu, adding the management page for this link, and updating the new option.