WordPress Preset Options Plugin

Late yesterday evening, my friend @immunity pointed me to Dan Gilmore’s post about hacking the database for WordPress MU to manually set options across a variety of blogs. @immunity figured there must be a better way and, indeed, there is.

In fact, I had written some code while at b5media which just did this. Because I am a persistent packrat, I still had that plugin and got the permission from b5media to release the very simple plugin publicly.

This plugin is perfect for WordPress MU administrators who want to exercise some control, whether for QA or otherwise, over the WordPress blogs on the system. Drop this plugin into the mu-plugins folder and it will be universally applied.

Couple things to think about… as for now, with this cleaned up but made-for-b5media plugin, there is no admin interface. You do have to edit the file manually to preset the options you want and you might have to comb your database options table to find the names of certain options, etc. This is not a braindead stupid plugin, but for those who are savvy enough to be administrators, it shouldn’t be too difficult to figure out via the example usage in the plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
Plugin Name: Preset Options
Plugin URI: #
Description: A plugin created when I was working for <a href="http://b5media.com">b5media</a> and released, with permission, under GPLv2. The plugin provides two functions that will set any blog option as defined and unchangeable. Perfect for WPMU.
Author: Aaron Brazell
Version: 1.0
Author URI: http://technosailor.com
*/


/*
Example Usage:

Turn off Smilies across the board:
po_hardcode_option('use_smilies', '0');

Remove the lifestream plugin from the list of active plugins.
po_hardcore_option_serialized('active_plugins', array('lifestream/lifestream.php'), false, true );
*/


/**
 * Function takes two arguments and is meant for setting a setting for a single string/boolean option. It is not meant for serialized data.
 **/

function po_hardcode_option($name, $value) {
        add_filter('pre_option_' . $name, create_function('$a', 'return "' . addslashes($value) . '";'));
}

/**
 * Function takes two required arguments, the name of an option and an array of key/value array data. Two optional arguments are boolean
 */

function po_hardcode_option_serialized( $setting, $options = array(), $removekey = false, $removevalue = false )
{
    if( !empty($options) )
    {
        if( $exist_options = get_option($setting) )
        {
            foreach( $options as $k =&gt; $v )
            {
                if( $removevalue )
                {
                    $key = array_search( $v, $exist_options );
                    if( $key != null )
                    unset( $exist_options[$key] );
                }
                else if( array_key_exists( $k, $exist_options) &amp;&amp; !is_int( $k ) )
                {
                    if( $removekey )
                        unset( $exist_options[$k] );
                    else
                        $exist_options[$k] = $v;
                }
            }
            add_filter('pre_option_' . $setting, change_option_array( $setting, $exist_options ) );
        }
    }
}

function change_option_array($setting, $array)
{
    update_option($setting, $array);
}
?&gt;

Download the plugin here.

So Dan, you would have just added the following line to the bottom of the plugin and would have been set. :-)

1
po_hardcode_option('comment_registration', true);