Created
November 5, 2016 07:18
-
-
Save splitinfinities/5aefc5304c5173fb41760101630cad10 to your computer and use it in GitHub Desktop.
Simple WordPress Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use SPLIT\Wrapper; | |
?> | |
<head> | |
<?php wp_head(); ?> | |
</head> | |
<body> | |
<?php include Wrapper\template_path(); ?> | |
<?php wp_footer(); ?> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// use include() to add this file in your functions.php, based off an old version of Sage. | |
namespace SPLIT\Wrapper; | |
/** | |
* Theme wrapper | |
* | |
* @link https://roots.io/sage/docs/theme-wrapper/ | |
* @link http://scribu.net/wordpress/theme-wrappers.html | |
*/ | |
function template_path() { | |
return SPLIT_Wrapping::$main_partial; | |
} | |
function sidebar_path() { | |
return new SPLIT_Wrapping('partials/sidebar.php'); | |
} | |
class SPLIT_Wrapping { | |
// Stores the full path to the main partial file | |
public static $main_partial; | |
// Basename of partial file | |
public $slug; | |
// Array of partials | |
public $partials; | |
// Stores the base name of the partial file; e.g. 'page' for 'page.php' etc. | |
public static $base; | |
public function __construct($partial = 'base.php') { | |
$this->slug = basename($partial, '.php'); | |
$this->partials = [$partial]; | |
if (self::$base) { | |
$str = substr($partial, 0, -4); | |
array_unshift($this->partials, sprintf($str . '-%s.php', self::$base)); | |
} | |
} | |
public function __toString() { | |
$this->partials = apply_filters('SPLIT/wrap_' . $this->slug, $this->partials); | |
return locate_template($this->partials); | |
} | |
public static function wrap($main) { | |
// Check for other filters returning null | |
if (!is_string($main)) { | |
return $main; | |
} | |
self::$main_partial = $main; | |
self::$base = basename(self::$main_partial, '.php'); | |
if (self::$base === 'index') { | |
self::$base = false; | |
} | |
return new SPLIT_Wrapping(); | |
} | |
} | |
add_filter('template_include', [__NAMESPACE__ . '\\SPLIT_Wrapping', 'wrap'], 109); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment