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 ofadmin_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 }
- set the form action to
-
add an action hook for
update_wpmu_options. In it you can parse the$_POSTdata and update your options usingupdate_site_option, then usewp_redirectto redirect tosettings.php?updated=true&page=mycustomsettingsif (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_optioncalls toget_site_option
The rest is similar - call add_settings_field, register_setting etc.
Written by Nicolas Galler
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Wordpress
Authors
Related Tags
#wordpress
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#