The most powerful file in a WordPress theme is the functions.php. The purpose of this file is to allow theme developers to define theme features and functions. This file contributes to some of the unique functions of your WordPress installation, even if added as a plugin to your already existing theme.

In today’s post we’d like to share with you some of the most useful tricks for your WordPress functions file.

Randomly Change Background Color in WordPress

Add this code to your theme’s functions file.

function sow_bg() { 
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color ='#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].
$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo $color;
}

Edit the header.php file in your theme. Locate thetag and add replace it with this line: 

<body <?php body_class(); ?> style="background-color:<?php sow_bg();?>">>

You can now save your changes and visit your website to see this in action.

PC:http://www.wpbeginner.com

Keep logged in WordPress for a longer period

When you’re on your home network and your computer, getting kicked out after a while might be annoying to some. This code will do the trick, just open the functions.php file and copy it:

add_filter( 'auth_cookie_expiration', 'stay_logged_in_for_1_year' );
function stay_logged_in_for_1_year( $expire ) {
  return 31556926; // 1 year in seconds
}

BTW: By the default, WordPress will keep you logged in for 2 weeks when you check the “Remember Me” option at login.

Change the length of excerpts

By default the Excerpt length is intuitively capped at exactly 55 words. If you want to change it you can use following code:

function sow_new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'sow_new_excerpt_length');

Remove All Dashboard Widgets

function sow_remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes[‘dashboard’][‘side’][‘core’][‘dashboard_quick_press’]);
unset($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_incoming_links’]);
unset($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_right_now’]);
unset($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_plugins’]);
unset($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_recent_drafts’]);
unset($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_recent_comments’]);
unset($wp_meta_boxes[‘dashboard’][‘side’][‘core’][‘dashboard_primary’]);
unset($wp_meta_boxes[‘dashboard’][‘side’][‘core’][‘dashboard_secondary’]);
remove_meta_box( ‘dashboard_activity’, ‘dashboard’, ‘normal’ );
}
add_action(‘wp_dashboard_setup’, ‘sow_remove_dashboard_widgets’);

Add New Navigation Menus to Your Theme

Add this code in your theme’s functions file to define a new menu location in your theme.

function sow_custom_new_menu() {
  register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'sow_custom_new_menu' );

Add Author Profile Fields

You can easily to add extra fields to your author profiles in WordPress by adding this code to your functions file:

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

return $contactmethods;
}
add_filter('user_contactmethods','sow_new_contactmethods',10,1);

Hide Login Errors in WordPress

Login errors in WordPress can be used by hackers to guess whether they entered wrong username or password. So with following code you can make your login area a bit more secure.

function no_wordpress_errors(){
  return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

Disable RSS Feeds in WordPress

If you want to disable RSS feeds on your WordPress site, then add this code to your theme’s functions file.

function fb_disable_feed() {
wp_die( __('No feed available,please visit ourhomepage') );
}

add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);

Show Total Number of Registered Users

Add this code to your theme’s functions file if you want to show total number of registered users on your WordPress site.

// Function to return user count
function sow_user_count() { 
$usercount = count_users();
$result = $usercount['total_users']; 
return $result; 
} 
// Creating a shortcode to display user count
add_shortcode('user_count', 'sow_user_count');
function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;

// to error
if ( $error == true )
$query->is_404 = true;
}
}

add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

We hope this article helped you learn some new useful tricks for functions.php file in WordPress.