functions.php Snippet Generator
Pick the small WordPress tweaks you actually want, excerpt length, admin bar, emoji scripts, login logo, and get them as one tidy file.
<?php
/**
* Site tweaks.
*
* These belong in a site specific plugin rather than a theme's functions.php:
* the behaviour here is not the theme's, and it should survive a redesign.
*/
/**
* The excerpt length, in words.
*
* Priority 999 because themes filter this too, and the last one wins.
*
* @param int $length Current length.
* @return int
*/
function my_site_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'my_site_excerpt_length', 999 );
/**
* What an automatic excerpt ends with.
*
* @param string $more Current ending.
* @return string
*/
function my_site_excerpt_more( $more ) {
return '…';
}
add_filter( 'excerpt_more', 'my_site_excerpt_more' );
/**
* Hides the toolbar from anyone who cannot edit.
*
* It pushes the page down 32 pixels for people who have no use for it.
*
* @param bool $show Whether to show it.
* @return bool
*/
function my_site_hide_admin_bar( $show ) {
if ( current_user_can( 'edit_posts' ) ) {
return $show;
}
return false;
}
add_filter( 'show_admin_bar', 'my_site_hide_admin_bar' );
/**
* Removes the emoji script.
*
* Around 12 KB of JavaScript polyfilling emoji for browsers that have
* supported them natively for years. Emoji still work; only the polyfill goes.
*/
function my_site_remove_emoji() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
}
add_action( 'init', 'my_site_remove_emoji' );
/**
* Stops the DNS prefetch hint for the emoji CDN that goes with it.
*
* @param array $urls Hints for this relation.
* @param string $relation_type The relation.
* @return array
*/
function my_site_remove_emoji_dns( $urls, $relation_type ) {
if ( 'dns-prefetch' !== $relation_type ) {
return $urls;
}
return array_filter(
$urls,
static function ( $url ) {
return false === strpos( (string) $url, 's.w.org' );
}
);
}
add_filter( 'wp_resource_hints', 'my_site_remove_emoji_dns', 10, 2 );
Output is valid and updates as you type.
Fix the highlighted fields to update the output.
Pick the small WordPress tweaks you actually want and get them as one file, each with the priority or hook that makes it stick.
How to use
- Put the result in a site specific plugin, not the theme’s
functions.php. None of this is the theme’s behaviour, and it should survive a redesign. - Take only what you need. A file of twenty snippets nobody remembers adding is worse than three you can explain.
- Watch the priorities. The excerpt length filter runs at 999 here because themes filter it too and the last one wins.
- Test the toolbar rule while logged in as a subscriber, not as yourself. It is the one people get backwards.
- Keep the emoji removal if performance matters; drop it if you would rather not explain a missing polyfill later.
Example
function acme_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'acme_excerpt_length', 999 );
The priority is the whole trick. At the default 10, a theme that filters the same thing at 11 silently wins, and the snippet looks broken.
Pitfalls
excerpt_lengthonly applies to automatic excerpts. A manually written excerpt is used as typed, which is why the setting “does nothing” on some posts.- Hiding the toolbar with
show_admin_bardoes not block the admin. Anyone who can reach/wp-adminstill can; this is presentation only. - Removing the emoji script does not remove emoji. It removes the polyfill that rewrote them into images for browsers that no longer need it.
wp-embedis what makes your posts embeddable elsewhere. Removing it is a trade, not a free win.- The revisions limit applies from the moment it is added. Revisions already stored stay until something deletes them.
- Changing the login logo with CSS only covers the login screen. The admin logo in the toolbar is separate.
- Snippets in
functions.phpdisappear when the theme changes, usually at the worst moment. - A fatal in this file takes the whole site down, front end and admin together. Add one thing at a time.
Compatibility
excerpt_length, excerpt_more, show_admin_bar, admin_footer_text and login_headerurl have all been stable since WordPress 3.x. wp_revisions_to_keep needs 3.6, wp_resource_hints needs 4.6, and the custom logo theme mod needs 4.5. The emoji actions are the ones core registers in wp-includes/default-filters.php. The generated code targets PHP 7.0 and up, and the tool runs entirely in your browser.
Frequently asked questions
Why is my excerpt still long?
Where should this file go?
functions.php ties the behaviour to the theme.