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.
You would find many of these code snippets on websites like WPBeginner with instructions telling you to add this code in your themeβs functions.php file or a site-specific WordPress plugin.
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.
For detailed instructions, see our guide on the right way to remove WordPress version number.
2. Add a Custom Dashboard Logo
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.
For alternate methods and more details see our guide on how to add a custom dashboard logo in WordPress.
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.
Feel free to change the text and links that you want to add. Here is how it looks on our test site.
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:
This is how it would look like:
For details, see our tutorial on how to add custom dashboard widgets in WordPress.
5. Change the Default Gravatar in WordPress
Have you seen the default mystery man avatar on blogs? You can easily replace it with your own branded custom avatars. Simply upload the image you want to use as default avatar and then add this code to your functions file.
Now you can head over to Settings Β» Discussion page and select your default avatar.
For detailed instructions, see our guide on how to change the default gravatar in WordPress.
6. Dynamic Copyright Date in WordPress Footer
You can simply add copyright date by editing the footer template in your theme. However, it will not show when your site started and it will not automatically change next year.
You can use this code to add a dynamic copyright date in WordPress footer.
After adding this function, youβll need to open your footer.php file and add the following code wherever you like to display the dynamic copyright date:
This function looks for the date of your first post, and the date of your last post. It then echos the years wherever you call the function.
For more details, see our guide on how to add dynamic copyright date in WordPress.
7. Randomly Change Background Color in WordPress
Do you want to randomly change background color on your WordPress upon each visit and page reload? Here is how to easily do this.
First you need to add this code to your themeβs functions file.
Next, youβll need to edit the header.php file in your theme. Locate the <body>
tag and add replace it with this line:
You can now save your changes and visit your website to see this in action.
For more details and alternate methods, see our tutorial on how to randomly change background color in WordPress.
8. 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.
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.
9. 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.
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.
For detailed instructions, see our guide on how to create additional image sizes in WordPress.
10. 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.
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.
For detailed instructions, see our guide on how to add custom navigation menus in WordPress themes.
11. 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:
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:
You may also want to see our guide on how to add additional user profile fields in WordPress registration.
12. Adding Widget Ready Areas or Sidebar in WordPress Themes
This is one of the most used ones and many developers already know about this. But it deserves to be in this list for those who donβt know. Paste the following code in your functions.php file:
You can now visit Appearance Β» Widgets page and you will see your new custom widget area.
To display this sidebar or widget ready area in your theme add this code:
For more details see our guide on how to add dynamic widget ready areas and sidebars in WordPress.
13. Manipulate RSS Feed Footer
Have you seen blogs that adds their advertisement in their RSS Feeds below each post. You can accomplish that easily with a simple function. Paste the following code:
For more information, see our guide on how to add content and completely manipulate your RSS feeds.
14. Add Featured Images to RSS Feeds
The post thumbnail or featured images are usually only displayed within your site design. You can easily extend that functionality to your RSS feed with a simple function in your RSS feed.
For more details see our guide on how to add post thumbnails to your WordPress RSS feed.
15. Hide Login Errors in WordPress
Login errors in WordPress can be used by hackers to guess whether they entered wrong username or password. By hiding login errors in WordPress you can make your login area a bit more secure.
Now users see a generic message when they enter incorrect username or password.
For more information, see our tutorial on how to disable login hints in WordPress login error messages.
16. 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.
For more information see our guide on how to disable login by email feature in WordPress.
17. 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.
For more information, see our tutorial on how to disable search feature in WordPress.
18. Delay Posts in RSS Feed
Sometimes you may end up with a grammar or spelling mistake in your article. The mistake goes live and is distributed to your RSS feed subscribers. If you have email subscriptions on your WordPress blog, then those subscribers will get it as well.
Simply add this code in your themeβs functions file.
In this code we have used 10 minutes as $wait or delay time. Feel free to change that into any number of minutes you want.
For plugin method and more information, see our detailed guide on how to delay posts from appearing in WordPress RSS feed.
19. Change Read More Text for Excerpts in WordPress
Do you want to change the text that appears after the excerpt? Simply add this code to your themeβs functions file.
20. Disable RSS Feeds in WordPress
Not all websites need RSS feeds. If you want to disable RSS feeds on your WordPress site, then add this code to your themeβs functions file.
For a plugin method and more information, see our guide on how to disable RSS feeds in WordPress.
21. 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.
Change 100 to the number of words you want to show in the excerpts.
For alternate method, you may want to take a look at our guide on how to customize WordPress excerpts (no coding required).
22. 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.
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.
For more on this topic, take a look at our tutorial on how to add an admin user in WordPress using FTP.
23. Remove Welcome Panel from WordPress Dashboard
Welcome panel is a meta box added to the dashboard screen of WordPress admin area. It provides useful shortcuts for beginners to do things on their new WordPress site.
You can easily hide by adding this code in your functions file.
For other methods and more details check out our guide on how to remove welcome panel in WordPress dashboard.
24. Show Total Number of Registered Users in WordPress
Do you want to show total number of registered users on your WordPress site? Simply add this code to your themeβs functions file.
This code creates a shortcode that allows you to display total number of registered users on your site. Now you just need to add this shortcode to [user_count] your post or page where you want to show the total number of users.
For more information and a plugin method, see our tutorial on how to display total number of registered users in WordPress.
25. Exclude Specific Categories from RSS Feed
Do you want to exclude specific categories from your WordPress RSS feed? Add this code to your themeβs functions file.
26. Enable Shortcode Execution in Text Widgets
By default, WordPress does not execute shortcodes inside text widgets. To fix this you need to simply add this code to your themeβs functions file.
For an alternate method and more information, take a look at our guide on how to use shortcodes in WordPress sidebar widgets.
27. Add Odd and Even CSS Classes to WordPress Posts
You may have seen WordPress themes using an old or even class for WordPress comments. It helps users visualize where one comment ends and the next one begins.
You can use the same technique for your WordPress posts. It looks aesthetically pleasing and helps users quickly scan pages with lots of content. Simply add this code to your themeβs functions file.
This code simply adds an odd or even class to WordPress posts. You can now add custom CSS to style them differently. Here is a sample code to help you get started.
The end result will look something like this:
Need more detailed instructions? Take a look at our tutorial on how to add odd/even class to your post in WordPress themes.
28. 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:
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.
For more on this topic, check out our tutorial on how to add additional file types to be uploaded in WordPress.
29. Remove Default Image Links in WordPress
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:
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.
You may want to check out our tutorial on how to remove default image links in WordPress for an alternate plugin method and more information.
30. Add an Author Info Box in WordPress Posts
If you run a multi-author site and want to showcase author bios at the end of your post, then you can try this method. Start by adding this code to your functions file:
Next you will need to add some custom CSS to make it look better. You can use this sample CSS as an starting point.
This is how your author box would look like:
For plugin method and more detailed instructions, check out our article on how to add an author info box in WordPress posts.
31. 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:
1 | add_filter( 'xmlrpc_enabled' , '__return_false' ); |
You may want to read our article on how to disable XML-RPC in WordPress for more information.
32. Automatically Link Featured Images to Posts
If your WordPress theme does not automatically link featured images to full articles, then you can try this method. Simply add this code to your themeβs functions file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function wpb_autolink_featured_images( $html , $post_id , $post_image_id ) { If (! is_singular()) { $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>' ; return $html ; } else { return $html ; } } add_filter( 'post_thumbnail_html' , 'wpb_autolink_featured_images' , 10, 3 ); |
You may want to read our article on how to automatically link featured images to posts in WordPress.
Thatβs all for now.
We hope this article helped you learn some new useful tricks for functions.php file in WordPress. You may also want to see our ultimate guide to boost WordPress speed and performance.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.