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.

Live output

Enable JavaScript to customise; default output below.

Live preview site-tweaks.php
<?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.

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

  1. 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.
  2. Take only what you need. A file of twenty snippets nobody remembers adding is worse than three you can explain.
  3. Watch the priorities. The excerpt length filter runs at 999 here because themes filter it too and the last one wins.
  4. Test the toolbar rule while logged in as a subscriber, not as yourself. It is the one people get backwards.
  5. 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_length only 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_bar does not block the admin. Anyone who can reach /wp-admin still 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-embed is 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.php disappear 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?
The post has a manually written excerpt, which is used as typed. The filter only applies to automatic ones.
Where should this file go?
A site specific plugin. functions.php ties the behaviour to the theme.
Does removing the emoji script break emoji?
No. Browsers render emoji natively. The script only existed to polyfill the ones that did not.
Why did my snippet stop working after a theme update?
Either it lived in the theme, or the theme now filters the same thing at a later priority.
Is any of this a performance win?
Removing the emoji and embed scripts saves a few requests and some kilobytes. It will not fix a slow site on its own.

From the people who built this tool

WP Adminify

The WordPress admin, rebuilt: a dashboard worth looking at, menu and column control, a real file manager and the login page your client sees.

See WP Adminify Free version on WordPress.org

Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.