2

I'm creating a theme based on Sage WordPress starter theme and created a new namespace to store some of my functions (Roots\Sage\Color_Scheme).

When I try to call a function from that namespace, I get the error message

Call to undefined function Roots\Sage\Color_Scheme\custom_header_and_background() in C:\xampp\htdocs\wordpress\wp-content\themes\soaring\lib\setup.php on line 19

Since I have "use Roots\Sage\Color_Scheme" declared and the function is definitely in the color_scheme.php file, I'm not sure why it's not actually recognizing the function. Note that color_scheme.php is a namespaced collection of functions but contains no declared class.

Setup.php

namespace Roots\Sage\Setup;

use Roots\Sage\Assets;
use Roots\Sage\Color_Scheme;

/**
 * Theme setup
 */
function setup() {
  // Enable features from Soil when plugin is activated
  // https://roots.io/plugins/soil/
  add_theme_support('soil-clean-up');
  add_theme_support('soil-nav-walker');
  add_theme_support('soil-nice-search');
  add_theme_support('soil-jquery-cdn');
  add_theme_support('soil-relative-urls');
  Color_Scheme\custom_header_and_background();
  ...

Here are the relevant portions of Color_Scheme.php (located in the same directory)

<?php
namespace Roots\Sage\Color_Scheme;

/**
 * Adds theme support for custom background and custom header
 * 
 * Include default values for several settings.
 */
function custom_header_and_background() {
    $color_scheme             = get_color_scheme();
    $default_background_color = trim( $color_scheme[0], '#' );
    $default_text_color       = trim( $color_scheme[1], '#' );

/**
 * Filter the arguments used when adding 'custom-background' support in Soaring.
 *
 * @since Soaring 1.0
 *
 * @param array $args {
 *     An array of custom-background support arguments.
 *
 *     @type string $default-color Default color of the background.
 * }
 */
add_theme_support( 'custom-background', apply_filters( 'soaring_custom_background_args', array(
        'default-color' => $default_background_color,
)));

// HEADER
$custom_header_args = array(
        'width'         => 300,
        'height'        => 120,
        'flex-width'    => true,
        'flex-height'   => true,
        'default-image' => get_template_directory_uri() . '/images/soaring-logo.png',
);
add_theme_support( 'custom-header', $custom_header_args );
}
4
  • Does your Setup.php contain an include to Color_Scheme.php? Commented Dec 23, 2015 at 1:13
  • No it doesn't. color_scheme.php is in the directory but I never wrote a line to include it. Is that necessary even with namespace and the same directory? Commented Dec 23, 2015 at 4:48
  • It certainly seems to work and recognize the function if I do an "include 'color_scheme.php'." That's just what I need to do if I want to access the functions from another file at all then? Commented Dec 23, 2015 at 5:01
  • If you don't want to require_once in your Setup.php file. You can also include your color_scheme.php file using Sage's $sage_includes = [ ] array found in functions.php. Commented Jun 1, 2016 at 16:39

1 Answer 1

3

The name of the undefined function is:

  • Roots\Sage\Color_Scheme\custom_header_and_background()

The name of the defined function is:

  • Roots\Sage\Color_Scheme\custom_header_and_background()

As it shows, the name itself is not the problem, just the definition is missing. That is exactly what you also comment:

color_scheme.php is in the directory but I never wrote a line to include it. Is that necessary even with namespace and the same directory?

Yes it is. PHP will not on it's own load the file. You need to do that.

PHP only has a mechanism to autoload classes, but even then, you need to register an autoloader for it.

For functions no autoloading exist yet (there are some discussions about it, e.g. PHP RFC Function Autoloading), so you need to include the function definition before use. The require_once directive and the __DIR__ constant might then be helpful in your case:

...
require_once __DIR__ . '/color_scheme.php';
Color_Scheme\custom_header_and_background();
...

That should allow you to get it to work. I then strongly suggest to move the require line up to the top of the file to have it less conditional and you can better see which dependencies a file has.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.