The MU forums have moved to WordPress.org

Automatically Updating Plug-in Options (3 posts)

  1. mdgross
    Member
    Posted 14 years ago #

    I've created an options page for a plug-in. By setting the form action to point to options.php and adding settings_fields('some-options'); to the form's body. It works great, with the exception of one "problem."

    It appears that the values are stored using update_option() and not update_site_option(). This would be fine if I wanted the options to be local to a single blog, but these particular options need to be accessible site-wide.

    I've thought about just doing a switch_to_blog(1) on the options page and when I call the options in the template. This hack seems less than ideal. I figured I'd probably just have to write my own script to receive and update the values. I'd prefer to use the built in method for security reasons (nonce for instance) if possible.

    Ideas?

  2. tdjcbe
    Member
    Posted 14 years ago #

    I'm a bit lost here.

    - Just to verify, the plugin is actually using the update_site_option function but it's saving the data to the individual blog's option table, right?

    - Are you access this options page while logged in as a site admin?

    edit: There's something in the back of my mind about problems with *_site_option but I can't find it right off.

  3. mdgross
    Member
    Posted 14 years ago #

    <?php
    
    add_action('admin_menu', 'my_plugin_menu');
    add_action('admin_init', 'my_registered_options');
    
    function my_registered_options()
    {
    	// Option Group, Option Name, Sanitize Function
    	register_setting('some-options', 'new_option_name', 'intval');
    	register_setting('some-options', 'some_other_option', 'intval');
    	register_setting('some-options', 'option_etc');
    }
    function my_plugin_menu()
    {
      add_options_page('My Plugin Options', 'My Plugin', 8, __FILE__, 'my_plugin_options');
    }
    
    function my_plugin_options() { ?>
    
    	<div class="wrap">
    		<h2>Your Plugin Name</h2>
    
    		<form method="post" action="options.php">
    			<?php settings_fields('some-options'); ?>
    
    			<table class="form-table">
    
    			<tr valign="top">
    				<th scope="row">New Option Name</th>
    				<td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    				<th scope="row">Some Other Option</th>
    				<td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
    			</tr>
    
    			<tr valign="top">
    				<th scope="row">Options, Etc.</th>
    				<td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
    			</tr>
    
    			</table>
    
    			<p class="submit">
    				<input type="submit" name="Submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    
    		</form>
    	</div>
    
    <?php } ?>

    Lets say you wanted this code to update the three options site-wide instead of the individual blog's option table.

About this Topic