5

How do you remove the 'Site name' field in the register form for a multisite?

When users sign up, I just would like them to enter the 'user name', 'email' and 'domain name'. I tried removing it with CSS but can't because it is a required field (must be filled in).

If anyone wants to know why I need to do this, it is because my multisite is designed for a specific use and doesn't use site titles at all - there is only one site per user and only one administrator user per site allowed. Each site displays the user administrator's display name at the top for each site instead of the site title.

Any help appreciated.

  • 3
    The registration form can be customized with hooks. Perhaps instead of removing the Site Name field entirely, you can modify it so that the site name is populated by the username instead. – Jami Gibbs Jul 3 '15 at 15:19
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.