Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Combined Register + Post

Okay, after much lucky guessing with regards to the user object handling, I have an ALPHA VERSION of a plugin to do this. There’s nothing to stop spambots, this is purely a proof of concept, I’ll try adding a captcha or something later if necessary. Email verification sounds like a royal pain though. Save the files exactly where I’ve said and as ever, make sure you leave no whitespace around the PHP tags. Apart from that, please test it but it’s your own risk if you run it on a production server.

my-plugins/register-post/load.php:

<?php
/*
Plugin Name: Register & Post
Description: Allows a user to register and make a post at the same time.
Author: Kawauso
Version: Alpha
*/

// Add the post form
add_action( 'post_post_form', 'register_post_form' );

function register_post_form() {

global $h2, $forum, $topic;

if( bb_is_user_logged_in() )
return;

// Setup the register form

$profile_info_keys = bb_get_profile_info_keys();

unset( $profile_info_keys['first_name'], $profile_info_keys['last_name'], $profile_info_keys['display_name'], $profile_info_keys['occ'], $profile_info_keys['from'], $profile_info_keys['user_url'], $profile_info_keys['interest'] );

$bb_register_error = new WP_Error;

$user_login_error = $bb_register_error->get_error_message( 'user_login' );

?>
<style type="text/css">p#post-form-forum-container{display:none}</style>
<form class="postform post-form" id="postform" method="post" action="<?php bb_uri( 'my-plugins/register-post/create.php', null, BB_URI_CONTEXT_FORM_ACTION )?>">
<h2 class="postform">Register and post?</h2>
<fieldset>
<table width="100%">
<tr class="form-field form-required required<?php if ( $user_login_error ) echo ' form-invalid error'; ?>">
<th scope="row">
<label for="user_login"><?php _e('Username'); ?></label>
<?php if ( $user_login_error ) echo "<em>$user_login_error</em>"; ?>
</th>
<td>
<input name="user_login" type="text" id="user_login" size="30" maxlength="30" value="<?php echo $user_login; ?>" />
</td>
</tr>
<?php

if ( is_array($profile_info_keys) ) :
foreach ( $profile_info_keys as $key => $label ) :
$class = 'form-field';
if ( $label[0] ) {
$class .= ' form-required required';
}
if ( $profile_info_key_error = $bb_register_error->get_error_message( $key ) )
$class .= ' form-invalid error';

?>

<tr class="<?php echo $class?>">
<th scope="row">
<label for="<?php echo $key?>"><?php echo $label[1]?></label>
<?php if ( $profile_info_key_error ) echo "<em>$profile_info_key_error</em>"; ?>
</th>
<td>
<input name="<?php echo $key?>" type="text" id="<?php echo $key?>" size="30" maxlength="140" value="<?php echo $$key?>" />
</td>
</tr>

<?php

endforeach; // profile_info_keys
endif; // profile_info_keys

?>
</table>

<br />

<?php
bb_load_template( 'post-form.php', array('h2' => $h2) );
bb_nonce_field( bb_is_topic() ? 'create-post_' . $topic->topic_id : 'create-topic' );
if ( bb_is_forum() ) {
echo '<input type="hidden" name="forum_id" value="' . $forum->forum_id . '" />' . "n";
} elseif ( bb_is_topic() ) {
echo '<input type="hidden" name="topic_id" value="' . $topic->topic_id . '" />' . "n";
}
echo "n</fieldset>n</form>n";

}

my-plugins/register-post/create.php:

<?php

require_once('../../bb-load.php');

$profile_info_keys = bb_get_profile_info_keys();

unset( $profile_info_keys['first_name'], $profile_info_keys['last_name'], $profile_info_keys['display_name'], $profile_info_keys['occ'], $profile_info_keys['from'], $profile_info_keys['user_url'], $profile_info_keys['interest'] );

$user_login = '';
$user_safe = true;

$bb_register_error = new WP_Error;

