1

Instead of bogging down my child theme's functions.php file, I would like to have a separate .php file that has various functions, that I can call within functions.php (and in other files).

I have created my-custom-functions.php within my child theme, where the functions.php lives.

Here's the folder structure:

wp-content
    themes
        grow-minimal-child
            functions.php
            my-custom-functions.php

Code for my-custom-functions.php:

<?php

function js_log($msg){
  return "<script type='text/javascript'>alert('$msg');</script>";
}

echo js_log("Hello!");

And the first few lines of functions.php:

<?php
include_once(get_theme_roots() . '/grow-minimal-child/rs-custom-functions.php');

But, when I load any page, I get this error:

Warning: include_once(/themes/grow-minimal-child/my-custom-functions.php): failed to open stream: No such file or directory in /opt/bitnami/apps/wordpress/htdocs/wp-content/themes/grow-minimal-child/functions.php on line 2

What do I need to fix, it looks like the include_once is looking for functions.php?

2

get_theme_roots() and get_theme_root() aren't really appropriate functions for getting the path to a file from a theme.

I recommend you use get_theme_file_path() instead:

include_once get_theme_file_path( 'grow-minimal-child/rs-custom-functions.php' );
  • Thanks! I was thinking there was a better function. I'll check the documentation as to why; I appreciate your help! – BruceWayne Oct 18 '18 at 14:38
0

The error was using get_theme_roots(). This returns a relative path.

get_theme_root() returns /opt/bitnami/apps/wordpress/htdocs/wp-content/themes get_theme_roots() returns /themes

By switching that to get_theme_root() (note singular), it was able to correctly locate the custom .php file.

0

I think you could use get_stylesheet_directory() as you're using a child theme.

Something like, include_once( get_stylesheet_directory() . '/rs-custom-functions.php');

  • Looks like that returns the same path as get_theme_roots() (at least in this instance). Thanks! – BruceWayne Oct 17 '18 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.