Web 2.0, Php, MySQL, HTML, CSS, Wordpress, Javascript, iTouch iPod, iPhone, Adobe…
23 Dec
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.
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: BuddyPress, PHP, Wordpress

23 Responses for "BuddyPress: Expanding Settings Menu"
Very nice! Glad you worked it out before I had the chance to write some how-to guides.
You can actually include this code in a separate file, then drop that into mu-plugins. This means you don’t have to modify the core files, and it will still run alongside BuddyPress.
Andy,
I’ll give that a try today thanks. Hate playing with core files. If there is a special naming convention or location for this new file let me know. Otherwise I will assume it’s just that easy.
No naming convention, call it what you like.
You might however want to put all your plugins in a sub folder in mu-plugins, then use a file in the root to call each file in that folder. That’s just for tidiness though - makes no difference to the actual code.
Andy,
Worked pretty well. Only thing that bugged me was that i was unable to add my new menu item in the settings sub-nav in the MIDDLE of the other items. You can see my screenshot to see “Social” was between “General” and “Notifications”. Now I can either get Social below these or above these (using “1″ or “2″ in the last variable in the add_action function).
Not a big deal. Just bothers me a tiny bit. But worth it not to touch the core file.
David, I’m getting an error when I click on your link to the attached file.
Wardeh,
My blog is attempting to run the php code. Duh. Try a “save link as” instead of actually clicking on it. Let me know if that works. Sorry, too much going on today!
IMPORTANT FIX: just got an email from david who asked that I post this link (he is on the road)
The provided link to bp-press is invalid (he apologizes)
Here is the code: http://davidbisset.com/wp-content/uploads/2008/12/bp-social.txt
Create your own file and upload as nec. Thanks,
Mike
i think you have neither store password for privacy :S nor using password :S I think you can do that using only feed rss and parse them for example with simplepie
Nicola,
If i understand you correctly… yes you could pull information from Twitter without a password via RSS.
But you can’t do this with most other social services. Plus you can easily MD5 the password in the database if you are big on security (and state to the user you are doing this). Remember that this was a proof of concept to show that you can store settings in the first place. Up to you how you want to use it.
David, thank for your plugin! Can I translate it into Russian and redistribute under your permission and rights?
slaFFik,
Of course!
I think the code is missing a ?> closing, am I right? don’t know much about php coding… PS: Im translating it to spanish, great plugin! Keep developing it!
As I understood, this plugin doesn’t create any page except in Settings? Than, what does this plugin do? No one can see your twitter status… Or I have missed something?
slaFFik,
Although it goes in the mu-plugin folder, it’s not really a plugin if you read my explanation above. The twitter in the screenshots was an example I used with this “proof of concept”. This was to show you how to setup your own settings (sub)menu, not a twitter plugin or a plugin really of any kind. My later posts are along the same theme - creating building blocks that lead to components and plugins.
Oh, understood, so it need to be ramade somehow to show twitter in Activity, for example.
Thanks for explanation, will try to do something with it
Oh yeah, even my building block posts don’t deal with merging anything into Activity. That is what i would consider a more advanced topic, but doable if i ever wanted to write another tutorial. There already exists a plugin that takes twitter posts and puts them into the WIRE (not ACTIVITY).
And where is that plugin? Could you give a link?
http://daveaubin.getpaidfrom.us/2008/12/07/buddypress-twitter-to-wire-plugin/
Thank you! Now I’m tryimg using your bp-test implement twitter to Activity with the ability to manage twitter status (writing and so on)
That’s cool. Keep me updated. I wish i had the time to create a building block or tutorial for intergrating with ACTIVITY. My examples assume you want to build something separate and it’s own page, etc. But I wish you well my friend!
Sure:)
It’s easy to integrate in Activity (I mean create there a page called Twitter), I created in Settings Twitter page too (using your code). The problem for me now is to make this 2 variables global:
$current_user->user_twitterusername
$current_user->user_twitterpassword
to call them and make twitter statuses appear. I don’t know what twitter library I need to use and how to confige it.
Trying to find somehing… Do you have any advice?
In my tests, I found a PHP twitter library in Google Code. Sorry, I don’t have exact url on me at the moment. Perhaps do a google search and/or ask in the BP forums.
I found: code.google.com/p/php-twitter/
Will see what I can do with it.
Thanks!
Leave a reply
You must be logged in to post a comment.