Skip to:
Content
Pages
Categories
Search
Top
Bottom

Echo slug of custom nav component


  • maelga
    Participant

    @maelga

    Hi guys,

    I have created a few custom nav and respective screens using

    bp_core_new_nav_item()

    and everything works fine.

    Now I need to include hyperlinks to these navs/screens in some of my php files but I can’t figure out how to retrieve/echo those hyperlinks dynamically. I could kind of hardcode them based on the slug of each component, like this:

    echo bp_loggedin_user_domain(). '/mynavslug/';

    but this is obviously not the right solution because I might change the slugs at some point.

    How can I retrieve/echo the entire slug of a custom nav component dynamically?
    Does this need to be defined somewhere?

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

  • Henry Wright
    Moderator

    @henrywright

    You can define the slug when you call the bp_core_new_nav_item() function. For example:

    // Define slug in args array.
    $args = array(
        'slug' => 'foo'
    );
    bp_core_new_nav_item( $args );

    danbp
    Participant

    @danbp

    @maelga, please use the code button instead of blockquote.


    maelga
    Participant

    @maelga

    Thanks @henrywright
    Defining the slug with bp_core_new_nav_item() is not an issue.

    How can i get the slug to be echoed dynamically?
    I’m currently using echo bp_loggedin_user_domain(). '/mynavslug/'; to show the hyperlink in my theme files but i don’ t want the slug to be hardcoded.
    Maybe something like this echo $bp->bp_nav[]['link']?


    Henry Wright
    Moderator

    @henrywright

    Try buddypress()->foo->slug where foo is the name of your item. If this doesn’t work then “foo” may need to be a component.


    maelga
    Participant

    @maelga

    Thanks @henrywright. No luck with the above.

    I got it working with this:

    global $bp;
    echo $bp->bp_nav['foo']['link'];

    from the $bp doc https://codex.buddypress.org/developer/the-bp-global/

    I am now wondering whether there is already a function defined for this.
    Just like bp_loggedin_user_domain() works instead of global $bp; echo $bp->loggedin_user->domain;


    Henry Wright
    Moderator

    @henrywright

    I’m not aware of a function. Anyone else here know of one?

    You should be able to avoid the global by doing this:

    echo buddypress()->bp_nav['foo']['link'];


    maelga
    Participant

    @maelga

    Thanks @henrywright

    What’s the difference with calling global $bp?
    I guess if you’re advising the above, this might be performance related.


    Henry Wright
    Moderator

    @henrywright

    Using the buddypress() function avoids the need to declare a global. Try to avoid globals in your code if you can.

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