Reputation: 632
add_action( 'admin_menu', array( &$this, '_settings_menu' ) );
add_action( 'network_admin_menu', array( &$this, '_settings_menu' ) );
I'm using [admin_menu] and [network_admin_menuu] actions to create some option pages for my plugin and the saving operations working great in WordPress Multisite blogs.
BUT, In the network admin level [ /wp-admin/network/admin.php?page=plugin_settings ] NOT [ /blog-1/wp-admin/admin.php?page=plugin_settings ]
After saving the page redirect to [ /wp-admin/network/options.php ] and the following error appears and of course won't save the test values.
Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster.
The plugin is Activated under Network
Upvotes: 2
Views: 130
Reputation: 26075
WordPress Multisite doesn't automatically process fields created with register_settings
, you have to create an update function yourself, like in:
add_action( 'network_admin_menu', [$this, 'admin_menu']);
add_action( 'network_admin_edit_' . self::SLUG . '-update', [$this, 'update'] );
On the callback of the page created by your menu the form action is defined like this, options.php
is for single sites:
<?php
$action = is_network_admin() ? 'edit.php?action=' . self::SLUG . '-update' : 'options.php';
?>
<form action="<?php echo $action; ?>" method="POST">
<?php
settings_fields( self::SLUG . '-page' );
do_settings_sections( self::SLUG . '-page' );
submit_button();
?>
</form>
The update() method goes like this:
public function update() {
check_admin_referer( self::SLUG . '-page-options' );
if ( isset( $_POST[ self::SLUG ] ) ) {
update_site_option( self::SLUG, $_POST[ self::SLUG ] );
}
wp_safe_redirect(
add_query_arg(
array(
'page' => self::SLUG . '-page',
'updated' => 'true',
),
network_admin_url( 'settings.php' )
)
);
exit;
}
And here's a minimal example of everything in action:
class Settings_API_Multisite {
const SLUG = 'base-for-multisite';
public function __construct() {
add_action('plugins_loaded', [$this, 'setup']);
}
public function setup() {
add_action( 'admin_init', [$this, 'init'] );
if (is_multisite()) {
add_action('network_admin_menu', [$this, 'admin_menu']);
add_action('network_admin_edit_' . self::SLUG . '-update', [$this, 'update']);
}
}
public function init() {
if (is_network_admin()) {
add_settings_section(
'network-section',
__('Working with the Settings API', ''),
'__return_false',
self::SLUG . '-page'
);
add_settings_field(
'the_key',
__('Your Key', ''),
[$this, 'render_txt_field'],
self::SLUG . '-page',
'network-section',
[
'label_for' => 'the_key',
'description' => '',
'class' => '',
'placeholder' => 'Fill your key',
]
);
register_setting(self::SLUG . '-page', self::SLUG);
}
}
public function admin_menu() {
if (is_multisite()) {
$hook = add_submenu_page(
'settings.php',
'My Multisite',
'My Multisite',
"manage_network_options",
self::SLUG . '-page',
[$this, 'create_page']
);
// add_action("admin_print_styles-$hook", [$this, 'styles']);
}
}
public function create_page() {
$capabil = is_network_admin() ? 'manage_network_options' : 'manage_options';
if (!current_user_can($capabil)) return;
$action = is_network_admin() ? 'edit.php?action=' . self::SLUG . '-update' : 'options.php';
?>
<?php if (isset($_GET['updated'])) : ?>
<div id="message" class="updated notice is-dismissible">
<p><?php esc_html_e('Options Saved', ''); ?></p>
</div>
<?php endif; ?>
<div class="wrap">
<h1><?php echo esc_attr(get_admin_page_title()); ?></h1>
<form action="<?php echo $action; ?>" method="POST">
<?php
settings_fields(self::SLUG . '-page');
do_settings_sections(self::SLUG . '-page');
submit_button();
?>
</form>
</div>
<?php
}
public function update() {
check_admin_referer(self::SLUG . '-page-options');
if (isset($_POST[self::SLUG])) {
update_site_option(self::SLUG, $_POST[self::SLUG]);
}
wp_safe_redirect(
add_query_arg(
[
'page' => self::SLUG . '-page',
'updated' => 'true',
],
network_admin_url('settings.php')
)
);
exit;
}
public function render_txt_field($args) {
$value = is_network_admin() ? get_site_option(self::SLUG) : get_option(self::SLUG);
$option = $args['label_for'];
$val = isset($value[$option]) ? $value[$option] : '';
$class = !empty($args['class']) ? " class='{$args['class']}'" : '';
$extra = !empty($args['extra']) ? wp_kses_post($args['extra']) : '';
$placeholder = !empty($args['placeholder']) ? " placeholder='{$args['placeholder']}'" : '';
echo '<input type="text" name="' . self::SLUG . '[' . $option . ']" value="' . esc_attr($val) . '" ' . $class . $placeholder . ' />' . $extra;
}
}
new Settings_API_Multisite();
Upvotes: 0