calderawp/file-locator

返回文件路径或文件内容,首先检查子主题,然后检查主题,最后以绝对文件路径的方式。

1.0.0 2015-02-27 03:37 UTC

This package is auto-updated.

Last update: 2024-09-08 07:11:46 UTC


README

返回文件路径或文件内容,首先检查子主题,然后检查主题,最后以绝对文件路径的方式。

用法

    //Load a file called "food.html" from current themes "templates" folder
    $file = calderawp_file_locator( 'templates/food.html' );
    if ( is_string( $file ) ) {
        echo $file;
    }
    
    //load a php file from your plugin, if a file "noms.php" of same name is not in template
    $file = calderawp_file_locator( 'noms.php' );
    
    //By default only file extensions php|html|htm are allowed.
    //TO override this use the "calderawp_file_locator_allow_extensions" filter before calling it, like this:
    add_filter( 'calderawp_file_locator_allow_extensions', function( $allowed, $context ) {
    	if ( 'slug_json_loader' === $context ) {
    		$allowed = array( 'json' );
    	}
    
    	return $allowed;
    
    }, 10, 2 );
    
    return calderawp_file_locator( 'hats.json', 'slug_json_loader' );
    
    //Use a special single-{$post-type}.php, in this case single-hat.php from plugin if theme/child theme doesn't have that file.
add_filter( 'template_includes', function( $template ) {
	if ( 'single-hat.php' == $template && ! is_file( calderawp_file_locator( $template ) ) ) {
		$template = calderawp_file_locator( trailingslashit( plugin_dir_path( __FILE__ ) ) . $template );
	}

	return $template;

}, 10 );
    

WordPress 已经可以做到这一点了吗?

@see https://core.trac.wordpress.org/ticket/13239

许可,版权等。

版权所有 2015 CalderaWP LLC & Josh Pollock

根据GNU 通用公共许可证版本 2 或更高版本许可。请与您的邻居分享。

实际的文件定位代码基本上是从 https://github.com/pods-framework/pods/blob/master/classes/PodsView (c) Pods 基金会复制粘贴过来的。很多 GPL,非常感谢。