Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/17/2016 03:30:29 PM (9 years ago)
Author:
imath
Message:

Introduce the support for a custom home page template for single members profiles.

  • Themes can now include a front.php template to customize the home page of members.
  • Introduce a template hierarchy for this template so that it is possible to have different front pages according to the ID, the nicename or the member type of the displayed user.

Props hnla, dcavins, DJPaul, imath.

Fixes #6769

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-template.php

    r10745 r10774  
    13271327
    13281328/**
     1329 * Output the contents of the current user's home page.
     1330 *
     1331 * @since 2.6.0
     1332 */
     1333function bp_displayed_user_front_template_part() {
     1334    $located = bp_displayed_user_get_front_template();
     1335
     1336    if ( false !== $located ) {
     1337        $slug = str_replace( '.php', '', $located );
     1338        $name = null;
     1339
     1340        /**
     1341         * Let plugins adding an action to bp_get_template_part get it from here
     1342         *
     1343         * @param string $slug Template part slug requested.
     1344         * @param string $name Template part name requested.
     1345         */
     1346        do_action( 'get_template_part_' . $slug, $slug, $name );
     1347
     1348        load_template( $located, true );
     1349    }
     1350
     1351    return $located;
     1352}
     1353
     1354/**
     1355 * Locate a custom user front template if it exists.
     1356 *
     1357 * @since 2.6.0
     1358 *
     1359 * @param  object|null $displayed_user Optional. Falls back to current user if not passed.
     1360 * @return string|bool                 Path to front template on success; boolean false on failure.
     1361 */
     1362function bp_displayed_user_get_front_template( $displayed_user = null ) {
     1363    if ( ! is_object( $displayed_user ) || empty( $displayed_user->id ) ) {
     1364        $displayed_user = bp_get_displayed_user();
     1365    }
     1366
     1367    if ( ! isset( $displayed_user->id ) ) {
     1368        return false;
     1369    }
     1370
     1371    if ( isset( $displayed_user->front_template ) ) {
     1372        return $displayed_user->front_template;
     1373    }
     1374
     1375    // Init the hierarchy
     1376    $template_names = array(
     1377        'members/single/front-id-' . sanitize_file_name( $displayed_user->id ) . '.php',
     1378        'members/single/front-nicename-' . sanitize_file_name( $displayed_user->userdata->user_nicename ) . '.php',
     1379    );
     1380
     1381    /**
     1382     * Check for member types and add it to the hierarchy
     1383     *
     1384     * Make sure to register your member
     1385     * type using the hook 'bp_register_member_types'
     1386     */
     1387    if ( bp_get_member_types() ) {
     1388        $displayed_user_member_type = bp_get_member_type( $displayed_user->id );
     1389        if ( ! $displayed_user_member_type ) {
     1390            $displayed_user_member_type = 'none';
     1391        }
     1392
     1393        $template_names[] = 'members/single/front-member-type-' . sanitize_file_name( $displayed_user_member_type )   . '.php';
     1394    }
     1395
     1396    // Add The generic template to the end of the hierarchy
     1397    $template_names[] = 'members/single/front.php';
     1398
     1399    /**
     1400     * Filters the hierarchy of user front templates corresponding to a specific user.
     1401     *
     1402     * @since 2.6.0
     1403     *
     1404     * @param array  $template_names Array of template paths.
     1405     */
     1406    return bp_locate_template( apply_filters( 'bp_displayed_user_get_front_template', $template_names ), false, true );
     1407}
     1408
     1409/**
     1410 * Check if the displayed user has a custom front template.
     1411 *
     1412 * @since 2.6.0
     1413 */
     1414function bp_displayed_user_has_front_template() {
     1415    $displayed_user = bp_get_displayed_user();
     1416
     1417    return ! empty( $displayed_user->front_template );
     1418}
     1419
     1420/**
    13291421 * Render the navigation markup for the displayed user.
    13301422 *
Note: See TracChangeset for help on using the changeset viewer.