$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Using separate .php file for functions - how to run on site?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far