Force BuddyPress to allow unique group names only

Jotted by brajesh on July 3, 2010

As you may be knowing, BuddyPress does not forces users to create unique group names. If you are the webmaster of a social network, you may not want to allow same group name for multiple groups as it will cause confusions among the users. So, here is a small code snippet to force buddypress to allow creation of unique groups only.

add_filter("groups_group_name_before_save","allow_unique_names_only");
function allow_unique_names_only($name){
    global $bp;
 
  $groups=BP_Groups_Group::search_groups($name);//search groups
  $duplicate=false;
  if('group-details'==$bp->groups->current_create_step&&!empty($groups)&&$groups['total']>0){
      //we have match, but is there an exact match
      foreach($groups['groups'] as $g){
         $group=new BP_Groups_Group($g->group_id);
 
         if($group->name==$name){
              $duplicate=true;//found duplicate
              break;//break the loop;
          }
      }
if($duplicate){
        bp_core_add_message("A group with this name already exists. Please Choose another name.","error");
        bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $bp->groups->current_create_step . '/' );
  }
 
}
 return $name;
}

Put the above code in your bp-custom.php and you are good to go. Go ahead and test by creating groups with same name. It will inform you that a group with same exists, please use another name.

Credit: Thanks goes to “gwu” for asking this question in buddydev forum.

I am looking forward to your comments.

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]


WordPress 3.0 and the problem with Mass Updates fail

Jotted by brajesh on July 1, 2010

Have you got the message “Briefly unavailable for scheduled maintenance. Check back in a minute“. Well, It is far too common with wordpress 3.0 if you are using mass updater and if the update fails.

WordPress puts your site to maintinence mode while you use mass updater and if the update fails(or one of the update fails) wordpress does not put back the site to live mode and making everything practically inaccesible

There is an easy workaround.

Check the folder in which wordpress is installed and look for a file called .maintinence. Once you locate it, just delete it and your site will be back to normal.

I believe the mass plugin updater is a welcome addition but leaving the site in maintenance mode when update fails is not a good thing. Anyway, that’s my opinion. What do you think of it? Have you faced the similar problem. If yes, what was your workaround, I would love to hear from you.

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]


Changing default component for landing page of User Profile in Buddypress

Jotted by brajesh on June 30, 2010

So you must have seen that the default landing page for a user profile in BuddyPress is User activity page. Sometimes you may want to change it to rather the profile page or wire or some other page.
The good thing is, it’s easy, well super easy to do that. What you need to do is put a line in bp-custom.php

//define the default component
define("BP_DEFAULT_COMPONENT","profile");// Now when you click on a user name link, You will land on User's profile not user's activity page

Similarly, you can use any of the active component as the landing page. For example, if you are using my wire plugin, you can define BP_DEFAULT_COMPONENT to be “wire” and the wire will be the landing page. Or if you want, you may define “groups” and User’s Groups page will be the landing page but I don’t think you will ever want to do that.
I see only two three practical uses of it.

1. Using xprofile page(or profile page) as default landing page
In that case define BP_DEFAULT_COMPONENT to be “profile”

2. Using Wire as the landing page
In that case define BP_DEFAULT_COMPONENT to be “wire” or what ever you have put as BP_WIRE_SLUG(in case you have customized the slug).

3. For creating a landing page which aggregates user activities, shows users groups and others
In that case, I assume you have created a custom landing page(which is pretty easy and I will be discussing about it in one of the articles) and want it to be the default case.

So try changing and experimenting with “BP_DEFAULT_COMPONENT”  and may be you can have better usage ideas for it . As always, any comment suggestions or anything, please let me know in comments.

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]


Enable Blog Admin menu in buddypress adminbar for Single User wordpress

Jotted by brajesh on June 28, 2010

Everyone likes shortcuts(and may be a few not), and so do I. Since my personal preference of WordPress Multiuser(or now multisite/multi network what ever you call them) I missed one thing the most while working on my BuddyPress site with WordPress single User and It was the My Blogs drop down menu. It provided me with a quick option to navigate to WordPress Dashboard or to new post screen and so on. On single user WordPress I had to manually type wp-admin in the url to get to the dashboard.

Fade up with the number of times I had to type this, I rather created a small code snippet which enabled me to navigate to the dashboard/posts screen with the same efficiency(well even took less time as it has to be a single level navigation).
here is a screenshot from my test site.
And here is the code snippet to enable this

add_action( 'bp_adminbar_menus', 'bp_adminbar_blogs_menu_single_wp', 6 );
function bp_adminbar_blogs_menu_single_wp() {
	global $bp;
 
	if ( !is_user_logged_in()||!current_user_can("publish_posts") )//show this menu to author
		return false;
 
        $blog =bpdev_get_single_wp_blog_info();
 
 
	echo '<li id="bp-adminbar-blogs-menu"><a href="' . $bp->root_domain .'/wp-admin/">';
 
	_e( 'Blog Admin', 'buddypress' );
 
	echo '</a>';
	echo '<ul>';
 
 
 
		if ( !empty( $blog) ) {
			$alt = ' class="alt"';
			$site_url = esc_attr( $blog->siteurl );
 
			echo '<li' . $alt . '>';
 
			echo '<li class="alt"><a href="' . $site_url . 'wp-admin/">' . __( 'Dashboard', 'buddypress' ) . '</a></li>';
			echo '<li><a href="' . $site_url . 'wp-admin/post-new.php">' . __( 'New Post', 'buddypress' ) . '</a></li>';
			echo '<li class="alt"><a href="' . $site_url . 'wp-admin/edit.php">' . __( 'Manage Posts', 'buddypress' ) . '</a></li>';
			echo '<li><a href="' . $site_url . 'wp-admin/edit-comments.php">' . __( 'Manage Comments', 'buddypress' ) . '</a></li>';
 
 
		}
 
 
	echo '</ul>';
	echo '</li>';
}
//create dummy blog object, we can even avoid it too, as we only need site_url and nothing else
function bpdev_get_single_wp_blog_info(){
    global $bp;
    $blog=new stdClass();
    $blog->siteurl=$bp->root_domain."/";
    $blog->name=get_bloginfo('name');
    return $blog;
}

In fact, the code for bp_adminbar_blogs_menu_single_wp is actually taken from “bp_adminbar_blogs_menu”[bp-core-adminbar.php] and modified for our purpose.
You can put the above code in your themes functions.php or in bp-custom.php or just create a plugin and drop it. That’s it.
Hope it helps, please do let me know if you use it and find any issues.

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]


Automatically adding users to all blog on your wpmu/wp multisite blog network

Jotted by brajesh on June 27, 2010

There are times when you want all of the registered member on your blog network to be members of all the blog. WordPress does it differently. By default all the registered users are added to the main blog with the role you have specified(or “subscriber” by default ). If the user creates a new blog, they are removed from the main blog and added to the new blog they created.

My solution here will allow to automatically add users to all blogs on your network, it does not matter much if they register a new one or they do not have a blog but just a registered member. In case the user registers new blog, this function will do no harm the user will be admin of the new blog as in normal case.

To our help, there are two functions

is_blog_user():-It checks whether the current logged in user is a member of the current blog or not.

add_user_to_blog($blog_id,$user_id,$role):- it allows you to add an existing user to a blog with the specific role where $role can be “subscriber”,”author”,”editor”,”contributor” or “administrator”

Now the code, putting the following code in your theme’s functions.php will do the job or you may wrap it as a plugin in case you want it that way

 
add_action('wp','bpdev_add_user_to_blog',10);//hook our function
 
/*add logged in user to current blog*/
function bpdev_add_user_to_blog(){
if(!is_user_logged_in())
return false;//do not do anything
 
global $current_user,$blog_id;
 
if(!is_blog_user())//check for current membership
add_user_to_blog($blog_id, $current_user->ID, "subscriber");//give subscriber role for current blog
 
}

And if you are using buddypress, you can put the above code in bp-custom.php.

What it does:-

When a logged in user visits a blog on your network, it checks whether he/she is a member of the current blog or not. If not, it adds them as a member to the current blog.

I hope it helps in some of the cases. Let me know it it works for you or not ?

[blinklist] [Bloglines] [del.icio.us] [Digg] [dzone] [Facebook] [Ma.gnolia] [Mister Wong] [Reddit] [Sphere] [Sphinn] [StumbleUpon] [Technorati] [Email]



Get Adobe Flash playerPlugin by wpburn.com wordpress themes