Skip to:
Content
Pages
Categories
Search
Top
Bottom

Creating links to BP specific pages via functions.php


  • kaneesha
    Participant

    @kaneesha

    Hi,

    first of all i did a search and my coding knowledge is at best “casual” so there were probably solutions to my question inside the search results but since i’m as i said a casual coder at best there is more than just a slight change that haven’t recognized them as solutions … and well now i’m here.

    So please bear with me if possible 🙂

    So i implemented two snippets in my WP theme functions.php, the one shows unread messages and the other one all unread notifications, so far so good! But i’d like to assign both with proper links (clickable urls) to their logical destinations so in this case either to the BP unread messages or BP unread notifications pages.

    Here is my code so far:

    //notification alerts
    function my_nav_menu_notif_counter($menu) {      
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<span class="noticecount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span>';
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' ); 
    
    //msg notification alerts
    function my_nav_menu_msg_counter($menu) {      
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<span class="noticecount2">'. bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .'</span>';
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_msg_counter' ); 

    So what magic is it actually to set the links properly?

    Thank you in advance :))

Viewing 8 replies - 1 through 8 (of 8 total)

  • shanebp
    Moderator

    @shanebp

    Suggestion…

    //notification alerts
    function my_nav_menu_notif_counter( $menu, $args ) {      
    	if (!is_user_logged_in()) {
    		return $menu;
    	} else {
    		 if( $args->theme_location == 'primary' ) {	
    			$unread = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    			if ( 0 != $unread ) {	
    				$link = '<a href="' . bp_get_notifications_unread_permalink() . '">Notifications '. $unread . '</a>';
    				$notif = '<span class="noticecount">' . $link . '</span>';
    				$menu .= $notif;
    			}
    		}
    	}
    	return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter', 10, 2 ); 

    shanebp
    Moderator

    @shanebp

    Note changes to suggestion above in $link.


    kaneesha
    Participant

    @kaneesha

    Hi, first of all thanks for the reply.

    I’ve tested your suggestion but it doesn’t seem to work for me (quad-checked also formatting errors) even the previously visible counters are now gone, or in other words my DIV which contained the counters up till now is now completely empty.

    :/


    shanebp
    Moderator

    @shanebp

    It was just an example.
    You’ll have to adjust it, probably this line:
    if( $args->theme_location == 'primary' ) {
    Remove it for testing.
    It just checks to see which nav you are working on.
    Or replace ‘primary’ with your menu name.

    It’s not much code; you need to walk thru each line and understand it.


    kaneesha
    Participant

    @kaneesha

    Hi there,

    So i’ve managed it with a mix of my initial code which i posted firstly and your permalink, so i’ve changed basically this:

    $notif = '<span class="noticecount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span>';

    to this:

    $notif = '<a href="' . bp_get_notifications_unread_permalink() . '"><span class="noticecount"><i class="noticecounttitle">Notifications</i>'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a>

    Ok so far so good, but this alchemy failed badly with my unread messages version.
    I tested bp_get_messages_permalink(), or bp_get_unread_messages_permalink() and basically the wierdest combinations one can imagine but nothing worked.

    I’ve searched then here the BP froums plus additionally stack overflow but it seems there are so many techniques to link stuff that i’m utterly lost here.

    There were also permalink tags with the term slug this and slug that so i thought i ask here again before my head explodes.


    shanebp
    Moderator

    @shanebp

    If you aren’t distributing the code, then you know the slug ( read it from the browser url ), then just build the url:

    $notif = '<a href="' . bp_loggedin_user_domain() . 'messages/' . '"> etc


    kaneesha
    Participant

    @kaneesha

    Ah that was it, thank you very much.
    I’m sure i’m going to learn plenty of things in the future about how BP’s code works that’s for sure.

    I appreciate your help and your time a lot.


    kaneesha
    Participant

    @kaneesha

    Hi again,

    Is it possible to don’t display any notifications if there are none? Right now it shows Unread Messages: 0

    Thank you in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar