Friday 15 November 2013

Manually creating a Buddypress menu

The simplest method:

bp_get_displayed_user_nav();

But this works only when the current page is a member page (url is http://host/members/username). Because the function generates a menu with relative links.

The below code will replace the relative links with absolute ones:


$bp_user_link = bp_loggedin_user_domain();
  ob_start();
 bp_get_displayed_user_nav();
 $the_user_nav = ob_get_clean();
 
 print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));

bp_loggedin_user_domain gives the url http://host/members/username/

Since bp_get_displayed_user_nav directly prints the code, to capture the code in a variable we have to use ob_start();

One odd thing is that this command will not output the Messages menu in non-member pages. Therefore this has to be obtained separately.

  $bp_user_link = bp_loggedin_user_domain();
  $messages_count = messages_get_unread_count();
  $messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages

<span>$messages_count</span>
</a></li>\n\n"


Putting it together:



$bp_user_link = bp_loggedin_user_domain();
$messages_count = messages_get_unread_count();
$messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages 

<span>$messages_count</span>
</a></li>\n\n";

      $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      ob_start();
      bp_get_displayed_user_nav();
      $the_user_nav = ob_get_clean();
     
      print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));


$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
ob_start();
bp_get_displayed_user_nav();
$the_user_nav = ob_get_clean();
print str_replace('Profile','Profile'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));
This code works on a stand-alone page. However, when the code should be modified when putting it in a template. Because the same template is used in member and non-member pages.

$current_url_part = "$_SERVER[REQUEST_URI]";
$current_url_part_9 = substr($current_url_part,0,9);

if(!($current_url_part_9 == "/members/" && $current_url_part != "/members/")) // the second check is because the members page is simply http://192.168.42.20/members/, it doesn't have the username after it
{
 /* put an if over here where you check wether the url is under members. if so, show the regular bp_get_displayed_user_nav */
$bp_user_link = bp_loggedin_user_domain();
$messages_count = messages_get_unread_count();
$messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages<span> $messages_count</span>
</a></li>\n\n";
//print $messages_code;
ob_start();
bp_get_displayed_user_nav();
$the_user_nav = ob_get_clean();
 
print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));
}
else
{
bp_get_displayed_user_nav();
}     
For the message count to be dynamically updated, the following html structure should be used since there is jquery that keeps the count updated:

<a id="user-messages">

<span><?php echo messages_get_unread_count(); ?></span>
</a>

No comments:

Post a Comment