Will Squire Will Squire - 1 year ago 68
PHP Question

How do you add a lib namespace to Wordpress roots sage?

Following the documentation written here, I added a file to lib directory containing:


namespace Theme\URIs;

* Returns theme images directory uri
function get_images_directory_uri() {
return get_template_directory_uri() . '/assets/images';

Which I then tried calling from a template file with:

<?php print Theme\URIs\get_images_directory_uri(); ?>

Calling this however, returned a
Fatal error: Call to undefined function Theme\URIs\get_images_directory_uri()

Answer Source

The solution is to add the file to the $sage_includes array in functions.php, so it looks like this:

$sage_includes = [
  'lib/assets.php',  // Scripts and stylesheets
  'lib/extras.php',  // Custom functions
  'lib/setup.php',   // Theme setup
  'lib/titles.php',  // Page titles
  'lib/wrapper.php',  // Theme wrapper class
  'lib/uris.php' // Project URIs

This imports the file to be used in the theme.

The present documentation does not explain this. (I assumed it simply imported all files in the libs directory...)