Common causes
- A file included with include or require instead of include_once or require_once, so it loads twice
- Two plugins, or a plugin and a theme, defining the same function name
- A child theme copying a parent theme function that is not wrapped in function_exists()
- A function declared inside a loop or inside another function that runs more than once
- A function name that collides with a new built-in function after a PHP upgrade
- A backup copy of a file (functions-old.php) being loaded alongside the original
How to fix it
- Compare the two locations. If both paths are the same file, it is being included twice. If they are different files, you have a naming conflict between two pieces of code.
- Load files only once. Change include 'helpers.php'; to require_once __DIR__ . '/helpers.php'; everywhere the file is loaded. Use absolute paths so the same file is not loaded via two different relative paths.
- Guard optional definitions. Wrap functions that may already exist in if (!function_exists('my_helper')) { ... }. This is the standard pattern for pluggable functions in WordPress parent themes.
- Resolve plugin or theme conflicts. In WordPress, rename the plugin folder named in one of the paths over SFTP to deactivate it, then contact its author or update it. Prefix your own functions with a unique name, such as mytheme_.
- Use namespaces in new code. Put functions in a namespace (namespace MyPlugin;) or as static methods on a class so names cannot clash with other code.
Safe function definition
<?php
// Load the file only once
require_once __DIR__ . '/inc/helpers.php';
// In shared or pluggable code
if (!function_exists('mytheme_format_price')) {
function mytheme_format_price(float $amount): string {
return number_format($amount, 2);
}
} How to stop it happening again
- Always use require_once or Composer autoloading for library files
- Prefix or namespace every function in plugins and themes
- Delete old copies of files instead of leaving them where they are loaded
- Check new PHP releases for built-in functions that clash with your names