1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Docs] Adding Content to User Profiles

Discussion in 'Developing Plugins' started by Nick, Feb 28, 2010.

  1. Nick

    Nick Well-Known Member

    User profiles in the Users plugin can be extended in three ways:

    1. Add a link to the profile menu
    2. Add content to the body of the main profile page
    3. Add additional pages

    These instructions explain each of the above.

    1. Add a link to the profile menu

    The profile menu has its own template in the Users plugin templates folder. In it, there are two plugin hooks:

    PHP:
    <?php $h->pluginHook('profile_navigation'); ?>
    PHP:
    <?php $h->pluginHook('profile_navigation_restricted'); ?>
    Links added to the first hook can be seen by everybody. Links added to the "restricted" hook can only be seen by the profile owner and anyone with "access_admin" permissions.

    Any other rules for who can and can't see the links need be added within your functions.

    Example function:

    PHP:
        /**
         * Add a link to the profile menu
         */
        
    public function profile_navigation($h)
        {
            echo 
    "<li><a href='http://www.example.com'>Example link</a></li>\n";
        }
    2. Add content to the body of the main profile page

    The Users plugin has a users_profile.php template with this hook in it:
    PHP:
    <?php $h->pluginHook('profile'); ?>
    Here's a basic function which displays "Hello World" or includes another template:

    PHP:
        /**
         * Display "Hello World" on the profile page
         */
        
    public function profile($h)
        {
            echo 
    "Hello World";

            
    // or...

            
    $h->displayTemplate('another_template'); // name of a template in your plugin's templates folder
        
    }
    Tip: The Activity plugin is one example of a plugin that adds content to the profile.

    3. Add additional pages

    One of Hotaru's best features is its ability for plugins to extend profiles with extra pages. Here's how to do it:

    First, you'll need these hooks: theme_index_top, profile_navigation, breadcrumbs, theme_index_main

    Most of the work is done in the theme_index_top function:
    PHP:
        /**
         * Determine page and get user details
         */
        
    public function theme_index_top($h)
        {
            if (
    $h->pageName != 'mypage') { return false; } // not "mypage" so get out of here!

            // get user name from the url. If not present, use the current user's name
            
    $user $h->cage->get->testUsername('user');
            if (!
    $user) { $user $h->currentUser->name; }

            
    // set the page title
            
    $h->pageTitle $h->lang['mypage_title'] . "[delimiter]" $user;

            
    // set the page types
            
    $h->pageType 'user';  // use this to hide the posts filter bar
            
    $h->subPage 'user';    // pageName is 'mypage', subPage is 'user'

            // create a user object and fill it with user info (user being viewed)
            
    $h->vars['user'] = new UserAuth();
            
    $h->vars['user']->getUserBasic($h0$user);
        }
    Add a link in the profile menu:
    PHP:
        /**
         * Profile menu link to "mypage"
         */
        
    public function profile_navigation($h)
        {
             echo 
    "<li><a href='" $h->url(array('page'=>'mypage''user'=>$h->vars['user']->name)) . "'>" $h->lang['mypage_link_text'] . "</a></li>\n";
        }
    The default breadcrumbs add the page title after "Home" so you might have Home >> My Page << Nick. That's not what we want, so to change it to Home >> Nick >> My Page, you need this:
    PHP:
        /**
         * Breadcrumbs for "mypage"
         */
        
    public function breadcrumbs($h)
        {
            if (
    $h->pageName != 'mypage') { return false; } // not "mypage" so get out of here!
            
            
    return "<a href='" $h->url(array('user'=>$user)) . "'>" $user "</a> &raquo; " $h->lang['mypage_link_text'];
        }
    Display your page:

    PHP:
        /**
         * Display "mypage"
         */
        
    public function theme_index_main($h)
        {
            if (
    $h->pageName != 'mypage') { return false; } // not "mypage" so get out of here!

            
    $h->displayTemplate('mypage_template');
            return 
    true;
        }
    Tip: A full-on example of user pages is demonstrated by the Messaging plugin. A much easier example is in the Hello Universe plugin.
     
    ties likes this.

Share This Page