Brian Lacy
Brian Lacy

Reputation: 19098

WordPress + Multisite: How to add custom blog options to Add New Site form in Network Admin?

In a WordPress Multisite installation I'm customizing, I need to add a simple text field to the entry form for creating new blog sites, which is located at

Network Admin > Sites > Add New

Naturally I need this field to get saved along with the other meta data from that form, in the {new_blog_prefix}_options table.

I'm particularly interested for the simplest, most straightforward, and/or the "right way" (i.e. The WordPress Way) to accomplish this, but I'll settle for The Way That Works™!

So far I've encountered numerous dead-ends in my research:

So... no dice. Any help is appreciated!

Upvotes: 17

Views: 12563

Answers (2)

Michelle
Michelle

Reputation: 570

I just wanted to share my solution, which is to add a site setting for the color. If the setting does not exist, it will be set the next time a super admin or admin visits the site, to their color choice. If it is set, it will be used in place of the user's setting. Here's the code if it helps anyone else who comes across this later:

add_filter('get_user_option_admin_color', 'change_admin_color');
function change_admin_color($user_admin_color) {
    global $_wp_admin_css_colors;
    $admin_colors = $_wp_admin_css_colors;
    $admin_color = $user_admin_color;
    if ( 1 == $wpmu ) {
        if ( ! get_site_option( 'site_admin_color' ) && is_super_admin() ) {
            add_site_option( 'site_admin_color', $user_admin_color); //add, not update
            return $user_admin_color;
        } else {
            $admin_color = get_site_option( 'site_admin_color' );
            if( key_exists($admin_color,$admin_colors) ) {
                return $admin_color;
            }
        }
    } else {
        if ( ! get_option( 'site_admin_color' ) && is_admin() ) {
            add_option( 'site_admin_color', $user_admin_color, '', 'yes' ); //add, not update
            return $user_admin_color;
        } else {
            $admin_color = get_option( 'site_admin_color' );
            if( key_exists($admin_color,$admin_colors) ) {
                return $admin_color;
            }
        }
    }
    return $user_admin_color;
}

Upvotes: 1

dennisg
dennisg

Reputation: 4368

If you look at the source code of the 'Add New Site' page, you can see that WordPress does not provide a hook for this purpose. Of course, it would be possible to add a hook yourself, but it is generally bad practice to edit the WordPress core.

However, once submitted and all information is present, the page calls the function wpmu_create_blog(). In this function there is a hook called, namely the action wpmu_new_blog:

do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );

At this point the blog is already created. However, we can still detect if a field was submitted by hooking onto this action and saving it into the database. We add the following into our plugin file (or template):

function add_new_blog_field($blog_id, $user_id, $domain, $path, $site_id, $meta) {

    // Make sure the user can perform this action and the request came from the correct page.

    switch_to_blog($blog_id);

    // Use a default value here if the field was not submitted.
    $new_field_value = 'default';

    if ( !empty($_POST['blog']['new_field']) )
        $new_field_value = $_POST['blog']['new_field'];

    // save option into the database
    update_option( 'new_field', $new_field_value);

    restore_current_blog();
}

add_action( 'wpmu_new_blog', 'add_new_blog_field' );

As for displaying the field onto the page, you could use a JavaScript approach. You add a javascript file, solely to the 'Add New Site' page, and onLoad of the page you insert the field into the correct position on the page. You should add an input field with the name 'blog[new_field]'. We create the following JavaScript file which, once loaded, adds a new field to the 'Add New Site' page:

(function($) {
    $(document).ready(function() {
        $('<tr class="form-field form-required"></tr>').append(
            $('<th scope="row">New field</th>')
        ).append(
            $('<td></td>').append(
                $('<input class="regular-text" type="text" title="New Field" name="blog[new_field]">')
            ).append(
                $('<p>Explanation about your new field</p>')
            )
        ).insertAfter('#wpbody-content table tr:eq(2)');
    });
})(jQuery);

Now the only thing left to do is include this file onto the 'Add New Site' page, by adding this to your plugin file:

// Only add the script for the page site-new.php (the page hook).
add_action( "admin_print_scripts-site-new.php", 'my_admin_scripts' );

function my_admin_scripts() {
    wp_register_script('yourScript', plugins_url('js/yourScript.js', __FILE__));
    wp_enqueue_script('yourScript');
}

Further suggestion could be, depending on your needs: Add an input field in your general settings page using add_settings_field, such that a user can edit it later (and maybe only if it is the default setting for this field). A 'you cannot change this field later' approach.

I hope this is the help you needed.

Upvotes: 28

Related Questions