Skip to content

Instantly share code, notes, and snippets.

@elliottmangham
Last active December 3, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliottmangham/7c444faf8d3d769a812ea83d4a646ef3 to your computer and use it in GitHub Desktop.
Save elliottmangham/7c444faf8d3d769a812ea83d4a646ef3 to your computer and use it in GitHub Desktop.
WordPress (PHP) / Utilities
/************************************
* Include component file.
* @param string $sFilePath The component to include
* @param array $aScopeVariables An array of variables to make available in the component
* @param boolean $bReturn Return the markup instead off outputting it?
************************************/
function get_component( $sFilePath, $aScopeVariables = [], $bReturn = false ) {
ob_start();
extract( $aScopeVariables, EXTR_OVERWRITE );
$sTemplateSlug = sprintf( '/templates/_components/%s.php', $sFilePath );
$sTemplateLocation = locate_template($sTemplateSlug);
if ( ! is_readable( $sTemplateLocation ) ) {
throw new \Exception( sprintf( 'Missing component: %s', $sFilePath ) );
}
include $sTemplateLocation;
$sComponent = ob_get_clean();
if ( $bReturn === true) return $sComponent;
echo $sComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment