[AP documentation] Adding tabs and pages to users profile

Solved6.33K viewsGeneral
1

Hi all!

I’m trying to use AP and AP profiles as a base for a social network going beyond the point of a “simple” Q&A community. In order to do so, I’m trying to customize AP user profile page to show more elements by using previous answers provided on this forum and trying to find my way in all the PHP files of the plugin (I’m no developer and this is my first serious attempt at getting a working customized code). I know some other users are trying to achieve similar results. So, with this new thread, I would like to:

  1. summarize what have been already said
  2. share what I have been able to achieve
  3. get some help in going a bit further
  4. add relevant information and pieces of code gathered to AP documentation

[Solved] Add/remove link from user menu

Easy to add links with this and remove some menu items with this. For a “Logout” link, it’s pretty useful and straightforward. But what if I want to display some content like the Following tab or the About tab?

[Solved…?] Add a tab with corresponding content in user menu

I’ve been trying among other things to display badges earned through the BadgeOS plugin. By reading this message, I have been able to understand (?) that I should: register a new “Badges” page, setup the output for the “Badges” page and, of course, create the corresponding badges.php. Is that correct?

Here is how I achieved it:

  1. Register page in /includes/class-user.php :
    ap_register_user_page( 'badges', __( 'Badges', 'anspress-question-answer' ), array( __CLASS__, 'badges_page' ) );

     

  2. Setup output for page in /includes/class-user.php :
    public static function badges_page() {
    include ap_get_theme_location( 'user/badges.php' );
    }

     

  3. Create corresponding page in /theme/default/user/ : badges.php

 

By doing like that and using the proper code in badges.php, I have been able to achieve adding a tab with corresponding content and everything seems to be fine. So for now, I would like to know:

  • is it the right way to do it?
  • since I don’t know my way around php, I directly edited AP files (in a staging environment of course) so that I could learn from and copy/paste relevant pieces of code. Is there a way to achieve the same thing (register page + setup output) directly from my theme’s functions.php?
  • As an alternative to editing functions.php, is it possible to create a folder called anspress inside active parent theme folder and copy the modified class-user.php file to override original one?

 

Ok, that’s it for now. Let’s go step by step. 😀

Thanks in advance!

Fred

selected answer
1

Hello Fred,

You are doing it right but but you should not edit core files. Instead add this filters to you theme’s function.php

https://gist.github.com/rahularyan/347bb9a2c9cc11a788d739cabf8d5376

You can also wrap above code inside a class but if you are not much familiar with PHP leave it as it is else it will confuse you.

commented on answer

Thanks a lot Rahul! This is truly useful. I’m gonna try this and play around with it. 😉

Hi Rahul! I tried it out and everything worked like a charm, thanks! I have 2 related questions.
Question 1 : by looking at some other piece of code you provided, I guess that the last 2 values “true” and “false” in the ap_register_user_page array are about “show in menu” and “public”, correct? Is there a list fo all possible array values for registering a page in AnsPress? Or maybe the example you just gave me list them all already? I think it would be nice to add that to the documentation. 😉

Last two parameters in ap_register_user_page are
$show_in_menu = true, $public = true

Like if you want to hide a page link from menu then pass false, for example “logout”.
And $public for if you want a use page to be accessible by public user like ” about page”.

Thank you very much for the follow up Rahul! Ok, it’s what I guessed then. 😉

Is there any other parameters for the ap_register_user_page (other than the ones we already used)?

0

Question 2:

By using the piece of code you provided, I’m able to register my new page and it is automatically added to the user menu, great! How can I decide in which position it will appear in the menu and assign an icon?

I tried to add a link using the method previously quoted hoping it would override the automatically added one:

function my_ap_user_link($links){
    
    $links['badges'] = array( 'slug' => 'badges', 'title' => __( 'Badges', 'anspress-question-answer' ), 'link' => 'badges', 'order' => 100, 'show_in_menu' => false, 'public' => true, 'class' => 'apicon-chevron-right');

	return $links;
}
add_filter( 'ap_user_menu', 'my_ap_user_link' );

It’s actually working but, I’m not sure it’s the right way to do it or not, and I don’t know which value to put for the “link” element.

So is it the right way to do it or can I actually specify order and icon class in my ap_register_user_page function? If it is the right way to do it, what should be the value for the “link” element?

This should be my last question on that matter. ^^; Thanks in advance. 😀

Fred

PS: sorry to post it as an answer but I need to add code to it. Maybe I should open a new thread but it is closely related to my first inquiry. If it’s still better to separate every questions, I’ll do it from next one. 😉

commented on answer

Ok! I found the way to specify the link element! In my case, it should be: ‘link’ => ap_user_link( ap_get_displayed_user_id(), ‘badges’ ). Still don’t know if “overriding” the menu item with my_ap_user_link functions is the best way but at least it’s working. 😀

@Fred you don’t need to pass link arg, as it will be automatically added after filter

Thanks again Rahul, I really appreciate the time you take sorting things out for me! Actually, not passing the link argument is the first thing I tried and it’s not working. For example, if I use:

$links[‘badges’] = array( ‘slug’ => ‘badges’, ‘title’ => __(‘Badges’), ‘order’ => 100, ‘show_in_menu’ => true, ‘public’ => true, ‘class’ => ‘apicon-reputation’);

it helps me specify order and override icon but then the link always points to currently displayed user page. (If I’m checking the “About” page, then the “Badges” tab link is pointing to /user/about/.

Anyway, if using the $links[‘badges’] array to override order, icon,… is ok on a code level, it’s fine with me for now! 😀