locate_template()

 

File: wp-includes/template.php

function locate_template( $template_names, $load = false, $require_once = true ) {
	$located = '';
	foreach ( (array) $template_names as $template_name ) {
		if ( ! $template_name ) {
			continue;
		}
		if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
			$located = STYLESHEETPATH . '/' . $template_name;
			break;
		} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
			$located = TEMPLATEPATH . '/' . $template_name;
			break;
		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
			break;
		}
	}

	if ( $load && '' != $located ) {
		load_template( $located, $require_once );
	}

	return $located;
}

$template = locate_template( $template_filename_from_unsanitized_user_input );

// Only allow templates that are in the active theme directory, parent theme
// directory, or the /wp-includes/theme-compat/ directory (prevent directory 
// traversal attacks).
$template_in_theme_or_parent_theme_or_compat = (
	// Template is in current theme folder.
	0 === strpos( realpath( $template ), realpath( STYLESHEETPATH ) ) ||
	// Template is in current or parent theme folder.
	0 === strpos( realpath( $template ), realpath( TEMPLATEPATH ) ) ||
	// Template is in theme-compat folder.
	0 === strpos( realpath( $template ), realpath( ABSPATH . WPINC . '/theme-compat/' ) )
);

if ( $template_in_theme_or_parent_theme_or_compat ) {
	require_once( $template );
}

参数:locate_template( string|array $template_names, bool $load = false, bool $require_once = true )

在templatePath和wp includes/theme compat之前在stylesheetpath中搜索,以便继承自父主题的主题只会重载一个文件。

Template file(s) to search for, in order.If true the template file will be loaded if it is found.Whether to require_once or require. Has no effect if $load is false.

(string)模板文件名(如果有)。

$template_names

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

$load

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

Default value: false

$require_once

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

Default value: true

{acontent3}

  1. Note that locate_template() does not prevent directory traversal attacks, so if you’re passing a user-provided template name to the function, be sure to verify that it’s from one of the three appropriate locations (active theme directory, parent theme directory, or /wp-includes/theme-compat/ directory).

    Example:

    $template = locate_template( $template_filename_from_unsanitized_user_input );
    
    // Only allow templates that are in the active theme directory, parent theme
    // directory, or the /wp-includes/theme-compat/ directory (prevent directory 
    // traversal attacks).
    $template_in_theme_or_parent_theme_or_compat = (
    	// Template is in current theme folder.
    	0 === strpos( realpath( $template ), realpath( STYLESHEETPATH ) ) ||
    	// Template is in current or parent theme folder.
    	0 === strpos( realpath( $template ), realpath( TEMPLATEPATH ) ) ||
    	// Template is in theme-compat folder.
    	0 === strpos( realpath( $template ), realpath( ABSPATH . WPINC . '/theme-compat/' ) )
    );
    
    if ( $template_in_theme_or_parent_theme_or_compat ) {
    	require_once( $template );
    }
  2. Example
    Load a specific template part based on the current pagename.

    if (locate_template('content-' . $pageName . '.php') != '') {
    	// yep, load the page template
    	get_template_part('content', $pageName);
    } else {
    	// nope, load the content
    	the_content();
    }
    
    

You must log in before being able to contribute a note or feedback.

57.89% (11) 次Baidu访问
10.53% (2) 次Google访问
26.32% (5) 次Sogou访问
5.26% (1) 次sm访问
  • 蜘蛛:Baidu,11次,更新时间:2019-05-22 13:40:01
  • 蜘蛛:Google,2次,更新时间:2019-04-26 17:09:03
  • 蜘蛛:Sogou,5次,更新时间:2019-05-18 19:05:24
  • 蜘蛛:sm,1次,更新时间:2019-05-12 16:13:14

参与评论