Use PHP’s function_exists() when you need to know whether a named function is already defined. In a theme, the usual guarded declaration is:
if ( ! function_exists( 'acme_theme_setup' ) ) {
function acme_theme_setup() {
// Register theme setup behavior.
}
}
add_action( 'after_setup_theme', 'acme_theme_setup' );
The guard controls whether PHP declares the function; add_action() controls when WordPress runs it. Use this pattern deliberately, alongside unique names and correct WordPress hooks—not as boilerplate around every function.
What function_exists() actually checks
function_exists( 'some_function' ) returns whether PHP currently has a function with that name defined. It is a PHP availability check, not a general WordPress feature or page-state check. The WordPress Plugin Handbook lists function_exists() among PHP existence checks: Best Practices – Plugin Handbook.
In a theme, a negated check prevents a declaration when another loaded component has already claimed the name:
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
if ( ! function_exists( 'acme_theme_setup' ) ) {
function acme_theme_setup() {
// Theme setup code.
}
}
WordPress documents this guarded setup pattern in Theme Functions – Theme Handbook.
When a guarded declaration is appropriate
Theme-owned setup with a fallback
Use a guard when the theme should provide an implementation only if no earlier implementation exists. Connect the function to the appropriate hook:
if ( ! function_exists( 'acme_theme_setup' ) ) {
function acme_theme_setup() {
add_theme_support( 'post-thumbnails' );
register_nav_menus(
array( 'primary' => __( 'Primary Menu', 'acme-theme' ) )
);
}
}
add_action( 'after_setup_theme', 'acme_theme_setup' );
after_setup_theme is the first hook available after the theme’s functions.php has loaded, making it the normal place for theme setup. See Custom Functionality (functions.php) – Theme Handbook.
Rank #2
- Used Book in Good Condition
Shared or intentionally replaceable code
A guard can be justified when a shared library or compatibility layer intentionally allows one implementation to stand in for another. In that design, document which implementation wins and what API callers can rely on.
Why naming matters more than guards
Prefix functions with a distinctive theme identifier, such as acme_theme_, or use a namespace where the project architecture supports it. Unique identifiers prevent collisions at the source. WordPress recommends unique function and class names in its theme and plugin guidance.
Do not wrap every declaration in function_exists() merely to avoid thinking about names. WordPress warns that if another implementation loads first, your later code may silently use that implementation—or fail because it expects behavior the earlier function does not provide. The Plugin Handbook recommends reserving override-style existence checks for shared libraries: Best Practices – Plugin Handbook.
WordPress load order changes what “already exists” means
- Only the active theme’s
functions.phpexecutes. - When a child theme is active, its
functions.phploads immediately before the parent theme’s file. - Theme functions load after plugin files.
Therefore, a function may already exist because a plugin, child theme, or earlier-loaded code defined it. An existence guard can make a declaration conditional, but it does not make a generic function name safe. WordPress describes functions.php as behaving similarly to a plugin and recommends putting site-wide functionality in a plugin when it should survive a theme change. Details are in Theme Functions – Theme Handbook and Custom Functionality (functions.php) – Theme Handbook.
Do not confuse three different checks
PHP function availability
if ( function_exists( 'some_function' ) ) {
some_function();
}
Use this when the question is “Can PHP call this function now?”
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minutePage or query state
Conditional tags such as is_home() answer questions about the current WordPress query. They do not test whether a function exists. Query-dependent conditionals generally work only after the query has run, so calling them at file load time in functions.php is too early. Put the check in a template or defer it to an appropriate action or filter. WordPress explains this timing in Conditional Tags – Theme Handbook.
Rank #4
Theme feature support
current_theme_supports( 'feature-name' ) checks whether the active theme supports a feature. It does not check for a PHP function. When loading a file only if a feature is supported, use WordPress’s documented require_if_theme_supports( $feature, $file ) helper:
require_if_theme_supports( 'post-thumbnails', get_template_directory() . '/inc/thumbnails.php' );
Reference: require_if_theme_supports() – Function.
Choosing the right pattern
| Approach | Use it when | Trade-off |
|---|---|---|
| Theme-specific prefix or namespace | The theme owns the implementation. | Prevents accidental clashes at the naming level. |
function_exists() guard |
A shared or intentionally replaceable implementation may already be loaded. | Allows conditional declaration, but an earlier same-named function may silently determine behavior. |
current_theme_supports() |
You need to test active-theme feature support. | Tests a WordPress feature flag, not PHP function availability. |
require_if_theme_supports() |
You need to load a file only when a theme feature is supported. | Controls file loading based on support, not declaration collisions. |
A practical workflow for theme code
- Define the question. Decide whether you are checking a PHP function, query state, or theme support.
- Choose a unique identifier. Prefix the function with the theme or project name; use a namespace where appropriate.
- Guard only a deliberate fallback. Add
if ( ! function_exists( ... ) )when an existing implementation is an intentional compatibility option. - Attach the callback to the correct hook. For theme setup, use
after_setup_themerather than executing setup at an arbitrary file-load point. - Consider ownership. Move functionality that must remain after a theme switch into a plugin instead of keeping it in the theme.
- Test the collision case. Confirm what happens when a plugin or child theme defines the same name first; do not assume the guard selects your implementation.
Common mistakes and fixes
Using a conditional tag to test a function
Mistake: treating is_home() or another conditional tag as a function-existence test. Fix: use function_exists() for PHP availability, and run query conditionals only after WordPress has built the query.
Best Value
Calling a query conditional while functions.php loads
Mistake: evaluating is_home() at top level in functions.php. Fix: move the logic into a template or a later hook, as described in Conditional Tags – Theme Handbook.
Guarding every function
Mistake: adding guards to all declarations and assuming this is always safer. Fix: use a distinctive prefix or namespace for theme-owned code, and reserve guards for documented fallback or shared-library behavior.
Putting permanent site features in a theme
Mistake: placing functionality that must survive a design change in functions.php. Fix: implement that functionality in a plugin, while keeping presentation-specific setup in the theme.
Recommended baseline
For ordinary theme setup, use a unique name and a hook. Add a function_exists() guard only when the absence of the function specifically means the theme should provide its fallback implementation:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteQuick Recap
if ( ! function_exists( 'acme_theme_setup' ) ) {
function acme_theme_setup() {
// Register theme setup behavior here.
}
}
add_action( 'after_setup_theme', 'acme_theme_setup' );
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

