It’s not often that a WordPress developer doesn’t modify the functions.php file within a theme they’re developing.
Why? Because functions.php gives you the power to control functionality specific to the theme. With some simple code inclusions and/or modifications, you can override existing website functionality and execute multiple functions in an efficient manner, instead of using potentially bulky and insecure plugins to achieve the same thing.
Although adding a few plugins can get the job done, learning the following ways to change WordPress websites through the functions.php file – located in your theme’s folder – is rather quick and easy.
What is the functions.php File and How Can You Access It?
The functions.php file basically acts like a WordPress plugin.
You can add code to call functions and change features and functionality on your client sites. In short, you can add as many functions to this file as you like, whereas plugins typically focus on one task, and you have to install a bunch of them to cover different areas.
If you’ve already made your WordPress website but you don’t currently have a functions file for a theme, just create a file called functions.php and place it in the /wp-content/themes/yourthemename/ subdirectory. If you do have a functions.php file, or you want to figure out if your client sites have them, use an FTP client (such as FileZilla) to navigate to the /wp-content/themes/yourthemename/ subdirectory and look for the file.
Once you find or create the file, feel free to add whatever code you want.
Keep in mind that if you have a plugin that calls the same function as something you put in functions.php, it can cause problems on a site.
Let’s have a look at the coolest ways to change your client WordPress sites with the functions.php file.
1. Post Thumbnails (Featured Images)
Although many themes include support for adding post thumbnails, or featured images, you can also add this functionality through the functions.php file:
add_theme_support( 'post-thumbnails' );
The following code must be included in the particular loop where you want the thumbnail to show up on a site:
<?php the_post_thumbnail(); ?>
2. Automate the Copyright Date on a Site
There’s no reason to change the copyright at the bottom of a site every time a new calendar year comes around. Chances are you will forget, then it looks like you haven’t updated the site since last year. Make the copyright date dynamic by adding the following code to the functions.php file:
function copyright($start_year, $site_name) {
$year = date('Y');
echo "© Copyright ";
echo $start_year;
if($start_year != $year) echo "-$year";
echo ", $site_name, All Rights Reserved.";
}
After that, you need to locate the footer file and place the following code in there. Keep in mind that the year argument is when the company or site started, not the current year.
<div class='copyright'>
<?php copyright(2011, "Your Company Name"); ?>
</div>
3. Modify a Site’s Default Gravatar
If you go to Settings > Discussion in WordPress you can see an area that allows you to change the default Gravatar when visitors post comments on your client sites.
Why not brand the Gravatar to go along with the site theme? Add the code below to the functions.php file and you are good to go:
add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/mygravatar.gif';
Make sure you specify the right file location above for your Gravatar, and copy the code after everything above while changing the CompanyName area.
$avatar_defaults[$myavatar] = "CompanyName";
return $avatar_defaults;
}
4. Put a Favicon on Your Client Sites
The favicon is that little tiny icon that shows up in a browser tab when someone lands on a site. There are plenty of plugins to include a favicon, but there’s really no reason to waste resources with a plugin. Try out the code below, and simply replace the area that says “thepathtoyourimagegoeshere” with the file path of your desired favicon image.
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'thepathtoyourimagegoeshere" />';
}
Change the path to your image in the code above, and place the following code just below:
add_action('wp_head', 'blog_favicon');
5. Google Analytics
This is one of the more common modifications to make in the functions.php file, since it links a Google Analytics account so clients can see additional info on visitors.
The code below works just fine, but you have to add your Google Analytics code for it to link up to an account.
<?php
add_action('wp_footer', 'add_googleanalytics');
function add_googleanalytics() { ?>
// Place the code you get from Google Analytics here
<?php } ?>
6. Customize Footers
The footer usually includes default verbiage, so why not change it to match a client’s brand? You can add the code in the functions.php file, but there are a few areas to change so your footer is customized for the site.
function remove_footer_admin () {
echo "Whatever Text You Want";
}
add_filter('admin_footer_text', 'remove_footer_admin');
7. Auto-Resizing and Cropping Featured Image
This makes resizing featured images super fast. Add the code below so that images are cropped and scaled to the perfect size.
Check to see if the following line is in functions.php. If not, add it:
add_theme_support( 'post-thumbnails' );
Also, place the following code in the functions file:
add_image_size('imagename', 200, 200, true);
Change the image name to what you want, and modify the first number for crop width and second number for crop height.
Place this wherever you want to see the image:
<?php the_post_thumbnail('imagename'); ?>
8. Maintenance Mode
Maintenance mode is perfect for you to reveal a friendly page when you are working on a client’s site.
function maintenance_mode() {
if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {wp_die('Maintenance.');}
}
The following line needs to be included for this to work. If you want to remove the maintenance mode and let people see your site, just comment out the following line.
add_action('get_header', 'maintenance_mode');
There are also plugins, like the image above, that help you get a little more creative with this mode.
9. Minimum Character Comment Limit
If a client has problems with comments that are too long or too short, you can provide a solution. Reveal an error warning whenever someone posts a comment that is too long or too short using the code below in the functions.php file:
new GW_Minimum_Characters( array(
'form_id' => 524,
'field_id' => 1,
'min_chars' => 10,
'max_chars' => 500,
'min_validation_message' => __( 'Sorry! You need more than %s characters.' ),
'max_validation_message' => __( 'Sorry! You need less than %s characters.' )
) );
Feel free to change the IDs, character limits and validation messages. (Please note that you must use this snippet with Gravity Forms.)
10. Show Twitter Follower Counts for Your Web Design Customers
This one comes from Rarst. It allows you to reveal a Twitter follower count anywhere on a site without using a bulky plugin.
function rarst_twitter_user( $username, $field, $display = false ) {
$interval = 3600;
$cache = get_option('rarst_twitter_user');
$url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
if ( false == $cache )
$cache = array();
// if first time request add placeholder and force update
if ( !isset( $cache[$username][$field] ) ) {
$cache[$username][$field] = NULL;
$cache[$username]['lastcheck'] = 0;
}
// if outdated
if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
// holds decoded JSON data in memory
static $memorycache;
if ( isset($memorycache[$username]) ) {
$data = $memorycache[$username];
}
else {
$result = wp_remote_retrieve_body(wp_remote_request($url));
$data = json_decode( $result );
if ( is_object($data) )
$memorycache[$username] = $data;
}
if ( is_object($data) ) {
// update all fields, known to be requested
foreach ($cache[$username] as $key => $value)
if( isset($data->$key) )
$cache[$username][$key] = $data->$key;
$cache[$username]['lastcheck'] = time();
}
else {
$cache[$username]['lastcheck'] = time()+60;
}
update_option( 'rarst_twitter_user', $cache );
}
if ( false != $display )
echo $cache[$username][$field];
return $cache[$username][$field];
}
Figure out where to put the counter on your client sites, and include the following code in the proper loop:
echo rarst_twitter_user('codeinwp', 'name').' has '.
rarst_twitter_user('codeinwp', 'followers_count').' followers after '.
rarst_twitter_user('codeinwp', 'statuses_count').' updates.';
You may also be interested in:
- 10+ Best HTML/CSS Books for Beginners and Advanced Coders
- 5 High-Quality Sketch Alternatives for Windows (2024)
- How to Create a WordPress Multilingual Site: Considerations + Step-by-Step Tutorial
And please let us know in the comments section if you have any questions about the above ways to change the way WordPress sites work using the functions.php file. Share your thoughts if you have any other cool hacks to include in this list.
…
Don’t forget to join our crash course on speeding up your WordPress site. Learn more below:
Layout and presentation by Chris Fitzgerald and Karol K.