What belongs in functions.php?
Every WordPress theme can include a functions.php file that runs on every request, front end and admin. It is where themes declare features (add_theme_support()), register menus and image sizes and enqueue their CSS and JavaScript — and where site owners add small tweaks with actions and filters: removing the emoji script, disabling comments, changing the excerpt length or customising the login screen.
Copying snippets from random blog posts is risky: many are outdated (emoji styles moved to wp_enqueue_emoji_styles in WordPress 6.4), use unprefixed function names that collide with plugins, or skip escaping. This generator offers a curated library of 22 snippets that follow the WordPress Coding Standards: tabs, Yoda conditions, prefixed named functions with docblocks, escaped output and translatable strings using your text domain.
Choose Child theme functions.php for presentation-related code, or Site plugin for functionality that must survive a theme change — disabling comments, XML-RPC or author archives should not stop working when someone switches themes.
How to use the WordPress Functions.php Snippet Generator
- Set your function prefix (e.g. your theme or company slug) and text domain, and choose whether the code is for a child theme or a site plugin.
- Tick the snippets you need, or start from a preset such as Recommended cleanup or Brochure site. Options appear under each selected snippet.
- Review any warnings — some snippets (SVG uploads, disabling feeds or XML-RPC) have trade-offs explained right there.
- Copy the combined PHP or download it as
functions.php/ a plugin file, and test it on a staging site before deploying.
Example
Every snippet is a prefixed, documented function hooked into WordPress — for example, limiting revisions:
/**
* Keep at most 10 revisions per post.
*
* Preferred alternative — add this line to wp-config.php instead:
* define( 'WP_POST_REVISIONS', 10 );
*
* @return int
*/
function acme_limit_revisions() {
return 10;
}
add_filter( 'wp_revisions_to_keep', 'acme_limit_revisions' );functions.php or a site plugin?
Code in a theme’s functions.php only runs while that theme is active, and edits to a parent theme are lost on update — always use a child theme. A site-specific plugin is a single PHP file with a plugin header in wp-content/plugins/your-slug/; it keeps working regardless of the theme. A good rule: if the code changes how the site looks, put it in the theme; if it changes how the site works, put it in a plugin.
Notes on specific snippets
- Disable XML-RPC removes all methods, but
xmlrpc.phpis still executed. Block it at the server with the .htaccess generator for better performance under attack. - Allow SVG uploads is limited to administrators and does not sanitise files. SVGs can carry JavaScript; use a sanitising plugin if anyone else uploads media.
- Limit post revisions: the
WP_POST_REVISIONSconstant inwp-config.phpis simpler and applies before any plugin loads; the filter is for hosts where you cannot editwp-config.php. - Custom image sizes only apply to new uploads. Regenerate thumbnails (
wp media regenerate) for existing images, and preview the resulting files with the Image Size Calculator. - Enqueue assets uses
filemtime()so browsers fetch new CSS/JS as soon as the file changes, and the WordPress 6.3+strategy => deferargument for non-blocking scripts.
Frequently Asked Questions
Why do all the functions have a prefix?
PHP functions share one global namespace. If a plugin defines disable_emojis() and so does your theme, the site crashes with a fatal “cannot redeclare” error. A unique prefix such as acme_ avoids collisions.
What if I break my site with functions.php?
A PHP error triggers WordPress recovery mode, which emails the admin a special login link so you can fix the code. You can also rename or edit the file over SFTP or your host’s file manager. Testing on staging first avoids the problem altogether.
Should I include the opening <?php tag?
Only if the file is new. An existing functions.php already starts with <?php; append the snippets below it without a second opening tag and without a closing ?>.
Do these snippets work with block themes?
Most do. Theme supports, menus and the excerpt filter are mainly for classic themes — block themes enable features through theme.json and use the Navigation and Post Excerpt blocks, which have their own settings.
Is disabling comments with code better than a plugin?
It is lighter: the snippet covers post type support, open/closed status, existing comments, the admin menu, dashboard box, admin bar, widget, comments feed and REST endpoints without adding a settings screen.