1

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?.

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) 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 5

2

It'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:

(Adding this answer because I landed here and namespaces were my snag - I didn't realize the add_action namespace issue.)

1

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

1

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.

0

Try:

<?php

  $dir = get_template_directory_uri();
  require $dir . '/yourfolder/yourfile.php';

 ?>
1
  • Thanks for the comment. I am still researching. all Wordpress functions are not working in that file. Trying to load Wordpress functions to that php file. If i call get_template_directory_uri it will show me PHP Fatal error: Uncaught Error: Call to undefined function get_template_directory_uri() in /opt/bitnami/apps/wordpress/htdocs/wp-content/themes/devformaspace/modules/recaptcha_verify.php:3
    – Bayanaa
    Commented Apr 25, 2019 at 23:35
0

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.

3
  • Thanks for the answer. Q answer is : yes i have tons of php file in theme and my theme has a functions.php. Functions.php includes many functions like connect to API etc and all works for page-templates, core and libraries folders, but other folders cannot access to functions.php functions. I tried to required wp-load.php but it gaves me 500 error. I debugged wp-load.php file but it failed on ABSPATH defines. I still finding better way to use a function from functions.php for everywhere.
    – Bayanaa
    Commented Apr 29, 2019 at 7:37
  • "I debugged wp-load.php file but it failed on ABSPATH defines" ... really...? Never touch the core! Including wp-load is an everyday job. You must have a typo or wrong path. In your example the ".php" extension is missing in the require statement. Otherwise debug the pathes.
    – Pat_Morita
    Commented Apr 29, 2019 at 12:13
  • I copied and past your 2 example into that file but still cannot call function from functions.php and when page called from ajax it will stop and no response at require_once wp-load.php. Thats why I debugged where it will go but ABSPATH didn't defined and stopped there. I didn't change anything.
    – Bayanaa
    Commented Apr 29, 2019 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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