admin管理员组文章数量:1130349
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?
3 Answers
Reset to default 2get_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');
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?
3 Answers
Reset to default 2get_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');
本文标签: Using separate php file for functionshow to run on site
版权声明:本文标题:Using separate .php file for functions - how to run on site? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749253095a2340018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论