BuddyDev

Search

[Resolved] Create a tab (nav_item) that can display custom post type items of user.

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46402

    Hello 🙂

    I would like to create a tab (nav_item) in every users profile that can display the custom post type of some events that are assigned to the user.
    The plugin https://buddydev.com/plugins/buddyblog/ can do that just fine, but i don’t need all this extra code for creating,editing,deleting posts. Only the display part. Is there a way i could use specific part of the code for this?

    old conversation here https://buddypress.org/support/topic/resolved-show-posts-written-by-user-on-profile/

    I would also need to manipulate the code a bit so that the tab is being created only for specific user roles (for example only for Editors and Subscribers).

    Could you please help me?

    Kind Regards,
    Lefteris

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #46424

    Hi Lefteris,
    are you facing any issue with the code suggested on BuddyPress.org forums?

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46453

    Hello Brajesh,

    Thank you very much for your reply 🙂
    I have managed to create a nav_item and set a custom post type loop displaying my events in it.
    Here is my code. https://pastebin.com/xYCV5HzB (Please could you check it and let me know if i did any mistake?)

    However i am trying to fix 2 issues:
    1. i am having difficulties to set this nav_item only for specific user roles.
    This means that only specific roles will have this custom nav_item on the public members page and on their profile.(For example only Editors,Administrators and Producers)
    Somethink like

    $allowed_roles = array('editor', 'administrator', 'author');
    if( array_intersect($allowed_roles, $user->roles ) ) { 

    2. In the post loop i am trying to display the categories that are assigned to the custom post. (Also maybe set a better code format instead of all those echo)

    I would appreciate any help.

    Best Regards,
    Lefteris

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46516

    Hello Brajesh,

    I have tried to use your code example here https://buddydev.com/support/forums/topic/profile-tabs-depending-to-user-role-or-member-type/#post-11773 but still it doesn’t work 100% as it should.
    It removes the nav_item from all the users that are not for example with role ‘subscribers’ but if the current login user is an ‘author’ (and he should be able to access his own events custom nav_item) and he is on the screen of a subscriber the nav_item is getting removed for him.

    *Basically my theme has a profile menu as a dropdown in the users screen top right corner and i can see that the nav_item is getting removed.

    Thank you for any help.

    Best Regards,
    Lefteris

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #46524

    Hi Lefteris,
    Thank you for the replies.

    I am sorry, I haven’t been able to assist you due to my current work. Please allow me to look over them and get back to you on Monday.

    regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46528

    Hello Brajesh,

    Thank you for your reply and your patience 🙂

    You don’t need to check my second question.I got to display the categories correct.

    I am just trying to register this nav_tab only for specific roles.I have tried to use your code example here https://buddydev.com/support/forums/topic/profile-tabs-depending-to-user-role-or-member-type/#post-11773 but is not working exactly as it should.

    Please let me know when you have a check.

    Best Regards,
    Lefteris

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46607

    Hello Brajesh,

    Did you managed to have a check on the condition that i am trying to figure out (register the nav_tab only for specific roles)?

    Best Regards,
    Lefteris

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #46611

    Hi Lefteris,
    Thank you for the replies.
    I had a look.

    You can update your function tot his to limit to certain roles.

    
    function mb_bp_profile_menu_radio_shows() {
    
    	if ( ! bp_is_user() ) {
    		return;
    	}
    
    	$user = get_user_by( 'id', bp_displayed_user_id() );
    
    	if ( ! array_intersect( $user->roles, array( 'editor', 'administrator', 'author' ) ) ) {
    		return;
    	}
    
    	bp_core_new_nav_item(
    		array(
    			'name'                => 'ΕΚΠΟΜΠΕΣ',
    			'slug'                => 'shows',
    			'position'            => 30,
    			'default_subnav_slug' => 'shows',
    			'screen_function'     => 'producer_shows',
    		)
    	);
    
    }
    

    For cleaning up the second part, I will suggest using get_template_part to load content from your theme/child theme.

    Here, i have loaded the file producer-content.php from theme(you can change as you please)

    
    function producer_shows_content_screen() {
    	get_template_part( 'producer-content' ); // loads producer-conten.php from theme.
    }
    
    

    and you can put the markup there in this template file.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46612

    Hello Brajesh,

    Thank you very much for your kind help.

    I will cleanup the second part as you mentioned. However my main problem is about the certain roles tab creation.

    Your code works correct as expected but there is one problem. When the user with role ‘editor’ visits a profile page of a user that is not in the ΄allowed roles΄ (for example with role ‘subscriber’) then the nav_item is getting removed also for him (that it shouldn’t, since he is with role ‘editor’).

    I can see that because my theme has a drop-down profile menu in the right top corner section of the page, so this item is getting removed for the current logged in user. https://cdn2.wpbeginner.com/wp-content/uploads/2020/04/creating-a-submenu-1.png

    I hope you could understand what i am trying to say 🙂

    Best Regards,
    Lefteris

    • This reply was modified 1 year, 6 months ago by Lefteris.
  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #46681

    Hello Brajesh,

    I am sorry for bothering. Could you check my comment above? How could i fix your code to work properly?

    Best Regards,
    Lefteris

The topic ‘ [Resolved] Create a tab (nav_item) that can display custom post type items of user.’ is closed to new replies.

This topic is: resolved