Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Use PHP’s function_exists() to check whether a function is defined. In a WordPress theme, wrap a declaration in that check only when conditional definition is intentional; for ordinary theme functions, a distinctive theme prefix or PHP namespace is the better first defense against naming collisions.

Check for a PHP function with function_exists()

function_exists() checks whether a named PHP function is currently defined. WordPress’s Theme Handbook demonstrates guarding a theme setup declaration this way:

if ( ! function_exists( 'acme_theme_setup' ) ) {
    function acme_theme_setup() {
        // Add theme setup code here.
    }
}

add_action( 'after_setup_theme', 'acme_theme_setup' );

The guard controls whether PHP makes the declaration. The add_action() call separately registers the callback so WordPress runs it at the appropriate point. The Theme Handbook identifies after_setup_theme as the first hook available after the theme’s functions.php has loaded. See Theme Functions and Custom Functionality (functions.php).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Prefer unique names over blanket guards

Give functions a distinctive project or theme prefix, such as acme_theme_, or use a PHP namespace when that fits the codebase. Unique identifiers address the collision at its source rather than relying on load order to decide which implementation wins. WordPress recommends unique function and class names in its Plugin Handbook best practices.

A guard is useful when an existing implementation is deliberately allowed to provide the function. It is not a harmless wrapper to add around every declaration: if another implementation with the same name loads first, the guarded declaration is skipped, and later code may silently use that earlier implementation. WordPress cautions against wrapping every function or class in existence checks and reserves override-style checks for shared libraries.

Choose the approach that matches the code

Approach Use it when Tradeoff
Prefix or namespace the function The theme owns the implementation. Reduces accidental name clashes at their source; this is the normal choice for theme-owned functions.
Guard the declaration with function_exists() A shared or intentionally replaceable implementation may already have defined the function. Prevents a duplicate declaration, but the earlier implementation keeps the name and may be selected instead.

Account for WordPress theme load order

Only the active theme’s functions.php runs. When a child theme is active, WordPress loads its functions.php immediately before the parent theme’s file; theme functions load after plugin files. An implementation loaded earlier may therefore have defined a name before a later theme declaration is reached. A guard can avoid a duplicate declaration, but it does not make shared names a sound design.

WordPress describes functions.php as behaving similarly to a plugin, while recommending a plugin for functionality that should remain available regardless of the site’s design. Put presentation and theme setup code in the theme; move design-independent site functionality to a plugin when it must persist through a theme change. See Theme Functions and Custom Functionality (functions.php).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not confuse function checks with query or feature checks

Is a PHP function defined?

Use function_exists( 'some_function' ) when the question is whether that PHP function has been defined.

Is the current request a particular kind of page?

Conditional tags such as is_home() test WordPress query context, not whether a PHP function exists. The query has not run when functions.php loads, so query-dependent conditional tags belong in a template or in code deferred to an appropriate later hook. See the Conditional Tags guidance.

Does the active theme support a feature?

Use current_theme_supports() to test theme support. To load a file only when a feature is supported, WordPress documents require_if_theme_supports( $feature, $file ). These checks answer a feature-support question, not whether a PHP function is defined. See require_if_theme_supports().

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.