Last Updated: February 25, 2016
·
647
· nicocrm

Create a network admin screen for a Wordpress plugin

You can use the settings API to register a settings screen for your plugin, but in order to be able to configure that plugin at the network level for a multi site installation there are a few more steps needed.

  • the menu needs to be registered in network_admin_menu, instead of admin_menu
  • the menu needs to be added using add_submenu_page('settings.php', ...)
  • the "output" function for the settings page cannot use settings_fields, instead:

    • set the form action to settings.php
    • add a wpnoncefield with an action of siteoptions
    • add some sort of identifier so you can recognize posts to your page. For example use an action of settings.php?settings=mycustomsettings
    • if $_GET['updated'] is set, output a notification:

      if ( isset( $_GET['updated'] ) ) {
          ?><div id="message" class="updated notice is-dismissible"><p><?php _e( 'Options saved.' ) ?></p></div><?php
      }
  • add an action hook for update_wpmu_options. In it you can parse the $_POST data and update your options using update_site_option, then use wp_redirect to redirect to settings.php?updated=true&page=mycustomsettings

    if (isset($_GET['settings']) && $_GET['settings'] == mycustomsettings) {
        $options = $_POST[mycustomsettings];
        $options = $this->sanitizeOptions($options);
        update_site_option(mycustomsettings, $options);
        wp_redirect(network_admin_url('settings.php?updated=true&page=' . mycustomsettings));
        exit();
    }
  • Update get_option calls to get_site_option

The rest is similar - call add_settings_field, register_setting etc.