All WordPress themes come with a powerful functions.php file. This file acts as a plugin and allows you to do lots of cool things on your WordPress site. In this article, we will show you some of the most useful tricks for your WordPress functions file.

What is Functions File in WordPress?

Functions file commonly known as functions.php file is a WordPress theme file. It comes with all free and premium WordPress themes.

The purpose of this file is to allow theme developers to define theme features and functions. This file acts just like a WordPress plugin and can be used to add your own custom code snippets in WordPress.

Now you may be thinking what’s the difference between a site-specific WordPress plugin and functions.php file? Which one is better?

While functions.php file is more convenient, a site-specific plugin is much better. Simply because it is independent of your WordPress theme and would work regardless of which theme you are using.

On the other hand, a theme’s functions file will only work for that theme and if you switch the theme, then you will have to copy / paste your custom codes into the new theme.

Having said that, here are some extremely useful tricks for the WordPress functions file.

1. Remove WordPress Version Number

You should always use the latest version of WordPress. However, you may still want to remove the WordPress version number from your site. Simply add this code snippet to your functions file.

function wps_remove_version() {
    return '';
}
add_filter('the_generator', 'wpsc_remove_version');

Want to white label your WordPress admin area? Adding a custom dashboard logo is the first step in the process.

First you’ll need to upload your custom logo to your theme’s images folder as custom-logo.png. Make sure your custom logo is 16×16 pixels in size.

After that you can add this code to your theme’s functions file.

function wpsc_custom_logo() {
echo '
<style type="text/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-logo.png) !important;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
background-position: 0 0;
}
</style>
';
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpsc_custom_logo');

3. Change the Footer in WordPress Admin Panel

The footer in WordPress admin area shows the message ‘Thank you for creating with WordPress’. You can change it to anything you want by adding this code.

function wpsc_remove_footer_admin () {

echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Expert: <a href="http://www.sukhwantcheema.com" target="_blank">Sukhwant Cheema</a></p>';

}
add_filter('admin_footer_text', 'wpsc_remove_footer_admin');

4. Add Custom Dashboard Widgets in WordPress

You probably have seen widgets that numerous plugins and themes add in the WordPress dashboard. As a theme developer, you can add one yourself by pasting the following code:

add_action('wp_dashboard_setup', 'wpsc_custom_dashboard_widgets');

function wpsc_custom_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('wpsc_custom_help_widget', 'Widget Title here', 'wpsc_custom_dashboard_help');
}

function wpsc_custom_dashboard_help() {
echo '<p>Welcome message here! Need help? Contact the developer <a href="mailto:sukhwant86@hotmail.com">here</a>. For Hiring WordPress Expert visit: <a href="http://www.sukhwantcheema.com" target="_blank">Sukhwant Cheema</a></p>';
}

5. Update WordPress URLs

If your WordPress login page keeps refreshing or you are unable to access admin area, then you need to update WordPress URLs.

One way to do this is by using wp-config.php file. However, if you do that you will not be able to set the correct address on the settings page. The WordPress URL and Site URL fields will be locked and uneditable.

If you want to fix this, then you should add this code to your functions file.

update_option( 'siteurl', 'http://example.com' );
update_option( 'home', 'http://example.com' );

Don’t forget to replace example.com with your own domain name.

Once you are logged in, you can go to Settings and set the URLs there. After that you should remove the code you added to the functions file, otherwise it will keep updating those URLs any time your site is accessed.

6. Add Additional Image Sizes in WordPress

WordPress automatically creates several image sizes when you upload an image. You can also create additional image sizes to use in your theme. Add this code your theme’s functions file.

add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

This code creates three new image sizes with different sizes. Feel free to tweak the code to meet your own requirements.

You can display an image size in anywhere in your theme using this code.

<?php the_post_thumbnail( 'homepage-thumb' ); ?>

7. Add New Navigation Menus to Your Theme

WordPress allows theme developers to define navigation menus and then display them. Add this code in your theme’s functions file to define a new menu location in your theme.

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

You can now go to Appearance » Menus and you will see ‘My Custom Menu’ as theme location option.

Now you need to add this code to your theme where you want to display navigation menu.

<?php
wp_nav_menu( array(
'theme_location' => 'my-custom-menu',
'container_class' => 'custom-menu-class' )
);
?>

8. Add Author Profile Fields

Do you want to add extra fields to your author profiles in WordPress? You can easily do that by adding this code to your functions file:

function wpsc_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','wpsc_new_contactmethods',10,1);

This code will add Twitter and Facebook fields to user profiles in WordPress.

You can now display these fields in your author template like this:

<?php
// get author object
$author = get_the_author();
echo $author->twitter;
?>

9. Disable Login by Email in WordPress

WordPress allows users to login with username or email address. You can easily disable login by email in WordPress by adding this code to your functions file.

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );

10. Disable Search Feature in WordPress

If you want to disable search feature on your WordPress site, then simply add this code to your functions file.

function wpsc_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', 'wpsc_filter_query' );

function wpsc_search_form( $html ) {
return "";
}
add_filter( 'get_search_form', 'wpsc_search_form', 10, 1 );

11. Change Excerpt Length in WordPress

WordPress limits excerpt lengths to 55 words. If you need to change that, then you can add this code to your functions file.

function wpsc_new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'wpsc_new_excerpt_length', 10, 1);

Change 100 to the number of words you want to show in the excerpts.

12. Add an Admin User in WordPress

If you have forgotten your WordPress password and email, then you can add an admin user by adding this code to your theme’s functions file using an FTP client.

function wpsc_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = 'email@domain.com';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}
add_action('init','wpsc_admin_account');

Don’t forget to fill in the username, password, and email fields. Once you login to your WordPress site, don’t forget to delete the code from your functions file.

13. Add Additional File Types to be Uploaded in WordPress

By default, WordPress allows you to upload a limited number of most commonly used file types. However, you can extend it to allow other file types. Add this code to your theme’s functions file:

function wpsc_my_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml'; //Adding svg extension
$mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
return $mime_types;
}
add_filter('upload_mimes', 'wpsc_my_myme_types', 10, 1);

This code allows you to upload SVG and PSD files to WordPress. You will need to Google to find out the mime types for the file types you want to allow and then use it in the code.

By default, when you upload an image in WordPress it is automatically linked to the image file or the attachment page. If users click on the image they are then taken to a new page away from your post.

Here is how you can easily stop WordPress from automatically linking image uploads. All you have to do is to add this code snippet to your functions file:

function wpsc_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );

if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpsc_imagelink_setup', 10);

Now when you upload a new image in WordPress, it will not be automatically linked. You can still link it to the file or attachment page if you want.

15. Disable XML-RPC in WordPress

XML-RPC is a method that allows third party apps to communicate with your WordPress site remotely. This could cause security issues and can be exploited by hackers.

Simply add this code to your functions file to turn off XML-RPC in WordPress:

add_filter('xmlrpc_enabled', '__return_false');

While the above solution is sufficient for many, it can still be resource intensive for sites that are getting attacked.

In those cases, you may want to disable all xmlrpc.php requests from the .htaccess file before the request is even passed onto WordPress. The .htaccess in your site’s root folder.

Simply paste the following code in your .htaccess file:

# Block WordPress xmlrpc.php requests
<Files xmlrpc.php>
order deny,allow
deny from all
allow from 123.123.123.123
</Files>