Settings API in Multisite – Missing update message

When I use the settings API in a multisite installation and the options page sits at the network level, posting the options to options.php does not work, because the administration page sits at wp-admin/network and WP expects the page to be at wp-admin.

I added a function that checks whether this WP installation is a multsite installation (via the constant) and if it is, it changes the form’s action value to ../option.php. This saves the options OK, but the default message “Settings saved.” is missing (however, the query string does include settings-updated=true).

Any thoughts on how to get the message to appear?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

For network option pages the correct form action URL is:

wp-admin/network/edit.php?action=your_option_name

Then you have to register a callback:

add_action( 
    'network_admin_edit_your_option_name', 
    'your_save_network_options_function' 
);

In that callback function inspect the $_POST data, prepare the values, then save them:

update_site_option( $this->option_name, $this->option_values );

And then you have to create the redirect without further help:

// redirect to settings page in network
wp_redirect(
    add_query_arg(
        array( 'page' => 'your_options_page_slug', 'updated' => 'true' ),
        (is_multisite() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' ))
    )
);
exit;

On the options page check $_GET['updated'], and add an admin notice if you found that parameter.

Method 2

I’ve been using @toscho’s answer which works great, however in certain wordpress install paths the hard-coded form action URL won’t work. Here @birgire states how to build the correct URL:

When referring to urls within the network-admin, you should consider
the network_admin_url(). core function, that falls back to
admin_url() for non-multisite setups.

echo esc_url( 
    add_query_arg( 
       'action', 
       'your_option_name', 
       network_admin_url( 'edit.php' ) 
    )
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x