bp_core_load_template( array $templates )

Load a specific template file with fallback support.

Description

Example: bp_core_load_template( ‘members/index’ ); Loads: wp-content/themes/[activated_theme]/members/index.php

Parameters

$templates

(Required) Array of templates to attempt to load.

Source

File: bp-core/bp-core-catchuri.php

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
function bp_core_load_template( $templates ) {
    global $wp_query;
 
    // Reset the post.
    bp_theme_compat_reset_post( array(
        'ID'          => 0,
        'is_404'      => true,
        'post_status' => 'publish',
    ) );
 
    // Set theme compat to false since the reset post function automatically sets
    // theme compat to true.
    bp_set_theme_compat_active( false );
 
    // Fetch each template and add the php suffix.
    $filtered_templates = array();
    foreach ( (array) $templates as $template ) {
        $filtered_templates[] = $template . '.php';
    }
 
    // Only perform template lookup for bp-default themes.
    if ( ! bp_use_theme_compat_with_current_theme() ) {
        $template = locate_template( (array) $filtered_templates, false );
 
    // Theme compat doesn't require a template lookup.
    } else {
        $template = '';
    }
 
    /**
     * Filters the template locations.
     *
     * Allows plugins to alter where the template files are located.
     *
     * @since BuddyPress 1.1.0
     *
     * @param string $template           Located template path.
     * @param array  $filtered_templates Array of templates to attempt to load.
     */
    $located_template = apply_filters( 'bp_located_template', $template, $filtered_templates );
 
    /*
     * If current page is an embed, wipe out bp-default template.
     *
     * Wiping out the bp-default template allows WordPress to use their special
     * embed template, which is what we want.
     */
    if ( function_exists( 'is_embed' ) && is_embed() ) {
        $located_template = '';
    }
 
    if ( !empty( $located_template ) ) {
        // Template was located, lets set this as a valid page and not a 404.
        status_header( 200 );
        $wp_query->is_page     = true;
        $wp_query->is_singular = true;
        $wp_query->is_404      = false;
 
        /**
         * Fires before the loading of a located template file.
         *
         * @since BuddyPress 1.6.0
         *
         * @param string $located_template Template found to be loaded.
         */
        do_action( 'bp_core_pre_load_template', $located_template );
 
        /**
         * Filters the selected template right before loading.
         *
         * @since BuddyPress 1.1.0
         *
         * @param string $located_template Template found to be loaded.
         */
        load_template( apply_filters( 'bp_load_template', $located_template ) );
 
        /**
         * Fires after the loading of a located template file.
         *
         * @since BuddyPress 1.6.0
         *
         * @param string $located_template Template found that was loaded.
         */
        do_action( 'bp_core_post_load_template', $located_template );
 
        // Kill any other output after this.
        exit();
 
    // No template found, so setup theme compatibility.
    // @todo Some other 404 handling if theme compat doesn't kick in.
    } else {
 
        // We know where we are, so reset important $wp_query bits here early.
        // The rest will be done by bp_theme_compat_reset_post() later.
        if ( is_buddypress() ) {
            status_header( 200 );
            $wp_query->is_page     = true;
            $wp_query->is_singular = true;
            $wp_query->is_404      = false;
        }
 
        /**
         * Fires if there are no found templates to load and theme compat is needed.
         *
         * @since BuddyPress 1.7.0
         */
        do_action( 'bp_setup_theme_compat' );
    }
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.