0

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
    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ż Apr 26 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 Apr 26 at 10:07
0

Try:

<?php

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

 ?>
  • 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 Apr 25 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.

  • 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 Apr 29 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 Apr 29 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 Apr 29 at 13:41
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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