2017 © Pedro Peláez
 

library file-locator

Return a file path or file contents, checking in child theme, then theme, then as an absolute file path.

image

calderawp/file-locator

Return a file path or file contents, checking in child theme, then theme, then as an absolute file path.

  • Saturday, March 14, 2015
  • by Shelob9
  • Repository
  • 2 Watchers
  • 2 Stars
  • 51 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CalderaWP File Loader

Return a file path or file contents, checking in child theme, then theme, then as an absolute file path., (*1)

Usage

    //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 );

Can't you do this in WordPress Already?

@see https://core.trac.wordpress.org/ticket/13239, (*2)

Copyright 2015 CalderaWP LLC & Josh Pollock., (*3)

Licensed under the terms of the GNU General Public License version 2 or later. Please share with your neighbor., (*4)

The actual file locating code is basically copypasta from https://github.com/pods-framework/pods/blob/master/classes/PodsView (c) Pods Foundation. Much GPL, very thanks., (*5)

The Versions

14/03 2015

dev-master

9999999-dev

Return a file path or file contents, checking in child theme, then theme, then as an absolute file path.

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

wordpress locate_template

27/02 2015

1.0.0

1.0.0.0

Return a file path or file contents, checking in child theme, then theme, then as an absolute file path.

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

wordpress locate_template