bbp_locate_template( string|array $template_names, bool $load = false, bool $require_once = true )

Retrieve the name of the highest priority template file that exists.

Description

Searches in the child theme before parent theme so that themes which inherit from a parent theme can just overload one file. If the template is not found in either of those, it looks in the theme-compat folder last.

Parameters

$template_names

(Required) Template file(s) to search for, in order.

$load

(Optional) If true the template file will be loaded if it is found.

Default value: false

$require_once

(Optional) Whether to require_once or require. Has no effect if $load is false.

Default value: true

Return

(string) The template filename if one is located.

Source

File: bp-forums/core/template-functions.php

function bbp_locate_template( $template_names, $load = false, $require_once = true ) {

	// No file found yet
	$located            = false;
	$template_locations = bbp_get_template_stack();

	// Try to find a template file
	foreach ( (array) $template_names as $template_name ) {

		// Continue if template is empty
		if ( empty( $template_name ) ) {
			continue;
		}

		// Trim off any slashes from the template name
		$template_name  = ltrim( $template_name, '/' );

		// Loop through template stack
		foreach ( (array) $template_locations as $template_location ) {

			// Continue if $template_location is empty
			if ( empty( $template_location ) ) {
				continue;
			}

			// Check child theme first
			if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
				$located = trailingslashit( $template_location ) . $template_name;
				break 2;
			}
		}
	}

	/**
	 * This action exists only to follow the standard Forums coding convention,
	 * and should not be used to short-circuit any part of the template locator.
	 *
	 * If you want to override a specific template part, please either filter
	 * 'bbp_get_template_part' or add a new location to the template stack.
	 */
	do_action( 'bbp_locate_template', $located, $template_name, $template_names, $template_locations, $load, $require_once );

	// Maybe load the template if one was located
	if ( ( true === $load ) && !empty( $located ) ) {
		load_template( $located, $require_once );
	}

	return $located;
}

Changelog

Changelog
Version Description
bbPress (r3618) 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.