The MU forums have moved to WordPress.org

Manual blog creation, different username from title... (5 posts)

  1. Person
    Member
    Posted 14 years ago #

    This isn't an actual question, but since I was looking for this stuff before, I thought it might be potentially useful for someone else. All of the following mainly applies for the manual creation of blogs in Site Admin -> Blogs, as opposed to users signing up for an account.

    What I first noticed is that the blog address field doesn't check for invalid characters, so it's possible to create blogs with spaces and other symbols that will cause them to not load properly. To fix this, open wp-includes/wpmu-functions.php, and find the function called function wpmu_create_blog.

    below

    if( empty($path) )
    		$path = '/';

    add

    if ((!ereg("[A-Za-z0-9|-|_]",$path)) && (ereg("[!-)+=\';\|\\¦-Ã]",$path)))
    		return new WP_Error('blog_name_', __("The Blog Address contains one or more invalid characters. Please return and try again."));
    
        $path = ereg_replace(' ','_',$path);
        $path = stripslashes($path);

    Another thing you'll notice is that when you create a blog, the username is exactly the same as the blog address itself. Not everyone likes to have their path and user the same, so why not provide the option. While I'm at it, I'll also tell you how you can add an extra field to specify the password, and a checkbox that allows you to choose whether to forward the notification e-mail or not (perhaps you want to set up a few things for a specific client before letting them know it's ready). You don't have to add all these options though, it's your choice (I thank Andrej for some tips).

    In wp-admin/wpmu-blogs.php, at the bottom of the page where there's a cell for the admin email, add the following under it:

    $username = wp_specialchars($blog['username'] ); //.
            $password = wp_specialchars($blog['password'] ); //.
    
    		if ( empty($domain) || empty($email))
    			wp_die( __('Missing blog address or email address.') );
    		if( !is_email( $email ) )
    			wp_die( __('Invalid email address') ); 
    
            if ( empty($username) || empty($password))
    			wp_die( __('Missing username or password.') );
    
            if ((!ereg("[A-Za-z0-9|-|_]",$username)) && (ereg("[!-)+=\';\|\\¦-Ã]",$username)))
    			wp_die( __('Invalid username') );
    
    		$username = ereg_replace(' ','_',$username);
    		$username = stripslashes($username);
    
    		if( constant('VHOST') == 'yes' ) {
    			$newdomain = $domain.".".$current_site->domain;
    			$path = $base;
    		} else {
    			$newdomain = $current_site->domain;
    			$path = $base.$domain.'/';
    		}
    
    		//$password = 'N/A';
    		$user_id = email_exists($email);
    		if( !$user_id ) {
    			//$password = generate_random_password();
    			$user_id = wpmu_create_user( $username, $password, $email );
    			if(false == $user_id) {
    				wp_die( __('There was an error creating the user') );
    			} else {
    				if($blog['notify']) {
    					wp_new_user_notification($user_id, $password);
    				}
    			}
    		}
    
    		$wpdb->hide_errors();
    		$id = wpmu_create_blog($newdomain, $path, $title, $user_id , array( "public" => 1 ), $current_site->id);
    		$wpdb->show_errors();
    		if( !is_wp_error($id) ) {
    			if( get_user_option( $user_id, 'primary_blog' ) == 1 )
    				update_user_option( $user_id, 'primary_blog', $id, true );
    
    			if($blog['notify']) {
    				$content_mail = sprintf( __( "New blog created by %1s\n\nAddress: http://%2s\nName: %3s"), $current_user->user_login , $newdomain.$path, stripslashes( $title ) );
    				wp_mail( get_site_option('admin_email'),  sprintf(__('[%s] New Blog Created'), $current_site->site_name), $content_mail, 'From: "Site Admin" <' . get_site_option( 'admin_email' ) . '>' );
    				wpmu_welcome_notification( $id, $user_id, $password, $title, array( "public" => 1 ) );
    			}
    			wp_redirect( add_query_arg( array('updated' => 'true', 'action' => 'add-blog'), $_SERVER['HTTP_REFERER'] ) );
    			exit();
    		} else {
    			wp_die( $id->get_error_message() );
    		}
    	break;

    From my tests everything works as it should, the only thing to take into account is that the blog and the user are technically created separately, so if you have odd characters in the username but not the blog address, then the blog will still be created without the user, and vice-versa.

  2. andrea_r
    Moderator
    Posted 14 years ago #

    "Not everyone likes to have their path and user the same, so why not provide the option. "

    If they sign up as just a user, then go back to the signup page and get a blog, they can pick whatever they like.

  3. Person
    Member
    Posted 14 years ago #

    As I stated from the beginning, the point is to provide these options on the admin side, rather than only on the signup page. It is also advantageous for those who do not want the signup page to be accessible to users, and as such have registrations disabled. And although you could protect the signup page to only allow yourself access, this isn't a very feasible solution, and if you're logged in as administrator, then a newly created blog is automatically assigned your username, password, and e-mail. If you add the user afterwards, then unless you modify the user portion of wpmu-edit.php, it'll also automatically send an e-mail. The above modifications I provided make all of this a one-stop shop when creating a blog & user via Site Admin -> Blogs (as originally stated).

  4. Argentum
    Member
    Posted 14 years ago #

    I think that Person has some valid point. I tried to register a user with username adam.smith

    I the went ahead and created the blog URL as
    domain.com/bp/adam.smith

    It broke the blog. The link to the css files in the page headers were set to:
    domain.com/bp/adam.smith/wp-content/themes/bp-default/"stylesheetname".css

    I think it would be great if a check could be made for invalid characters and help the user. It is not that unusal to use firstnamn.lastname :)

    Wordpress-MU 2.8.4
    Buddy Press 1.1.1
    Installed from latest, no upgrades.

  5. tim.moore
    Member
    Posted 14 years ago #

    If you'd like this to be considered for inclusion into WordPress core files, you should file a ticket at trac.mu.wordpress.org.

About this Topic

  • Started 14 years ago by Person
  • Latest reply from tim.moore