Skip to:
Content
Pages
Categories
Search
Top
Bottom

Simplified blog creation – blogname generated from Blog Title


  • peterverkooijen
    Participant

    @peterverkooijen

    I’m trying to simplify blog creation. This customized version of the form works surprisingly well:

    function custom_signup_show_blog_form( $blogname = '', $blog_title = '', $errors = '' ) {
    global $current_site;

    ?>
    <div id="blog-details-fields">
    <input name="blogname" type="text" id="blogname" value="<? echo $blogname = str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))); ?>" maxlength="50" />

    <label for="blog_title"><?php _e( 'Blog Title:', 'buddypress' ) ?></label>
    <?php if ( $errmsg = $errors->get_error_message('blog_title') ) { ?>
    <p class="error"><?php echo $errmsg ?></p>
    <?php }
    echo '<input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" /></p>';

    ?>

    <p>
    <label for="blog_public_on"><?php _e( 'I would like my blog to appear in search engines like Google and Technorati, and in public listings around this site.', 'buddypress' ); ?> </label>
    <label class="checkbox" for="blog_public_on">
    <input type="radio" id="blog_public_on" name="blog_public" value="1" <?php if( !isset( $_POST['blog_public'] ) || '1' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
    &nbsp;<?php _e( 'Yes', 'buddypress' ); ?>
    </label>
    <label class="checkbox" for="blog_public_off">
    <input type="radio" id="blog_public_off" name="blog_public" value="0" <?php if( isset( $_POST['blog_public'] ) && '0' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
    &nbsp;<?php _e( 'No', 'buddypress' ); ?>
    </label>
    </p>
    </div>
    <?php
    do_action('signup_blogform', $errors);
    }

    It’s two fields. When you put a ‘Blog Title’ in the second field and click ‘Create Blog’, you’re returning to the same form, but with ‘blogtitle’ added to the first field.

    If you hit ‘Create Blog’ again, the blog is created at thewebsite.com/blogtitle.

    It works, but as it is it’s a bit silly and unexpected. Does anyone have suggestions how to finish this?

    My original plan was to make the first input field hidden. Then you only have one field: blog title + button: Go!

    But you’d still have to hit the ‘Create Blog’ button twice. Or is there a trick to make it a double strenght click?

    Another solution would be to have a message appear on the first click: “you’re URL will be… confirm?” But then you’d have to tell the user not to put something in the first field… :-(

    Should I replace the ‘echo’ in this line:

    <? echo $blogname = str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))); ?>

    with something else that immediately sends the value to the rest of the script, without first having to display it in the field? Perhaps ‘return’?

    To a real PHP coder the solution is probably obvious. Please share it, spare me endless fruitless experiments. (It’s now close to midnight, have to stop myself…)

Viewing 11 replies - 1 through 11 (of 11 total)

  • r-a-y
    Keymaster

    @r-a-y

    I think there’s a benefit to allowing the blog URL to be chosen.

    What if someone decides to put in a super-long title for their blog?

    eg. “My fantabulously great blog on widgets” would turn into:

    hxxp://example.com/myfantabulouslygreatblogonwidgets/ or

    hxxp://myfantabulouslygreatblogonwidgets.example.com

    Which wouldn’t be great.


    peterverkooijen
    Participant

    @peterverkooijen

    Good point r-a-y! I’ll put a limit on the number of characters for the title field. Problem solved!

    It’s a matter of choice of course. I try to follow the less is more school of interface design, think Apple – I use pc myself, not a Mac fanboy, just making a point.

    Does anybody know what I should use instead of ‘echo’ to send that value for blogname straight to the rest of the script?

    Or can I do something like this:

    <input name="blogname" type="hidden" id="blogname" value="$blogname" maxlength="50" />
    <?php
    $blogname = str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1)));
    ?>

    I think I’ve already tried this, but it didn’t do anything. I’ll try again…


    peterverkooijen
    Participant

    @peterverkooijen

    Latest attempt didn’t work… :-(

    Should I do something with $_POST['blogname']?

    Any ideas very much appreciated!

    BTW, improved cleanup:

    $blogname = preg_replace('/[^a-z0-9]/i', '', str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))));


    peterverkooijen
    Participant

    @peterverkooijen

    This doesn’t work either…:

    ...
    echo '<input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" /></p>';

    $blogname = preg_replace('/[^a-z0-9]/i', '', str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))));

    ?>

    <input name="blogname" type="text" id="blogname" value="<? wp_specialchars($blogname, 1); ?>" maxlength="50" />
    ...


    peterverkooijen
    Participant

    @peterverkooijen

    This also does not work…

    echo '<input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" maxlength="24" /></p>';
    $blogname = preg_replace('/[^a-z0-9]/i', '', str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))));
    $_POST['blogname'] = $blogname;

    Please, if anyone knows how to do this, put me out of my misery.


    peterverkooijen
    Participant

    @peterverkooijen

    This also does not work…:

    function autoblogname() {
    $blogname = preg_replace('/[^a-z0-9]/i', '', str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))));
    return $blogname;
    }

    ...

    echo '<input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" maxlength="24" /></p>';
    ?>
    <input name="blogname" type="text" id="blogname" value="<? $blogname = autoblogname(); ?>" />

    Help! :-(


    peterverkooijen
    Participant

    @peterverkooijen

    Removing the blogname field from the form and adding this to the next (?) function also does not work:

    function bp_core_signup_validate_blog_form() {
    $user = '';
    if ( is_user_logged_in() )
    $user = wp_get_current_user();

    $_POST['blogname'] = preg_replace('/[^a-z0-9]/i', '', str_replace(' ', '', strtolower(wp_specialchars($blog_title, 1))));

    return wpmu_validate_blog_signup($_POST['blogname'], $_POST['blog_title'], $user);
    }

    Is there any way to make that work or is it another dead end?


    peterverkooijen
    Participant

    @peterverkooijen

    Another approach. With this bit of Javascript you can copy the input from blog_title to blogname as you type:

    <script type = "text/javascript">
    function copyIt() {
    x = document.getElementById("blog_title").value;
    document.getElementById("blogname").value = x;
    }
    </script>

    ...

    <input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" maxlength="24" onkeyup="copyIt()"/>

    <input name="blogname" type="text" id="blogname" value="" />

    Now if I could figure out how to sanitize/lowercase the input between blog_title and blogname, this could be a really cool solution.

    A big if again… Suggestions very much appreciated!


    peterverkooijen
    Participant

    @peterverkooijen

    Success! Solution found here.

    The Javascript:

    <script type = "text/javascript">
    function copyinput()
    {
    var tmp = document.getElementById('blog_title').value;
    tmp = tmp.toLowerCase().replace(/^s+|s+$/g, "").replace(/[_|s]+/g, "");
    tmp = tmp.replace(/[^a-z0-9-]+/g, "").replace(/[-]+/g, "-").replace(/^-+|-+$/g, "");
    document.getElementById('blogname').value = tmp;
    }
    </script>

    The customized function:

    function custom_signup_show_blog_form( $blogname = '', $blog_title = '', $errors = '' ) {
    global $current_site;

    ?>
    <div id="blog-details-fields">

    <label for="blog_title">Blog title</label>
    <?php if ( $errmsg = $errors->get_error_message('blog_title') ) { ?>
    <p class="error"><?php echo $errmsg ?></p>
    <?php }
    echo '<input name="blog_title" type="text" id="blog_title" value="'.wp_specialchars($blog_title, 1).'" maxlength="24" onkeyup="copyinput()"/></p>';

    // Blog name
    if ( 'no' == constant( "VHOST" ) )
    echo '<label for="blogname">' . __('Blog Name:', 'buddypress') . '</label>';
    else
    echo '<label for="blogname">' . __('Blog Domain:', 'buddypress') . '</label>';

    if ( $errmsg = $errors->get_error_message('blogname') ) { ?>
    <p class="error"><?php echo $errmsg ?></p>
    <?php }

    if ( 'no' == constant( "VHOST" ) ) {
    echo '<span class="prefix_address">' . $current_site->domain . $current_site->path . '</span><input name="blogname" type="text" id="blogname" value="'.$blogname.'" maxlength="50" /><br />';
    } else {
    echo '<input name="blogname" type="text" id="blogname" value="'.$blogname.'" maxlength="50" /><span class="suffix_address">.' . $current_site->domain . $current_site->path . '</span><br />';
    }

    if ( !is_user_logged_in() ) {
    echo '<p class="help-text">';
    print '(<strong>' . __( 'Your address will be ', 'buddypress' );
    if( 'no' == constant( "VHOST" ) ) {
    print $current_site->domain . $current_site->path . __( 'blogname', 'buddypress' );
    } else {
    print __( 'domain.', 'buddypress' ) . $current_site->domain . $current_site->path;
    }
    echo '</strong>. ' . __( 'Must be at least 4 characters, letters and numbers only. It cannot be changed so choose carefully!)', 'buddypress' ) . '</p>';
    echo '</p>';
    }
    ?>
    <p>
    <label for="blog_public_on"><?php _e( 'I would like my blog to appear in search engines like Google and Technorati, and in public listings around this site.', 'buddypress' ); ?> </label>
    <label class="checkbox" for="blog_public_on">
    <input type="radio" id="blog_public_on" name="blog_public" value="1" <?php if( !isset( $_POST['blog_public'] ) || '1' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
     <?php _e( 'Yes', 'buddypress' ); ?>
    </label>
    <label class="checkbox" for="blog_public_off">
    <input type="radio" id="blog_public_off" name="blog_public" value="0" <?php if( isset( $_POST['blog_public'] ) && '0' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
     <?php _e( 'No', 'buddypress' ); ?>
    </label>
    </p>
    </div>
    <?php
    do_action('signup_blogform', $errors);
    }

    What this does:

    When the user starts typing the blog title – limited to 24 chars – the Javascript dynamically fills in the blogname for the URL.

    The user can manually change the blogname if he/she wants. Not sure how that would work out…

    You could probably also hide the blogname field, either through type=”hidden” or CSS. Haven’t tested all these options yet.

    But imho this looks like a very versatile solution. :-)

    Or have I overlooked some critical issues? Comments very much appreciated!


    r-a-y
    Keymaster

    @r-a-y

    I’d definitely leave the blog URL field shown… you can say that the blog URL is prefilled in according to your blog name, but can be modified to suit your fancy!


    peterverkooijen
    Participant

    @peterverkooijen

    r-a-y, I keep the blog URL field visible, but add ‘readonly’ to the input tag.

    I’ve noticed the same blog creation code is also in bp-blogs-template-tags.php, which is used in the members areas. So if you want to use this method, you’ll also have to hack that file. :-(

    Why don’t both blog creation pages use the same form and processing functions?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Simplified blog creation – blogname generated from Blog Title’ is closed to new replies.
Skip to toolbar