Search
Close this search box.

10 WordPress Functions File Hacks That Are Super Useful

[updated_on]
Share this post:

The theme folder, especially the WordPress functions.php file, is where a lot of action is, in terms of defining, editing and setting on and off a plethora of functionalities. The file itself works as most plug-ins, modifying some of the core features, without supplementary editing. In terms of development time, it’s a Godsend, as you can pack all of your modifications in one file, cutting a lot of corners. So, below we’ve selected 10 of what we think are the most important features that you might want to tackle via this file.

Table of Contents

1. Add Favicon
2. Customized Dashboard Logo
3. Personalized Dashboard Widgets
4. Change Gravatar
5. Add Post Thumbnails
6. Custom Author Fields
7. Threaded Comments
8. Remove Login Error Message
9. Adsense Shortcode
10. PayPal Donation Shortcode

1. Adding An Icon (Favicon)

You can elect to give your blog a unique icon. The way to do it is to either add the favicon code to the header .php or by adding the following lines of code to the functions file:

// add your own favicon to the blog 

blog_favicon() {
  echo '';
}

add_action('wp_head', 'blog_favicon');

What's a Favicon
This is a Favicon


The only thing that remains to be done is upload the .ico file to the root folder of your website and you’re done.

2. Add Your Own Customized Dashboard Logo

This one simply adds a dashboard logo to your theme, and while it’s simply esthetic, it is very appealing. To add the functionality, use the following code:

//hook the administrative header output

function my_custom_logo() {
  echo ''; add_action('admin_head', 'my_custom_logo');

3. Adding personalized dashboard widgets to WordPress

WordPress themes do add certain widgets, but you can take it further and add your very own; Use the following code to get it:

function my_custom_dashboard_widgets() {
  global $wp_meta_boxes;
  wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help');
}

function custom_dashboard_help() {
  echo '

Welcome To This Custom Theme! Need help? Contact the developer here. For WordPress Tutorials visit: WP Learning Lab

';
}

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');


Note: you have to change the email address to the one you’re going to use.

4. Change the Gravatar

Changing up the Gravatar icon is yet another way to further personalize your blog and give it a unified look. To do so, paste the following code into your functions.php file:

function newgravatar ($avatar_defaults) {
  $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
  $avatar_defaults[$myavatar] = "WPBeginner";
  return $avatar_defaults;
}

add_filter( 'avatar_defaults', 'newgravatar' );


Next up, make sure you upload the image of your choice to the theme image folder and change up the name as you see fit. You can then go into the WP-Admin » Settings » Discussion and select the Gravatar of your choice from there.

5. Adding post thumbnails

Ever since the WordPress 2.9 version was released thumbnails can be set on and off via the functions .php file. There is only one line of code to use:

add_theme_support( 'post-thumbnails' ); 

Inside the loop where you want the thumbnail displayed add:



You’ll see the thumbnail in the post, as soon as the blog is refreshed.

6. Custom author profile field

The author page is initially not that versatile. However, it can be modified to also include a Twitter and a Facebook account fields, or you can create your very own personal fields. The code to use is the following:

function my_new_contactmethods( $contactmethods ) {
  // Add Twitter
  $contactmethods['twitter'] = 'Twitter';
  //add Facebook
  $contactmethods['facebook'] = 'Facebook';
  return $contactmethods;
}

add_filter('user_contactmethods','my_new_contactmethods',10,1);

You will then get the additional fields, ready to be used when defining an author page. Additional fields can be added via:

twitter; ?>


Where Twitter can be defined as any other variable. Note though that this will only work for WordPress 2.9 or more recent.

7. Threaded comments

A really cool feature in WordPress, that makes comment viewing a lot easier can be achieved via the threaded comments enabling in your administrative area, plus some Java script. However, a much easier way to get this done is add into your Functions file, the following:

// enable threaded comments
function enable_threaded_comments(){
  if (!is_admin()) {
  if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
    wp_enqueue_script('comment-reply');
  }
}

add_action('get_header', 'enable_threaded_comments');


There, threaded comments, for a much better comment viewing experience!

8. Removing the error messages pop up in the login page

Maybe you know that you have some errors in your WordPress, but they’re not that important and you want to ignore them. If you don’t want to get them in your login page, simply add the following code:

add_filter('login_errors',create_function('$a', "return null;"));


Instead of the error messages greeting you, you get a clean empty box just above the login prompts.

9. Enabling Adsense shortcode functionality

Generally, most WordPress themes allow Adsense placement on the blog, but the placement might be limited to certain areas only. If you want to place the Adsense anywhere on the page, you can do so by including the following code into the functions file:

function showads() {
  return '
'; } add_shortcode('adsense', 'showads');

10. PayPal donations shortcode

One way to keep a blog going is to ask for donations, and with the following code you can enable this function easily. What it dies, precisely, is allow you to plugin your PayPal donations into any post, at will; Use the following code:

function donate_shortcode( $atts ) {
  extract(shortcode_atts(array(
  'text' => 'Make a donation',
  'account' => 'REPLACE ME',
  'for' => '',
  ), $atts));
  global $post;
  if (!$for) $for = str_replace(" "," ",$post->post_title);
  return ''.$text.'';
}

add_shortcode('donate', 'donate_shortcode');

Obviously you’ll need to update the account information in the example above. Sources for code: Smashing Magazine, Digging into WordPress, Yoast and WPBeginner

Do you have any favorite WordPress functions file hacks that we missed? Mention them in the comments below and we’ll add them to the list!

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *

WPLearningLab