if ( $_POST && 'post' == strtolower($_SERVER['REQUEST_METHOD']) ) {
$_POST = stripslashes_deep( $_POST );
$_POST['user_login'] = trim( $_POST['user_login'] );
$user_login = sanitize_user( $_POST['user_login'], true );
if ( $user_login !== $_POST['user_login'] ) {
$bad_input = true;
if ( $user_login )
$bb_register_error->add( 'user_login', sprintf( __( "%s is an invalid username. How's this one? <strong>%s</strong>" ), esc_html( $_POST['user_login'] ), $user_login ) );
else
$bb_register_error->add( 'user_login', sprintf( __( '%s is an invalid username.' ), esc_html( $_POST['user_login'] ) ) );
}

foreach ( $profile_info_keys as $key => $label ) {
if ( is_string($$key) )
$$key = esc_attr( $$key );
elseif ( is_null($$key) )
$$key = esc_attr( $_POST[$key] );

if ( !$$key && $label[0] == 1 ) {
$bad_input = true;
$$key = false;
$bb_register_error->add( $key, sprintf( __( '%s is required' ), $label[1] ) );
}
}

if ( !$bad_input ) {

// Invoke non-user post checks here
if ( !$post_content = trim($_POST['post_content']) )
bb_die(__('You need to actually submit some content!'));

if ( isset($_POST['topic']) && $forum_id = (int) $_POST['forum_id'] ) {

$topic = trim( $_POST['topic'] );

if ('' == $topic)
bb_die(__('Please enter a topic title'));

} elseif ( isset($_POST['topic_id'] ) ) {
$topic_id = (int) $_POST['topic_id'];
bb_check_admin_referer( 'create-post_' . $topic_id );
}

if ( !topic_is_open( $topic_id ) )
bb_die(__('This topic has been closed'));

// Invoke registration
$user_id = bb_new_user( $user_login, $_POST['user_email'], null );
if ( is_wp_error( $user_id ) ) { // Error creating user

foreach ( $user_id->get_error_codes() as $code )
$bb_register_error->add( $code, $user_id->get_error_message( $code ) );

if ( $bb_register_error->get_error_message( 'user_login' ) )
$user_safe = false;

} elseif ( $user_id ) { // Registration success

foreach( $profile_info_keys as $key => $label )
if ( strpos($key, 'user_') !== 0 && $$key !== '' )
bb_update_usermeta( $user_id, $key, $$key );

do_action('register_user', $user_id);

// Create the current user object

$wp_auth_object->set_current_user( $user_id );

$bb_current_user = $wp_auth_object->current;

// Do last minute post/topic checks
if ( !empty($topic) && $forum_id = (int) $_POST['forum_id'] ) {

if ( !bb_current_user_can('write_posts') )
bb_die(__('You are not allowed to post. Are you logged in?'));

if ( !bb_current_user_can( 'write_topic', $forum_id ) )
bb_die(__('You are not allowed to write new topics.'));

if ( !bb_current_user_can( 'write_topic', $forum_id ) )
bb_die(__('You are not allowed to write new topics in this forum.'));

bb_check_admin_referer( 'create-topic' );
$topic_id = bb_new_topic( $topic, $forum_id, $tags );

}

if ( !bb_current_user_can( 'write_post', $topic_id ) )
bb_die(__('You are not allowed to post in this topic.'));

// Create the new post
$post_id = bb_new_post( $topic_id, $_POST['post_content'] );

$tags = trim( $_POST['tags'] );
bb_add_topic_tags( $topic_id, $tags );

$link = get_post_link( $post_id );

$topic = get_topic( $topic_id, false );

if ( $topic->topic_posts )
$link = add_query_arg( 'replies', $topic->topic_posts, $link );

do_action( 'bb-post.php', $post_id );

if ( $post_id )
wp_redirect( $link );
else
wp_redirect( bb_get_uri(null, null, BB_URI_CONTEXT_HEADER) );
exit;
}
}
}

// If you've reached this point, there must have been an error

$user_login_error = $bb_register_error->get_error_message( 'user_login' );

if( empty( $user_login_error ) ) // For some reason, error strings are not always available (i.e. for invalid emails)
$user_login_error = __( 'Something went wrong! Check what you entered and try again.' );

bb_die( $user_login_error );

Skip to toolbar