Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to re-produce Notifications in a Custom Admin bar?


  • Gianfranco
    Participant

    @gian-ava

    For Cureuphoria (http://cureuphoria.com) I disabled the default admin bar and decided to design a custom one that would fit the design better.

    define('BP_DISABLE_ADMIN_BAR', true);

    When you’re not logged in, the bar displays the login fields, and a link to create an account if you don’t have one.

    When you are logged in, the bar displays the member avatar, and links such as: My profile – My messages – My friends – My groups – Settings – Logout.

    Now, I’d like to have the notifications system in that bar, like for the default bar I desabled.

    First question is: can I do that?

    Second question is: if I can, how?

    Here’s my code:

    <div id="curemates-bar">

    <?php if ( is_user_logged_in() ) : //logged in ?>

    <?php do_action( 'bp_before_sidebar_me' ) ?>

    <a href="<?php echo bp_loggedin_user_domain() ?>"><?php bp_loggedin_user_avatar( 'type=thumb&width=30&height=30' ) ?></a>

    <ul id="user-menu">
    <li><a href="<?php echo bp_loggedin_user_domain() ?>/profile/edit/">My profile</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/messages/">My messages</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/groups/">My groups</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/friends/">My friends</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/photos/">My photos</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/settings/">Settings</a></li>
    <li>| <a id="logout" href="<?php echo wp_logout_url( get_option('home') ) ?>"><?php _e( 'Log Out', 'buddypress' ) ?></a></li>
    </ul>

    <?php do_action( 'bp_sidebar_me' ) ?>

    <?php do_action( 'bp_after_sidebar_me' ) ?>

    <?php if ( function_exists( 'bp_message_get_notices' ) ) : ?>
    <?php bp_message_get_notices(); /* Site wide notices to all users */ ?>
    <?php endif; ?>

    <?php else : //not logged in ?>

    ...stuff...

    <?php endif; ?>

    </div><!-- end "curemates-bar" -->

    Now, I now that the code I am looking for is in the “bp-core-adminbar.php” file, and I’ve made a few attempts to extract what needed to re-create the notifications menu, but obviously without success.

    Can anybody help me out with this?

    Thanks.

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

  • Nahum
    Participant

    @nahummadrid

    I use this in a custom admin menu. you may need to fiddle with it to make it dropdown. I had to add a dropdown menu style.

    <li> | <?php bp_adminbar_notifications_menu(); ?> </li>


    Gianfranco
    Participant

    @gian-ava

    Thanks nahummadrid, I am going to try it out. Didn’t know about that function.


    Gianfranco
    Participant

    @gian-ava

    Nope, that doesn’t work for me. Are we sure it’s not a deprecated function?


    Brandon Allen
    Participant

    @cnorris23

    It is not a deprecated function. How are you calling the function? Is there any output at all? You should at least get a menu item that says “Notifications,” but no count may show.


    Nahum
    Participant

    @nahummadrid

    Are you logged in or logged out? link color not visible, maybe.


    Gianfranco
    Participant

    @gian-ava

    I’ve put the function right in the ul, like so:

    <ul id="user-menu">

    <li><a href="<?php echo bp_loggedin_user_domain() ?>/profile/edit/">My profile</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/messages/">My messages</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/groups/">My groups</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/friends/">My friends</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/photos/">My photos</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/settings/">Settings</a></li>
    <li>| <a id="logout" href="<?php echo wp_logout_url( get_option('home') ) ?>"><?php _e( 'Log Out', 'buddypress' ) ?></a></li>

    <?php bp_adminbar_notifications_menu(); ?>

    </ul>

    …to see waht it produces. It just breaks my layout. The whole ul disapeears and the layout after that is broken.

    In Firebug I can see this error: Call to undefined function bp_adminbar_notifications_menu()

    Mmm… I got BP Template Pack installed, if that makes it a hint of any kind.


    Nahum
    Participant

    @nahummadrid

    define(‘BP_DISABLE_ADMIN_BAR’, true); kills it. try removing the admin bar with one of the plugins that are out there and then see if that works.


    r-a-y
    Keymaster

    @r-a-y

    Or make a duplicate of the bp_adminbar_notifications_menu() function, rename it and use it.


    Gianfranco
    Participant

    @gian-ava

    @r-a-y I tried this:

    <?php function my_bp_adminbar_notifications_menu() {
    global $bp;

    if ( !is_user_logged_in() )
    return false;

    echo '<li id="bp-adminbar-notifications-menu"><a href="' . $bp->loggedin_user->domain . '">';
    _e( 'Notifications', 'buddypress' );

    if ( $notifications = bp_core_get_notifications_for_user( $bp->loggedin_user->id ) ) { ?>
    <span><?php echo count($notifications) ?></span>
    <?php
    }

    echo '</a>';
    echo '<ul>';

    if ( $notifications ) { ?>
    <?php $counter = 0; ?>
    <?php for ( $i = 0; $i < count($notifications); $i++ ) { ?>
    <?php $alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; ?>
    <li<?php echo $alt ?>><?php echo $notifications[$i] ?></li>
    <?php $counter++; ?>
    <?php } ?>
    <?php } else { ?>
    <li><a href="<?php echo $bp->loggedin_user->domain ?>"><?php _e( 'No new notifications.', 'buddypress' ); ?></a></li>
    <?php
    }

    echo '</ul>';
    echo '</li>';
    }
    ?>

    Which is the same funtion except for the name “my_bp_adminbar_notifications_menu”.

    It doesn’t print anything.

    Probably something with the “bp_core_get_notifications_for_user” that is desabled because of my desabling the default Admin bar?

    I admit that I php is not my strong point at all.


    r-a-y
    Keymaster

    @r-a-y

    Did you actually call the function after creating it?

    eg. somewhere in your theme add:

    <?php if( function_exists('my_bp_adminbar_notifications_menu') ) my_bp_adminbar_notifications_menu(); ?>


    Gianfranco
    Participant

    @gian-ava

    I tried this, doesn’t print anything neaither:

    <ul id="user-menu">
    <li><a href="<?php echo bp_loggedin_user_domain() ?>/profile/edit/">My profile</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/messages/">My messages</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/groups/">My groups</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/friends/">My friends</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/photos/">My photos</a></li>
    <li>| <a href="<?php echo bp_loggedin_user_domain() ?>/settings/">Settings</a></li>

    <?php function my_bp_adminbar_notifications_menu() {
    global $bp;

    if ( !is_user_logged_in() )
    return false;

    echo '<li id="bp-adminbar-notifications-menu"><a href="' . $bp->loggedin_user->domain . '">';
    _e( 'Notifications', 'buddypress' );

    if ( $notifications = bp_core_get_notifications_for_user( $bp->loggedin_user->id ) ) { ?>
    <span><?php echo count($notifications) ?></span>
    <?php
    }

    echo '</a>';
    echo '<ul>';

    if ( $notifications ) { ?>
    <?php $counter = 0; ?>
    <?php for ( $i = 0; $i < count($notifications); $i++ ) { ?>
    <?php $alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; ?>
    <li<?php echo $alt ?>><?php echo $notifications[$i] ?></li>
    <?php $counter++; ?>
    <?php } ?>
    <?php } else { ?>
    <li><a href="<?php echo $bp->loggedin_user->domain ?>"><?php _e( 'No new notifications.', 'buddypress' ); ?></a></li>
    <?php
    }

    echo '</ul>';
    echo '</li>';
    }
    ?>

    <?php if( function_exists('my_bp_adminbar_notifications_menu') ) my_bp_adminbar_notifications_menu(); ?>

    </ul>

    Is anything in there correct?


    Brandon Allen
    Participant

    @cnorris23

    I just tried the code you posted above, and it works. I get all the correct output. Are you actually logged into the site when you test this. The way the function works is that it won’t print any of the notification menu unless you are logged in. Also, I would actually move the my_bp_adminbar_notifications_menu() function to your theme’s functions.php file, and then leave the <?php if( function_exists('my_bp_adminbar_notifications_menu') ) my_bp_adminbar_notifications_menu(); ?> as is.


    Gianfranco
    Participant

    @gian-ava

    Thanks cnorris23!

    I am logged in, of course, otherwise I couldn’t see.

    I’ll try to put the function in “bp-custom.php” (yeah, guess is the same than if I put it in functions.php), since I got such file.

    But it it didn’t work in the template file, it normally won’t work in there.

    Will report.


    Gianfranco
    Participant

    @gian-ava

    Well, it did work!

    Put the function in “bp-custom.php”, and called teh function from the template file.

    Thanks cnorris23!

    And thanks to you r-a-y, for the right input.

    Now it’s a matter of styling the menu.

    I should be able to get the dropdown effect only with CSS, witthout any jQuery, right?

    When I got the result I want, I’ll mark this as resolved.

    Thanks guys, that was an important feature to have for the site.

    Hello, I’m new to BuddyPress and I have a question about this topic. Someone I could clearly explain the steps you need to do to create a button with notifications?

    Web:http://www.familiapixel.com (is a spanish page)
    Email: seravifer@hotmail.com

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to re-produce Notifications in a Custom Admin bar?’ is closed to new replies.
Skip to toolbar