10 cool things you can do with your functions.php files

Many designers don’t use it much because it is more intimidating in terms of code, but the functions.php file might just be the most powerful theme file in WordPress. Let’s take a look at some of the possibilities it offers.

1. Change the length of the excerpt

The default size of WordPress’ excerpts is at 55 words. For obvious reasons, you may want to override this setting by using the following code in your functions.php file.

function new_excerpt_length($length) {
return 90;
add_filter('excerpt_length', 'new_excerpt_length');

With this snippet, your excerpts will now be 90 words long.

2. Automatically extract the first image of a post

If you want to have a thumbnail-like feature, but without the work of creating the thumbnails every time, you should try to add this to your functions.php.

function first_image() {
global $post, $posts;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
return $first_img;

It will automatically pull out the first image from the content, then all you’ll need to do is to echo first_image() where you want to use the image in the theme.

3. Stop pinging your own blog

Remove pings to yourself and reduce the noise by adding this simple no_self_ping function.

function no_self_ping( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
add_action( 'pre_ping', 'no_self_ping' );

4. Add custom fields to user profiles

User profiles don’t give too much information out of the box in WordPress, you can extend the information about your users with this simple code in your functions.php file.

function my_custom_userfields( $contactmethods ) {
$contactmethods['contact_phone_office'] = 'Office Phone';
$contactmethods['contact_phone_mobile'] = 'Mobile Phone';
return $contactmethods;

Simply add more lines to create more contact methods. You can then display the info using the following code anywhere in your theme.

$current_author = get_userdata(get_query_var('author'));
echo esc_url($current_author->contact_phone_office);

5. Remove the WordPress Version Info

For security reasons, you may want to remove the WordPress version info from your site. For that, just add the following code in your functions.php file.

function complete_version_removal() { return ''; } add_filter('the_generator', 'complete_version_removal');

6. Limit the maximum number of post revisions

Post revisions functionality is great, but as your blog get larger it can bloat your database. This code in your functions.php file allows you to limit the number of post revisions.
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 3);

That’s it! Adding only one line of code will limit the number of post revisions on your blog.

7. Load jQuery from Google’s CDN

If you want to take advantage of Google’s servers to speed up your website, one way to do it is by loading jQuery from their content delivery network. By adding the following code to your functions.php file, you will de-register the included jQuery script and replace it by Google’s version.
add_action( 'init', 'jquery_register' );
function jquery_register() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );

8. Display all site settings in the admin

The following code will add an additional option to your settings menu with a link to “all settings” which will show you a complete list of all the settings you have within your database related to your wordpress site. Of course this menu will only make this link visible to admin users and hide it for all other users.

function all_settings_link() {
add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
add_action('admin_menu', 'all_settings_link');

9. Display DB queries, time spent and memory consumption

A great snippet if you want to check things for the speed of your website. After adding this code in your functions.php file, you should see the number of database question, time spent to create the page, and memory consumption. It could help you to identify speed problems with your website.

function performance( $visible = false ) {
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
echo $visible ? $stat : "<!-- {$stat} -->" ;
add_action( 'wp_footer', 'performance', 20 );

For this snippet to work, you’ll have to make sure that your theme is using wp_footer().

10. Enable shortcodes in widgets

A good feature to add if you want to make some plugins’ shortcodes functional in widgets also. To do it just add the following code to your functions.php file.

if ( !is_admin() ){
    add_filter('widget_text', 'do_shortcode', 11);