Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Seems like a simple enough requirement, but I'm struggling:

  1. I want to add an option field to the "Add New Site".
    A simple text option is fine. How do I do this?

  2. I need to save this custom blog option when the site is created.

    I know I can use the 'wpmu_new_blog' hook, which passes the $meta variable with supplied options, but I'm wondering if there's a way to do this all in one step so I don't have to perform another DB query, or worse yet, override/replace the whole site creation process?

Notes:

I'm using WP 3.3.1 with a MultiSite setup. I'm writing a plugin that lists all the Sites which match the specified custom field value (let's call it "type").

Also, I'm an experienced PHP developer but I'm still relatively new to WordPress hacking, so please assume I am not yet intimately familiar with all of the various WP functions and hooks.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

Joining two answers(*), I've done a plugin to add a custom meta option when registering a new site (front and back end) and display its value in a column in the Sites screen.

Multisite Categories

enter image description here enter image description here ...

(*) code references

1 - WordPress + Multisite: How to add custom blog options to Add New Site form in Network Admin?, by @dennisg

2 - Multisite: How to add Custom Blog Options to new blog setup form?, by @tbuteler

The add_action('wpmu_new_blog', 'add_new_blog_field'); was merged from the two answers.

I was already using the ID column code in my multisite, and also added the Category column in the plugin.

[update]

Tried to make the Category column sortable, but seems almost unsolvable...

See this question/answer: Filters 'request' and 'parse_query' not firing in sites.php nor link-manager.php

[update 2]

I've reworked the plugin and now the categories are sortable. The first version of the plugin is a Gist now (link updated at the beginning) and the new version stays in GitHub.

share|improve this answer
    
+1 Hey this is great, brasofilo! I'm looking forward to giving this a try :) –  Brian Lacy May 9 '12 at 17:45
    
THANK YOU for this!!! What a time saver. :) –  Michelle Aug 1 '12 at 19:01

The Multisite signup process is not an easy thing to tweak. It does have many hooks to play with, unlike other processes, but they don't seem to come together too well for heavy customization, so I understand your frustration.

If I understand correctly, it's not that complicated to achieve what you want, although it cannot be a one-step solution without overriding much of WordPress' default behavior, mainly because signup (i.e. when your user's will submit the custom information) and activation (i.e. when the new blog will actually be created) happen separately.

Here's a very rough code that you will need to get started. Hopefully it steers you in the right direction:

// Add text field on blog signup form
add_action('signup_blogform', 'add_extra_field_on_blog_signup');
function add_extra_field_on_blog_signup() { ?>
    <label>An extra field</label>
    <input type="text" name="extra_field" value="" />
<?php
}

// Append the submitted value of our custom input into the meta array that is stored while the user doesn't activate
add_filter('add_signup_meta', 'append_extra_field_as_meta');
function append_extra_field_as_meta($meta) {
    if(isset($_REQUEST['extra_field'])) {
        $meta['extra_field'] = $_REQUEST['extra_field'];
    }
    return $meta;
}

// When the new site is finally created (user has followed the activation link provided via e-mail), add a row to the options table with the value he submitted during signup
add_action('wpmu_new_blog', 'process_extra_field_on_blog_signup', 10, 6);
function process_extra_field_on_blog_signup($blog_id, $user_id, $domain, $path, $site_id, $meta) {
    update_blog_option($blog_id, 'extra_field', $meta['extra_field']);
}

Keep in mind:

  1. If you want to filter the input via plug-in, I would strongly suggest using a select input to lock down the options provided;
  2. Since some fields are required and pass through validation during registration (minimum characters, disallowed characters for site name, blacklisted names, etc), I would suggest checking for your extra field on the $_REQUEST global when populating the value of the input on the first function, so that the user doesn't lose his input in case he's sent back to fix the value of WordPress' default fields;
  3. You can obviously replace the last function's idea with whatever suits your plug-in best, but the field's value will now be available from within that last function, which should help you on your way.

On a final note, if you want to process all the 'extra_field' values from all the existing blogs later on, I would strong suggest storing the information on a single table; maybe use update_site_option instead of update_blog_option on that last function, losing the blog_id, which is not required in that case.

Anyway, let us know how it goes, and good luck!

share|improve this answer
    
Great helpful answer, lots of information I can put to good use here :) I'm awarding Best Answer to brasofilo only because you already got the bounty, and both are great answers. –  Brian Lacy May 9 '12 at 17:47
    
Awesome answer here, thank you. Any idea why my extra field gets added to site options if I don't let the update_blog_option() run? I echo'd out the values inside process_extra_field_on_blog_signup() and exit() and it worked, the extra_filed was added to options. But when I just let it run through without exit() then the extra_field is NOT added to wp_xx_options. –  thatryan Jan 4 at 16:55
    
@thatryan, that's strange... Either something is removing the values from the database (not likely), or they're not being added at all. Try running some more tests and feel free to post a question if you're having trouble figuring it out. Keep in mind that this answer was probably for WordPress 3.5 or earlier, so they might have changed something in newer versions... –  tbuteler Jan 4 at 21:00

if you look at site-new.php code you would have gotten your answer already - and that is no!

There are no conditions checking to see if anything could be added to the form. However, I studied the file and believe you'll have to manually add this input field in site-new.php and pass the value as meta in the array to wpmu_create_blog function that you'll find somewhere near line 77. NOT TESTED but reading on what the function does it may be what you are looking for.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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