9 Tweaks to Functions.php that can Tailor Your WordPress Site

  • Amy
  • 0
  • Jan 12, 2015
  • Web Development

Being an aspiring WordPress developer, it’s essential for you to figure out the nuts and bolts of functions.php file. It’s an integral part of the WordPress core and plays a huge role when it comes to developing or modifying a theme. Functions.php file is a core file within your theme and gives you enough control over the look and functionality of a specific theme.

With just a little code or modifications, you can speed up the development and execute a great deal of functions in the manner most effective. When used correctly, this file can do wonders within your website. Plus, it also accelerates the performance of your WordPress website development because you have all the codes at a common place.


Tweaks to WordPress Function.php file

In this post, I’ll throw some light on the 9 most practical ways and tricks to make the most out of your functions.php file.

Adding Post Thumbnails

Although the majority of the themes comes packed with support for adding post thumbnails, or we can say featured images, you can integrate the same functionality within your functions.php file with this way:

add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails' );

Below we have mentioned a loop which must be included in the loop where you want the thumbnail to be displayed on your website.

the_post_thumbnail();
the_post_thumbnail();

Google Analytics

Adding Google Analytics within your functions.php file will call you to add a code to your page where you have the footer. The code will look like this:

// add Google Analytics code here
<?php
  add_action('wp_footer', 'add_googleanalytics');
  function add_googleanalytics() { ?>
    // add Google Analytics code here
 <?php } ?>

In the first line, we have used the action ('wp_footer', 'add_googleanalytics), which depicts that it will add Google Analytics in the footer. This is called as “hook”. To utilize it, grab your Google analytics code first and replace it with the line “// add Google Analytics code here “. Also, not to forget about eliminating the comment marks.

Making the Copyright Date

There is no need to change the copyright year, every time the new year arrives. Just simply add the below mentioned code within your functions.php file, which helps you logically organize copyright year and gives your website an updated look.

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."; }
function copyright($start_year, $site_name) {
  $year = date('Y');
  echo "&copy; Copyright ";
  echo $start_year;
  if($start_year != $year) echo "-$year";
  echo ", $site_name, All Rights Reserved.";
}

The one you add the code, you need to locate the footer file and place the same code there. Also, remember that the year adds should be the year in which the website started and not the current year.

<div class='copyright'>
  <?php copyright(2014, "Add the name of your company"); ?>
</div>

Creating an Excerpt Box on Pages

WordPress users use excerpt boxes to provide a summary or a small description of their post. Apart from this, an excerpt box serves two purposes:
1) It replaces the full content when the option to display summary is selected in the Dashboard;
2) It can be easily displayed in places where instant post summaries have loading priority than the full content. Some of these places include: search results, Tag archives, Category Archives, and Author archives.

So, to create an excerpt box, add the following snippet within your functions.php file.

add_post_type_support( 'page', 'excerpt' );
add_post_type_support( 'page', 'excerpt' );

After adding the code, you can see an excerpt box on the editorial page. If it’s not there, you can check the Screen options and see if the right checkbox is selected.

Updating the Default Gravatar

Update the look of your gravatar by replacing the mystery man with a custom branded gravatar to freshen up the look of your theme. Just place the following code and you are ready to go.

add_filter( 'avatar_defaults', 'newgravatar' ); function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif'; $avatar_defaults[$myavatar] = "your website or company name"; return $avatar_defaults; }
add_filter( 'avatar_defaults', 'newgravatar' );

function newgravatar ($avatar_defaults) {
  $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
  $avatar_defaults[$myavatar] = "your website or company name";
  return $avatar_defaults;
}

After this, make sure you upload the custom image to your theme’s folder. Also change the name on everything to make the gravatar compatible with your brand name.

Adding a Custom Dashboard Logo

Custom dashboard logo can truly breathe a new life to your theme. It’s very simple to achieve with the help of the following themes.

//hook the administrative header output add_action('admin_head', 'my_custom_logo'); function my_custom_logo() { echo ' '; }
//hook the administrative header output
add_action('admin_head', 'my_custom_logo');

function my_custom_logo() {
echo '
  <style type="text/css">
    #header-logo
      {background-image: url('
.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
  </style>
  '
;
}

Providing Support for Custom Post Formats

WordPress users often don’t understand the difference between post formats and post types. There are 9 post types included in WordPress, you can even see all of them on your post edit page. But before activating them, you need to provide them support in the functions.php file. This is how you can add support to custom post formats.

add_theme_support('post-formats', array( 'aside', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video', 'audio'));
add_theme_support('post-formats', array( 'aside', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video', 'audio'));

Customizing Footer

You can easily customize your footer to give it a unique personality of your brand. You can add the following code in the functions.php file and customize it easily.

function remove_footer_admin () { echo "add the text you want"; } add_filter('admin_footer_text', 'remove_footer_admin');
function remove_footer_admin () {
 echo "add the text you want";
}
add_filter('admin_footer_text', 'remove_footer_admin');

Creating a Maintenance Page

There are times when your website goes into some kind of maintenance or internal improvements for upgrades. At that time, you don’t want your website to be available for the general audience. For this you need to create a perfect maintenance mode page so that your visitors don’t feel annoyed. This is extremely simple with the help of the following code in your functions.php file.

function maintenance_mode() { if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {wp_die('Maintenance.');} } For removing the maintenance page, the below mentioned lines can be added: add_action('get_header', 'maintenance_mode');
function maintenance_mode() {
 if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {wp_die('Maintenance.');}
}

For removing the maintenance page, the below mentioned lines can be added:
add_action('get_header', 'maintenance_mode');

All said and done, this is how you can leverage the power of the functions.php file for planning and developing the potential of your WordPress site.

Related Posts

Written by Amy

Amy Brown is a WordPress developer by profession and a writer by hobby. She works for WordPrax Ltd.- Custom WordPress Development Company with a global reach. In case, you have made up your mind on creating a custom plugin, hiring the experts would make for a wise decision.

View all posts by: