Using separate .php file for functions - how to run on site?

admin2025-06-07  24

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?

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?

Share Improve this question edited Oct 17, 2018 at 20:03 BruceWayne asked Oct 17, 2018 at 19:19 BruceWayneBruceWayne 1519 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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' );

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.

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');

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749253119a317636.html

最新回复(0)