Update: Andy has suggested to make these modifications a separate file to drop into the mu-plugins folder. This works like a charm, and perfect because you do NOT have touch ANY core files. I have attached the file called bp-social here. So drop this into the mu-plugins folder and you should see a new submenu appear in settings on a profile (just like my dropdown). Read the comments below this post.

I have been playing with the BuddyPress beta recently, and planning on focusing on it’s impact to Social Networking in 2009 on my socialnetworking.com blog.

In the meantime, i’m more focused on my personal blog on expanding on the code. First, you must have played already with BuddyPress to understand this. And if you want to try this, you must already have a good foundation in PHP. This is not a “drop a plugin and it works” deal.

Settings Screen in BuddyPress

From what i’ve discovered, you need to modify one file for basic settings but unfort. it’s a core file. bp-core-settings.php. I have made my own modified version here: bp-core-settings.php (You may download, but use it at your own risk… this should work if you replace the core file, but do this with care and backing up any files. Remember this is for learning only).

Notice that adding a submenu within settings is simple enough at the top of the file itself.

bp_core_add_subnav_item( 'settings', 'social', __('Social', 'buddypress'), $bp['loggedin_domain'] . 'settings/', 'bp_core_screen_social_settings', false, bp_is_home() );

If this is new to you, then you will probably have to take some time learning what the parameters mean and where BuddyPress is grabbing them. The important thing to note here is the displayed name (”Social”) and the php function it goes along with (”bp_core_screen_social_settings”).

Next, later in the same file, I simply duplicated the general settings, renamed the php functions (I kept the same naming convention but replaced “general settings” with “social” since I was attempting to create a social settings panel). Make sure the first function below matches the function you stated in the bp_core_add_subnav_item that we just talked about it, or it isn’t going to work. (Mine is “bp_core_screen_social_settings”). Here is my code:

 
/**** SOCIAL NETWORKING SETTINGS ****/

function bp_core_screen_social_settings() {
	global $current_user, $bp_settings_updated, $pass_error;

	$bp_settings_updated = false;
	$pass_error = false;

	if ( isset($_POST['submit']) && check_admin_referer('bp_settings_social') ) {

		// Form has been submitted and nonce checks out, lets do it.

		if ( $_POST['twitterusername'] != '' ) {
			update_usermeta( (int)$current_user->id, 'twitterusername', $_POST['twitterusername'] );
			$bp_settings_updated = true;
			//$current_user->user_twitterusername = wp_specialchars( trim( $_POST['twitterusername'] ));
		}

		if ( $_POST['twitterpassword'] != '' && !strpos( " " . $_POST['twitterpassword'], "\\\" ) ) {
			update_usermeta( (int)$current_user->id, 'twitterpassword', $_POST['twitterpassword'] );
			$bp_settings_updated = true;
//			$current_user->user_twitterpassword = $_POST['twitterpassword'];
		}

	}

	add_action( 'bp_template_title', 'bp_core_screen_social_settings_title' );
	add_action( 'bp_template_content', 'bp_core_screen_social_settings_content' );

	bp_catch_uri('plugin-template');
}

function bp_core_screen_social_settings_title() {
	_e( 'Social Networking Settings', 'buddypress' );
}

function bp_core_screen_social_settings_content() {
	global $bp, $current_user, $bp_settings_updated, $pass_error; ?>

	<?php if ( $bp_settings_updated && !$pass_error ) { ?>
		<div id="message" class="updated fade">
			<p><?php _e( 'Changes Saved.', 'buddypress' ) ?></p>
		</div>
	<?php } ?>

	<?php if ( $pass_error && !$bp_settings_updated ) { ?>
		<div id="message" class="error fade">
			<p><?php _e( 'Your passwords did not match', 'buddypress' ) ?></p>
		</div>
	<?php } ?>

	<form action="<?php echo $bp['loggedin_domain'] . 'settings/social' ?>" method="post" id="settings-form">
		<label for="email">Twitter Username</label>
		<input type="text" name="twitterusername" id="twitterusername" value="<?php echo $current_user->twitterusername ?>" class="settings-input small" />

		<label for="pass1">Twitter Password (leave blank for no change)</label>
		<input type="password" name="twitterpassword" id="twitterpassword" size="16" value="" class="settings-input small" /> 

		<p><input type="submit" name="submit" value="Save Changes" id="submit" class="auto"/></p>
		<?php wp_nonce_field('bp_settings_social') ?>
	</form>
<?php
} 

What is neat about this is that you can create basically any field for any settings you want to store with the update_usermeta() function. If the setting isn’t stored in the wp_usermeta table already, it gets created. If it’s already there, it gets overwritten.

This is not a complete tutorial, and it’s more or less my own personal notes in how i made this happen. But it does show that it is possible to expand BuddyPress to hold custom “settings” values for use elsewhere on the site. For example, I am trying to create a settings panel to hold social networking username/passwords (like for Twitter) and be able to use these elswhere on the user’s profile section. Perhaps the beginning of a custom BuddyPress livestream or social networking plugin.

Please make sure you know about the BuddyPress forums.

Tags: , ,

Related posts