0

I am trying to load WordPress options within a standalone PHP file. The file is located in /wp-content/plugins/plugin-name/file.php. I have used the following to open wp-load.php:

if (file_exists('../../../wp-load.php')) {
    require_once ('../../../wp-load.php');
}

Doing so outputs the following error:

Fatal error: Cannot redeclare some_plugin_function() (previously declared in /var/www/vhosts/domain.com/httpdocs/wp-content/plugins/plugin-name/class-frontend.php:5) in /var/www/vhosts/domain.com/httpdocs/wp-content/plugins/plugin-name/class-frontend.php on line 12

Removing all code from class-frontend.php does solve the issue, but of course I need that file. class-frontend.php is not called within the standalone PHP file so am unsure why this is happening.

1
  • I have the same problem. If I don't delete everything from functions.php of my child theme it will say that I am re-declaring some function (even though it is declared only once). And the error seems the same - the lines are different for the paths though they point to the same function in the same file. Did you get it fixed somehow ? Nov 24, 2019 at 16:13

3 Answers 3

1

The problem happens when you have a function declared in the root file of the plugin or in the functions.php file of a theme. You need to require_once a file for functions and move those functions into that file.

For example:

Your plugin is called some-plugin and the root file is some-plugin.php. Any function on the file some-plugin.php will cause an error in wp-load. So create a file called some-plugin-functions.php and require it once like

require_once('some-plugin-functions.php');

and move your functions to that file. This will eliminate the error message.

2
  • That's not true. It doesn't matter where you declare a function.
    – fuxia
    Jul 7, 2021 at 15:15
  • It doesn't but if you are having an issue with the function being declared 2 times, doing what I stated will fix the issue. I just fixed a site for another developer that had the same problem. Jul 8, 2021 at 16:04
1

I assume you're calling your PHP file directly.

Let's think about it logically.

You call your custom PHP file directly, and it loads all the functions in that file.

Then you include wp-load.php which will now load WordPress.

Part of loading WordPress is loading your active plugins. If the original file that you called is normally included when WordPress loads, then that file will load once at the very beginning, and then for a 2nd time when WordPress initiates all its plugins.

So yep, you'll be declaring functions and variable twice.

A simple solution is to use a flag at the beginning of your main plugin file:

if ( empty( $flag_to_say_ive_been_executed ) ){
    $flag_to_say_ive_been_executed = true;
}
else {
    return;
}

The better solution overall is to not have to call your plugin file directly to load WordPress as you're currently doing. You might want to redesign your approach to solving whatever you're trying to solve.

0

This error has nothing to do with wp-load.php. You are declaring the 'some_plugin_function' function twice within your plugin file. The error message tells you exactly where to look.

You don't need to call class-frontend.php from your stand-alone PHP file to get the error. It's in a plugin, so as long as that plugin is active, it will be parsed.

6
  • 1
    I looked there and it is not declared anywhere else. There is no error anywhere else on any other file. I find it odd as I don't call class-frontend.php in the file at all.
    – manc
    Mar 30, 2013 at 15:54
  • Please post the code from class-frontend.php
    – vancoder
    Mar 30, 2013 at 15:56
  • its a lot of code but know it has nothing to do with the error
    – manc
    Mar 30, 2013 at 16:41
  • It has everything to do with the error.
    – vancoder
    Mar 30, 2013 at 17:23
  • 1
    The question has been answered. The error message clearly states what the problem is (twice declaring the same function) and where this takes place (class-frontend.php lines 5 and 12). This error will always be thrown as long as the plugin is active. That's all there is to it.
    – vancoder
    Mar 31, 2013 at 19:45

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.