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

Use a theme’s functions.php for PHP that belongs to that theme: registering theme features, connecting callbacks to WordPress hooks, and loading theme assets. Put functionality that should keep working after a theme change in a plugin instead. The examples below focus on these documented patterns; adapt names and paths to your theme, and check each feature’s current requirements before adding it.

WordPress loads the active theme’s functions.php automatically. A child theme’s file loads before its parent theme’s file, and both block and classic themes can use the file. See the WordPress Theme Handbook for the loading model and theme-versus-plugin guidance.

Choose the right place for code

1. Keep design-dependent behavior in the theme

If code exists to support the active theme’s presentation or theme-specific behavior, functions.php is a natural home. WordPress loads it as part of the active theme.

2. Put durable site features in a plugin

If a feature should remain available when the site changes themes, put it in a plugin. A theme change can stop theme-bound code from running; don’t make a site-wide feature depend on the current design.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

3. Treat the active theme as the owner of its file

The active theme’s functions.php is loaded automatically. Code placed in an inactive theme’s file does not become a substitute for the active theme’s setup.

4. Know the child-theme loading order

When a child theme is active, its functions.php loads before the parent theme’s file. This matters when organizing code or defining functions: avoid assuming the parent file has already run when the child file is loaded.

5. Split a long file into helper files

Keep functions.php readable by moving related theme code into separate PHP files and including them from the theme file. Use the WordPress theme path helpers rather than relying on the current working directory. The handbook describes get_parent_theme_file_path() for a parent-theme path and get_theme_file_path() when child-theme override behavior is wanted.

<?php
require_once get_theme_file_path( 'inc/theme-helpers.php' );

Choose the path helper based on whether a child theme should be able to provide the file. Keep the included file within the theme’s intended code organization.

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

Connect code to WordPress at the right time

6. Use hooks instead of running setup immediately

Define a callback in functions.php, then attach it to the hook that matches when the work should happen. This avoids treating file loading itself as the right moment for every task.

7. Use an action when the callback performs work

An action callback performs a task at a lifecycle point. Attach work with add_action(); use the relevant action for the task rather than executing the task as soon as PHP reads the file.

8. Use a filter when the callback changes data

A filter callback receives a value to modify and must return the resulting value. Use add_filter() for this data-transforming pattern, rather than using an action as though it were a value-returning filter.

9. Register theme setup on after_setup_theme

Theme setup commonly belongs on after_setup_theme. Put theme setup work in a named callback and connect it to that action so WordPress invokes it at the appropriate point in the load process.

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

10. Keep callbacks narrowly named

Use a descriptive, theme-prefixed callback name so its purpose is apparent and it is less likely to collide with code from a plugin or another theme. For example, use a name such as freedom251_theme_setup rather than a generic name such as setup.

Load styles and scripts through WordPress

11. Enqueue front-end assets on wp_enqueue_scripts

Use the front-end wp_enqueue_scripts hook to register or enqueue front-end assets. WordPress’s Theme Handbook recommends asset functions and hooks instead of writing literal script or stylesheet markup from theme code. See Including Assets and the hook reference.

12. Use wp_enqueue_script() for scripts

Enqueue a script with WordPress’s API rather than printing a <script> tag yourself. The API accepts a unique handle and supports dependencies, version information, and placement options; consult the current function reference for its arguments.

13. Give each asset a unique handle

Choose a handle that identifies your theme and the specific file, such as freedom251-theme. A clear handle makes the asset identifiable when reviewing enqueue code and reduces the chance of an accidental naming collision.

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

14. Declare dependencies instead of assuming load order

When an enqueued script depends on another script, declare that dependency through the enqueue API. Do not rely on the browser or a manually printed tag to happen to load files in the order your code expects.

15. Supply version information deliberately

The script enqueue API supports a version argument. Use an intentional version value suited to your theme’s asset-update approach; do not imply that a version argument alone guarantees a particular caching outcome.

16. Choose script placement deliberately

wp_enqueue_script() supports a placement argument. Decide where the script should be placed based on the script’s needs, and check the current reference for the supported behavior rather than hard-coding a tag in a template.

17. Enqueue styles through WordPress asset APIs

Load styles through WordPress’s asset-loading approach and the appropriate hook instead of echoing a stylesheet tag from functions.php. That leaves asset handling within WordPress’s established mechanism; the Theme Handbook’s asset guidance covers the approach.

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

18. Keep asset loading in the theme only when it belongs there

Styles and scripts that exist specifically for the active theme can be loaded by the theme. If an asset supports functionality intended to remain regardless of theme changes, keep that feature and its asset ownership with the plugin that provides it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Escape values when rendering output

19. Escape HTML text with esc_html()

When printing a dynamic value as ordinary HTML text, escape it for that context with esc_html(). The WordPress escaping guidance explains why output should be escaped for its destination.

20. Escape URLs with esc_url()

When a dynamic value is used as a URL in rendered markup, use esc_url() for the URL context. Do not substitute an HTML-text escaping function and assume every output context is interchangeable.

21. Escape attribute values with esc_attr()

For a dynamic value placed in an HTML attribute, use esc_attr(). Attribute output has a different context from visible text or a URL.

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

22. Escape as late as possible

Keep data in its normal form while working with it, then escape it at the point it is printed. That makes the output context clear and avoids treating already-escaped content as a general-purpose stored value.

23. Match the escaping function to the exact context

HTML text, URLs, attributes, JavaScript, textarea content, XML, and permitted HTML each have context-specific considerations. Use the escaping method documented for the destination; esc_html(), esc_url(), and esc_attr() are not universal replacements for one another. See Escaping Data and the PHP Coding Standards.

Keep the PHP file predictable

24. Omit the closing PHP tag

For a PHP-only functions.php file, omit the closing ?> tag. The WordPress Theme Handbook recommends this practice to reduce the chance that accidental trailing whitespace is sent as output.

25. Review the theme-versus-plugin boundary before adding a feature

Before placing new code in functions.php, ask whether the behavior is genuinely tied to this theme. If the feature should continue after a theme change, implement it in a plugin; if it exists to support the theme, the theme file may be appropriate. This is the most important maintainability decision in the file.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

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.