Save plugin options multisite 3.1

DescriptionActionscript snippet by - 03/14/11

How to save plugin options with wordpress 3.1 multisite and buddypress?

Well i looked for a while to answer these two questions:
1) Why did my buddypress submenu dissappear when updating to wordpress 3.1 and buddypress 1.2.8
2) Why can't I save plugin options anymore, I just get a 404 error or a message saying that the options.php page does not exist.

Please comment on this post if you have a way to prevent the above issue 2 Help!

Here is my workaround:
1) Wordpress 3.1 for multisite moved the Network Admin menu from the sidebar to above the content, consequently you need to account for this change. Use snippet 1 below to add a conditional to the action that calls your function to create the submenu page.

2) I took a look at how buddypress saves their options and decided to change to a similar method. Instead of just posting to the options.php page (which produces 404 now with multisite) I am looping through the form fields and storing the options manually.

2a) This method allows you to submit form data in an array to make saving the options easier. The my_option array is what you will use for all the form input fields, this example only has one but add as many as you want.

2b) One downfall with the method below is that form elements like checkboxes don't send any post data if not checked so you need to do the checking. This I believe is why Buddypress does not have any checkboxes in their options pages, they use radio buttons which have an on/off value and there is no need to check for the lack of a checkbox post.

Tags , ,

<?php
//Snippet1 - New action for creating your buddypress submenu pages - replace 'create_my_submenu_page' with the name of your function

add_action(is_multisite() ? 'network_admin_menu' : 'admin_menu','create_my_submenu_page');


//Snippet2 - This is the form for submitting your plugin options. This works for plugins that only function on the main site, to account for plugins that are used on the network you may want to replace the update_options() with update_site_options() and check ahead of time to decide which to use.

<?php

function my_plugin_admin_form(){
	if(isset($_POST['my_submit'])){		
		if(!isset($_POST['my_option']['my_display_tool'])) $_POST['my_option']['my_display_tool'] = 'false';//If no checked box in post set value to false
		
		foreach((array)$_POST['my_option'] as $key => $value){//Add more my_option[FIELDNAME] to form for more fields
			update_option($key,$value);
		}
	}
	$my_display_tool = get_option('my_display_tool');
	?>

<div class="wrap">
    <h2>My Settings</h2>
    <?php if(isset($_POST['my_submit'])) : ?>
    <div id="message" class="updated fade">
        <p>
            <?php _e( 'Settings Saved', 'my' ) ?>
        </p>
    </div>
    <?php endif; ?>
    <form method="post" action="">
        <?php settings_fields('my_settings'); ?>
        <p style="margin-bottom:30px;">
            <label style="display:block;" class="input_label">ON/OFF Switch:</label>
            <br />
            <input name="my_option[my_display_tool]" type="checkbox" <?php if($my_display_tool == 'true') echo 'checked'; ?> value="true" />
            <!-- Add more my_option[FIELDNAME] to form for more fields -->
            <span class="checkbox_text">Turn This On</span><br />
        </p>
        <p>
            <input name="my_submit" type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
        </p>
    </form>
</div><!-- /.wrap -->
<?
}
?>
  • Share
Authored by: Adam J Nowak
http://hyperspatial.com

Comments and Feedback