Show BuddyPress User Registration form everywhere on your BuddyPress site
Are you running a WordPress & BuddyPress based social site? Do you need to put custom user registration form on your site? If yes, in that case, this tutorial will help you.
By default, BuddyPress allows users to register from the Signup page( which you set in the BuddyPress settings ). There are times when you may want to display the registration form at other places, say you want to show BuddyPress registration form on home page or in the sidebar on all pages.
Today, In this tutorial I will show how to do that. We will learn two things in this tutorial.
- How to showing BuddyPress user registration form anywhere?
- and How to process user registration from any page?
Step 1:- Creating the registration form
For the sake of simplicity, I will just copy the registration form from bp-default theme( buddypress/bp-themes/bp-default/registration/register.php or buddypress/bp-templates/bp-legacy/buddypress/members/register.php )
I have copied the following code from it and saved it as register-form.php in my theme. You can do the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | <form action="<?php echo bp_get_signup_page();?>" name="signup_form" id="signup_form" class="standard-form" method="post" enctype="multipart/form-data"> <?php if ( 'registration-disabled' == bp_get_current_signup_step() ) : ?> <?php do_action( 'template_notices' ); ?> <?php do_action( 'bp_before_registration_disabled' ); ?> <p><?php _e( 'User registration is currently not allowed.', 'buddypress' ); ?></p> <?php do_action( 'bp_after_registration_disabled' ); ?> <?php endif; // registration-disabled signup step ?> <?php if ( 'request-details' == bp_get_current_signup_step() ) : ?> <?php do_action( 'template_notices' ); ?> <p><?php _e( 'Registering for this site is easy. Just fill in the fields below, and we\'ll get a new account set up for you in no time.', 'buddypress' ); ?></p> <?php do_action( 'bp_before_account_details_fields' ); ?> <div class="register-section" id="basic-details-section"> <?php /***** Basic Account Details ******/ ?> <h4><?php _e( 'Account Details', 'buddypress' ); ?></h4> <label for="signup_username"><?php _e( 'Username', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_username_errors' ); ?> <input type="text" name="signup_username" id="signup_username" value="<?php bp_signup_username_value(); ?>" <?php bp_form_field_attributes( 'username' ); ?>/> <label for="signup_email"><?php _e( 'Email Address', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_email_errors' ); ?> <input type="email" name="signup_email" id="signup_email" value="<?php bp_signup_email_value(); ?>" <?php bp_form_field_attributes( 'email' ); ?>/> <label for="signup_password"><?php _e( 'Choose a Password', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_password_errors' ); ?> <input type="password" name="signup_password" id="signup_password" value="" class="password-entry" <?php bp_form_field_attributes( 'password' ); ?>/> <div id="pass-strength-result"></div> <label for="signup_password_confirm"><?php _e( 'Confirm Password', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_password_confirm_errors' ); ?> <input type="password" name="signup_password_confirm" id="signup_password_confirm" value="" class="password-entry-confirm" <?php bp_form_field_attributes( 'password' ); ?>/> <?php do_action( 'bp_account_details_fields' ); ?> </div><!-- #basic-details-section --> <?php do_action( 'bp_after_account_details_fields' ); ?> <?php /***** Extra Profile Details ******/ ?> <?php if ( bp_is_active( 'xprofile' ) ) : ?> <?php do_action( 'bp_before_signup_profile_fields' ); ?> <div class="register-section" id="profile-details-section"> <h4><?php _e( 'Profile Details', 'buddypress' ); ?></h4> <?php /* Use the profile field loop to render input fields for the 'base' profile field group */ ?> <?php if ( bp_is_active( 'xprofile' ) ) : if ( bp_has_profile( array( 'profile_group_id' => 1, 'fetch_field_data' => false ) ) ) : while ( bp_profile_groups() ) : bp_the_profile_group(); ?> <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?> <div<?php bp_field_css_class( 'editfield' ); ?>> <?php $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() ); $field_type->edit_field_html(); do_action( 'bp_custom_profile_edit_fields_pre_visibility' ); if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?> <p class="field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>"> <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _ex( 'Change', 'Change profile field visibility level', 'buddypress' ); ?></a> </p> <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>"> <fieldset> <legend><?php _e( 'Who can see this field?', 'buddypress' ) ?></legend> <?php bp_profile_visibility_radio_buttons() ?> </fieldset> <a class="field-visibility-settings-close" href="#"><?php _e( 'Close', 'buddypress' ) ?></a> </div> <?php else : ?> <p class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>"> <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> </p> <?php endif ?> <?php do_action( 'bp_custom_profile_edit_fields' ); ?> <p class="description"><?php bp_the_profile_field_description(); ?></p> </div> <?php endwhile; ?> <input type="hidden" name="signup_profile_field_ids" id="signup_profile_field_ids" value="<?php bp_the_profile_field_ids(); ?>" /> <?php endwhile; endif; endif; ?> <?php do_action( 'bp_signup_profile_fields' ); ?> </div><!-- #profile-details-section --> <?php do_action( 'bp_after_signup_profile_fields' ); ?> <?php endif; ?> <?php if ( bp_get_blog_signup_allowed() ) : ?> <?php do_action( 'bp_before_blog_details_fields' ); ?> <?php /***** Blog Creation Details ******/ ?> <div class="register-section" id="blog-details-section"> <h4><?php _e( 'Blog Details', 'buddypress' ); ?></h4> <p><input type="checkbox" name="signup_with_blog" id="signup_with_blog" value="1"<?php if ( (int) bp_get_signup_with_blog_value() ) : ?> checked="checked"<?php endif; ?> /> <?php _e( 'Yes, I\'d like to create a new site', 'buddypress' ); ?></p> <div id="blog-details"<?php if ( (int) bp_get_signup_with_blog_value() ) : ?>class="show"<?php endif; ?>> <label for="signup_blog_url"><?php _e( 'Blog URL', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_blog_url_errors' ); ?> <?php if ( is_subdomain_install() ) : ?> http:// <input type="text" name="signup_blog_url" id="signup_blog_url" value="<?php bp_signup_blog_url_value(); ?>" /> .<?php bp_signup_subdomain_base(); ?> <?php else : ?> <?php echo home_url( '/' ); ?> <input type="text" name="signup_blog_url" id="signup_blog_url" value="<?php bp_signup_blog_url_value(); ?>" /> <?php endif; ?> <label for="signup_blog_title"><?php _e( 'Site Title', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label> <?php do_action( 'bp_signup_blog_title_errors' ); ?> <input type="text" name="signup_blog_title" id="signup_blog_title" value="<?php bp_signup_blog_title_value(); ?>" /> <span class="label"><?php _e( 'I would like my site to appear in search engines, and in public listings around this network.', 'buddypress' ); ?></span> <?php do_action( 'bp_signup_blog_privacy_errors' ); ?> <label><input type="radio" name="signup_blog_privacy" id="signup_blog_privacy_public" value="public"<?php if ( 'public' == bp_get_signup_blog_privacy_value() || !bp_get_signup_blog_privacy_value() ) : ?> checked="checked"<?php endif; ?> /> <?php _e( 'Yes', 'buddypress' ); ?></label> <label><input type="radio" name="signup_blog_privacy" id="signup_blog_privacy_private" value="private"<?php if ( 'private' == bp_get_signup_blog_privacy_value() ) : ?> checked="checked"<?php endif; ?> /> <?php _e( 'No', 'buddypress' ); ?></label> <?php do_action( 'bp_blog_details_fields' ); ?> </div> </div><!-- #blog-details-section --> <?php do_action( 'bp_after_blog_details_fields' ); ?> <?php endif; ?> <?php do_action( 'bp_before_registration_submit_buttons' ); ?> <div class="submit"> <input type="submit" name="signup_submit" id="signup_submit" value="<?php esc_attr_e( 'Complete Sign Up', 'buddypress' ); ?>" /> </div> <?php do_action( 'bp_after_registration_submit_buttons' ); ?> <?php wp_nonce_field( 'bp_new_signup' ); ?> <?php endif; // request-details signup step ?> <?php if ( 'completed-confirmation' == bp_get_current_signup_step() ) : ?> <?php do_action( 'template_notices' ); ?> <?php do_action( 'bp_before_registration_confirmed' ); ?> <?php if ( bp_registration_needs_activation() ) : ?> <p><?php _e( 'You have successfully created your account! To begin using this site you will need to activate your account via the email we have just sent to your address.', 'buddypress' ); ?></p> <?php else : ?> <p><?php _e( 'You have successfully created your account! Please log in using the username and password you have just created.', 'buddypress' ); ?></p> <?php endif; ?> <?php do_action( 'bp_after_registration_confirmed' ); ?> <?php endif; // completed-confirmation signup step ?> <?php do_action( 'bp_custom_signup_steps' ); ?> </form> |
To show the form, include it anywhere(In the sidebar.php or anywhere you need it).
1 2 | if( ! is_user_logged_in() ) get_template_part( 'register-form' ); //includes register-form.php |
Now, if you go and see your page, you will see an error like this:-
To make the form show, we need to add a line to initialize the registration step just before the form.
That line is
1 2 3 4 | $bp = buddypress(); if( empty( $bp->signup->step ) ) $bp->signup->step='request-details'; |
All we are doing here is telling BuddyPress that if the signup step is not set, please consider is as the request details step.
We can either put that code just above the form or in the code that includes the form.
Here is the updated code to include the form again.
1 2 3 4 5 6 7 | $bp = buddypress(); if( empty( $bp->signup->step ) ) $bp->signup->step='request-details'; if( ! is_user_logged_in() ) get_template_part( 'register-form' ); |
Once you have updated the code, you can see the registration form. Here is what I see in my sidebar.
That's all for showing the registration form.
Continue reading if you don't want to redirect your users to the default signup page and need to process the registration on the same page.
To make sure that the user registration gets processed on the same page where it resides, we will need to check if the form is submitted via POST method and then redirect back to the original page from the BuddyPress signup page.
Here is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | add_action( 'bp_core_screen_signup', 'buddydev_redirect_on_signup' ); /** * If the signup form is being processed, Redirect to the page where the signup form is * */ function buddydev_redirect_on_signup() { // Bail if not a POST action if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) return; $bp = buddypress(); //only if bp signup object is set if( ! empty( $bp->signup ) ) { //save the signup object and submitted post data $_SESSION['buddydev_signup'] = $bp->signup; $_SESSION['buddydev_signup_fields'] = $_POST; } bp_core_redirect( wp_get_referer()); } |
The above code checks if the form was submitted via POST method, then saves the post fields and $bp->signup object, and redirects back to the original page.
What we are actually doing here is letting BuddyPress do all the processing of the registration data and then redirect to the original page where the form is residing. We need to save the post fields for showing form input data on error and we save $bp->signup for showing error or signup confirmation message.
In the code below, we process the saved data from our previous step to show error or confirmation message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | add_action( 'bp_init', 'buddydev_process_signup_errors' ); function buddydev_process_signup_errors() { //we don't need to process if the user is logged in if( is_user_logged_in() ) return; //if session was not started by another code, let us begin the session if( ! session_id() ) session_start (); //check if the current request if( ! empty( $_SESSION['buddydev_signup'] ) ) { $bp = buddypress(); //restore the old signup object $bp->signup = $_SESSION['buddydev_signup']; //we are sure that it is our redirect from the buddydev_redirect_on_signup function, so we can safely replace the $_POST array if( isset( $bp->signup->errors ) && !empty( $bp->signup->errors ) ) $_POST = $_SESSION['buddydev_signup_fields'];//we need to restore so that the signup form can show the old data $errors = array(); if( isset( $bp->signup->errors ) ) $errors = $bp->signup->errors; foreach ( (array) $errors as $fieldname => $error_message ) { add_action( 'bp_' . $fieldname . '_errors', create_function( '', 'echo apply_filters(\'bp_members_signup_error_message\', "<div class=\"error\">" . stripslashes( \'' . addslashes( $error_message ) . '\' ) . "</div>" );' ) ); } //remove from session $_SESSION['buddydev_signup'] = null; $_SESSION['buddydev_signup_fields'] = null; } } |
That's all. Now, the registration will be processed on the same page.
Here is a screenshot showing error:-
and here is the registration success screen.
You can put the codes from this tutorial in your theme's functions.php or bp-custom.php.
I am hoping that the tutorial will help you to further improve your BuddyPress based social sites. Please do let me know if you want to see the above code/form as shortcode or widget? Looking forward to hear your suggestions & feedback 🙂
Awesome! Make a plugin out of it with a shortcode. =)
Hi Renato,
nice to see your comment after a long time.
Will have it as a shortcode by the end of this week 🙂
Awesome! =)
If you could make this into a shortcode, that would be amazing.
Hi Derek,
Thank you for the comment.
Will certainly have it as a shortcode by the end of this week 🙂
Awesome tutorial. You just saved me a massive headache. I was looking for how to create multiple BuddyPress registration forms and couldn't find anything else useful anywhere.
Thank you Daniel,
I am happy that you found it useful 🙂
Thanks again for an excellent tutorial. One issue that I am having… I have customised the form to include some XProfile fields. I can register using the form and the user account is created, however none of the XProfile fields are being populated.
Do I need to edit your code to account for the extra fields? I have created a Gist here if you want to take a look.
https://gist.github.com/ch1n3s3b0y/a3e6a06efaa15579799f
I've fixed my own problem. My custom form wasn't submitting the data so I just used the form you created above. My issue now is trying to set a user role during the registration process. Causing me a ton of issues at the moment.
Works great! Tried for hours to get a working registration from. Keep up the excellent work!
Thank you Donald.
Glad that you found it useful 🙂
Hello, many thanks for this post it is exactly what I am trying to do !
For the moment I still have an error to display the form.
My code looks like the following:
Can you see any error in line with the steps that you describe ?
Thank you very much !!
Thomas
Just to clarify: My question is mainly on the "To make the form show, we need to add a line to initialize the registration step just before the form." step in your tutorial as I have exactly the error that you mention before this step.
What line has to be added and where (and why) ?
Many thanks !!
Thomas
Hi Thomas,
I have updated the post again with the code. Please take a look and let me know if it works now or not? It seems that we had lost that section of code earlier while changing our Syntax highlighter plugin.
Hello Brajesh,
Thank you so much !!!
It is working now, you are amazing.
I wish you an excellent day.
Thomas
Hi Thomas,
Glad that it is fixed now. happy to help. Have a great day to you too 🙂
Hello Brajesh,
Following this first success with the registration page I would like to create a template for the "member profile" page (buddypress/members/single/home.php).
The idea is to be able to display the user profile in 2 different pages. For example http://[site url]/members/user1 and http://[site url]/fr/membre/user1 would both be display the profile of user1 (or the edit profile page if the loggedin user is user1).
Do you know if a similar technique as described in this post makes it possible ?
Thank you !
Thomas
Hi
I have used the code above to redirect the user back to the page that the registration form is on, but it is redirecting the user back to the homepage. Do you know why this might be?
mee too.
Hi Lukid,
I am sorry I could not reply in time to Daniel.
Can you please post your code on pastebin and link me in our support forums?
I will be happy to assist.
Thank you
Brajesh
It's not working anymore. I followed it until:
"Continue reading if you don’t want to redirect your users to the default signup page and need to process the registration on the same page."
Form looks ok but after registration user just doesn't exist, there is no any confirmation message too. Also missed password strenght meter.
Hi Mariusx,
Please allow me to check it today. The password strength will not work(as BuddyPress only loads it on the Registration page). rest all should have.
Are you using any custom plugin to manage registration? Is your site multisite or normal WordPress?
No custom plugin and it's a normal WP.
Very good tutorial. However, how do I make a page not reload.
I want to add ajax admin, but I stop here.
thanks.
You will need to write the registration handler yourself in that case. In the above example, we are using BuddyPress's handler.
We have done that for one of our plugins BuddyPress Ajax registration.
https://buddydev.com/plugins/bp-ajax-registration/