admin管理员组文章数量:1124403
I have a custom php file in wp-content/themes/xxxx/modules/custom.php and i want to call function from Functions.php. Right now all my functions (same function) everywhere in different files, because still cannot solve this problem.
I want to create a function in Functions.php and call this function from everywhere in the website. This function will called from another php files (Ajax will not call this function). When I include or require any file (wp-load, wp-blog-header, functions.php are not imported)
I read many forums and tried many ways like
1.
define('WP_USE_THEMES', true);
require( '/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php' );
2.
include (/opt/bitnami/apps/wordpress/htdocs/wp-content/themes/xxx/functions.php);
3.
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load');
require(/opt/bitnami/apps/wordpress/htdocs/wp-load');
etc. I tried many ways but all examples not working.
Is there any way to declare a function in Funtions.php and use everywhere without any problem?.
I have a custom php file in wp-content/themes/xxxx/modules/custom.php and i want to call function from Functions.php. Right now all my functions (same function) everywhere in different files, because still cannot solve this problem.
I want to create a function in Functions.php and call this function from everywhere in the website. This function will called from another php files (Ajax will not call this function). When I include or require any file (wp-load, wp-blog-header, functions.php are not imported)
I read many forums and tried many ways like
1.
define('WP_USE_THEMES', true);
require( '/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php' );
2.
include (/opt/bitnami/apps/wordpress/htdocs/wp-content/themes/xxx/functions.php);
3.
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load');
require(/opt/bitnami/apps/wordpress/htdocs/wp-load');
etc. I tried many ways but all examples not working.
Is there any way to declare a function in Funtions.php and use everywhere without any problem?.
Share Improve this question edited Nov 27, 2020 at 11:39 Jesse Nickles 7357 silver badges19 bronze badges asked Apr 25, 2019 at 22:02 BayanaaBayanaa 331 silver badge6 bronze badges 2- 2 How do you use this custom.php file? Do you access it directly? If so, what is the reason? (Direct access to php files of your theme isn’t the best idea from security point of view) – Krzysiek Dróżdż Commented Apr 26, 2019 at 4:41
- What Krzysiek says plus what happens as soon as you switch your theme? Better create a plugin for that or have a look at wordpress.org/plugins/my-custom-functions – leymannx Commented Apr 26, 2019 at 10:07
5 Answers
Reset to default 2It's worth noting that PHP namespace issues can contribute to this problem. This can catch people because namespaces have been kept out of Wordpress core codebase until recently.
For example one of your code files may have a Namespace declaration around the top. If that is the case you will have to prefix any references to the functions with the correct namespace.
In Wordpress in particular if there is a mismatch it will not find the function unless it is namespaced correctly.
In themes such as Sage:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘myplugin_settings’ not found or invalid function name
You would need to indicate the namespace this way. If the namespace is already called above on this file, you need to add the following:
add_action( 'init' , __NAMESPACE__ . '\\myplugin_settings' );
If your file is not in the namespace you will have to add it manually. Note that double-backslashes are needed due to character escaping.
More about namespaces in PHP:
- PHP manual TOC for namespaces.
- Rationale for namespaces.
- Basics of namespaces.
- Complete guide to namespaces.
(Adding this answer because I landed here and namespaces were my snag - I didn't realize the add_action namespace issue.)
I tried to find a solution for last 7 days and still cannot find a solution.
I am going to post my code like how trying to import wp-blog-header.php or wp-load.php
Here is a page code :
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (file_exists('/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php')) {
error_log("The file '/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php' exists");
} else {
error_log("The file '/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php' does not exist");
}
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
error_log(print_r(get_header(), true));die();
here is a apache error log :
[Mon Apr 29 08:58:29.362243 2019] [proxy_fcgi:error] [pid 9264:tid 139719891371776] [client xxx] AH01071: Got error 'PHP message: The file '/opt/bitnami/apps/wordpress/htdocs/wp-blog-header.php' exists\n', referer: https://formaspace.com/contact/
You see that require_once section didn't answer it?, because it failed at wp-load page. Also if I remove that require_once line it shows me error like get_header call to undefined function
get_header is just a example. I already tried to call functions.php funtions
Some of this relates to the default load sequence of WordPress Core and plugin files. Things can get confusing for developers on complex sites that have a parent theme, child theme, theme framework or options framework, theme functions.php
file, MU plugins, and normal plugins...
In short, perhaps the more reliable solution is creating a MU plugin for certain critical functions, to ensure that it is loading before other items, and can't be disabled, etc.
We use the MU approach in SlickStack to load critical features (e.g. our Clear Caches plugin), and we also include /wp-content/functions.php
as a special file inside wp-config.php
so that functions load higher up in the load sequence, which allows developers to better modify certain site-wide features and functions. This also helps to prevent custom functions from disappearing when a client (etc) changes to a new WordPress theme, etc.
Ref: https://github.com/littlebizzy/slickstack/blob/master/modules/wordpress/wp-config-production.txt
/** rather than editing your wp-config.php file simply create a custom functions file */
/** if SlickStack detects this file exists it will load included constants etc */
$custom_functions = '/var/www/html/wp-content/custom-functions.php';
is_file($custom_functions) AND include $custom_functions;
This also allows web hosts or agencies to "lock" their wp-config.php
as read-only while still allowing developers to create high-priority PHP defined constants.
Try:
<?php
$dir = get_template_directory_uri();
require $dir . '/yourfolder/yourfile.php';
?>
The first question is: Is your Php file custom and not part of the wp-load? That means it is not loaded/included by a theme or plugin.
Then - inside that file - WordPress is unknown and so is your theme and so is your functions.php.
So you have to load it. Depending on where your file is you have to go back to the root level to load wordpress:
require_once('/../../../wp-load.php');
But this is not a very elegant solution. Better address the root directly
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
Once WordPress is loaded you can access the function in your functions.php file. But beware of security concerns. Calling a pho file directly is never a good idea. Make sure you sanitize input if you work with post/get requests.
本文标签: customizationHow to call custom function from functionsphp in sitewide template files
版权声明:本文标题:customization - How to call custom function from functions.php in site-wide template files? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736619314a1945538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论