All WordPress themes come with a powerful "functions.php" file. This file acts as a plugin and allows you to do a lot of interesting things on your WordPress site. In this article, we will show you some of the most useful tricks for your WordPress functions file.
You can easily change some default behaviors of WordPress by using the "functions.php" file. Adding features and functionality to any WordPress Website just by adding some PHP code. You can use it to call native PHP functions, WordPress functions, or to define your own functions.
There are a few differences between using a "plugin" and a functions.php file.
A WordPress plugin will execute only when individually activated via the Plugins panel. It will apply to all themes, requiring specific unique Header text. It is stored in a subdirectory.
The functions.php is a specially named PHP file that can be located inside any WordPress theme. Will add custom functions and feature to the theme and website. Each functions.php file belongs to its own theme. Any modifications to a functions PHP file will not carry over if you switch to another theme.
Each Theme has its own functions file, but only the functions.php with the "Active" Theme affects how your Website will publicly display. If your theme already has a functions file, you can add the below snippets to it. If not, you can create a plain-text file named functions.php to add to your theme's directory.
This code makes it easy. Create your favicon and upload to site’s root directory.
Change the "image" directory if needed. Be sure that the wp_head is present within your theme’s header.php file.
Add the following code in the <head> area in your functions.php file:
--------------------------------------------
--------------------------------------------
The footer in WordPress Admin area shows the message "Thank you for creating with WordPress". You can change the text and links to anything you want by adding this code.
-------------------------------------------
--------------------------------------------
Add a unique favicon to your Admin pages. Create a favicon and upload to your Theme’s "/images/" directory. Then add this code: Change the image directory to whatever is needed.
--------------------------------------------
--------------------------------------------
Tired of seeing the same "default" Avatar on your blogs? Replace it with your own branded custom avatars. Simply upload the image you want to use as the default avatar, and then add this code to your functions file.
--------------------------------------------
--------------------------------------------
Head to the "Settings" » "Discussion" page and select your default avatar.
For security reasons, you should always update to the latest version of WordPress. But if you want to remove the WordPress version number from your Website. Simply add this code to your functions file.
--------------------------------------------
-------------------------------------------
Want to change your WordPress Admin area? 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. Add this code to your theme’s functions file.
--------------------------------------------
--------------------------------------------
You can add whatever widget you want to your WordPress dashboard, simply by adding the following code:
--------------------------------------------
--------------------------------------------
You can 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. Use this code to add a dynamic copyright date in WordPress footer.
--------------------------------------------
--------------------------------------------
After adding this code, you will 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 shows the years wherever you call the function.
Randomly change the background color on your WordPress site with each visit and page reload. First add this code to your theme’s functions file.
--------------------------------------------
--------------------------------------------
You will need to edit the header.php file in your theme. Locate the <body> tag and add replace it with this line:
--------------------------------------------
If your WordPress login page keeps refreshing, or you are unable to access the Admin area. 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 non-editable. If you want to fix this, then you should add this code to your functions file. Replace example.com with your own domain name.
--------------------------------------------
--------------------------------------------
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.
WordPress automatically creates several image sizes when you upload an image. You can also create additional sizes to use in your Themes. This code creates three new images with different sizes. Change the code to meet your own requirements. You can display an image size anywhere in your theme using this code.
-------------------------------------------
--------------------------------------------
WordPress allows 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 the "Appearance" » "Menus" and you will see "My Custom Menu" as theme location option
You will need to add this code to your theme where you want to display a navigation menu.
--------------------------------------------
--------------------------------------------
Add extra fields to your "Author Profiles" in WordPress. As an example, this code, will add Twitter and Facebook fields to user profiles in WordPress, change it to suit your needs.
--------------------------------------------
--------------------------------------------
Display these fields in your "Author Template" like this:
------------------------------------------
This is one of the most used and many WordPress developers already know about this. For those who do not know, just paste the following code in your functions.php file:
-------------------------------------------
--------------------------------------------
Go to "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:
-------------------------------------------
-------------------------------------------
Have you seen websites that show their advertisement in their RSS Feeds below each post? You can accomplish this with a simple function:
--------------------------------------------
-------------------------------------------
The "Post" thumbnail or "Featured Image" are usually only displayed within your Website design. You can extend that functionality to your RSS feed with this function in your RSS feed.
--------------------------------------------
-------------------------------------------
Login errors could be used by Hackers to see if they entered a wrong user-name or password. By hiding these login errors, you can make your login area a bit more secure. Users will now see a generic "Something is wrong!" message when they enter an incorrect user-name or password. Change the message to what ever you like.
--------------------------------------------
-------------------------------------------
WordPress allows users to log in with a username or email address. You can disable the "Login by Email" by adding this code to your functions file.
--------------------------------------------
-------------------------------------------
By default, when you upload an image in WordPress it automatically links to the image file or the attachment page. If users click on the new image, they are then taken to a new page away from your post. Stop WordPress from automatically linking image uploads by adding this code snippet to your functions file:
-------------------------------------------
--------------------------------------------
Sometimes you may need to edit your articles before they go live, and are distributed to your RSS feed, and or email subscribers. In this code, we set it to 10 minutes as a "$wait or delay time". Feel free to change that into any number of minutes you want.
--------------------------------------------
-------------------------------------------
Change the text that appears after an "Excerpt". Simply add this code to your theme’s functions file.
--------------------------------------------
--------------------------------------------
If you would like to disable RSS feeds on your WordPress website, add this code to your theme’s functions file.
-------------------------------------------
--------------------------------------------
WordPress limits excerpt word lengths to 55. If you would like to change the length, add this code to your functions file.
Change 200 to the number of words you want to show in the excerpts:
-------------------------------------------
--------------------------------------------
Lost or forgotten your WordPress username, password and or email? You can add an "Admin Temporary User" by adding this code to your theme’s functions file by using an FTP client and simple text editor
Change the "Username", "Password", and "Email" fields to yours.
-------------------------------------------
--------------------------------------------
Once you log in to your WordPress site, do not forget to delete the code from your functions file.
The "Welcome Panel" is a meta box added to the dashboard screen of your Admin area. It provides useful shortcuts for beginners to do things on their new WordPress site. You can easily hide this by adding the below code in your functions file.
-------------------------------------------
--------------------------------------------
Do you want to show total number of registered users on your WordPress site? You need to add the following code to your theme’s functions.php file or a site specific plugin. Add this short code to your post or page where you want to show the total number of users.
-------------------------------------------
--------------------------------------------
This code creates short code [user_count] which you can use in your WordPress posts, pages, or a sidebar widget to display the user count. The function does not add any HTML formatting to the user count and simply returns the number. You may want to wrap the short code around HTML to use CSS or basic HTML formatting. For example:
Do you want to exclude specific categories from your WordPress RSS feed? Add this code to your theme’s functions file.
-------------------------------------------
--------------------------------------------
By default, WordPress does not execute short codes inside text widgets. To fix this, you need to simply add this code to your theme’s functions file.
-------------------------------------------
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 large pages. 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.
-------------------------------------------
--------------------------------------------
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.
WordPress can add all relevant feed links (main, comments, categories) to your "<head>" area. This will check to see if you’re using a version of WordPress that is compatible, and then enable the automatic feed links. Add the following snippet to make it work:
-------------------------------------------
--------------------------------------------
How to add jQuery by placing a little snippet from your theme’s functions.php file. This code ensures that only one copy of jQuery is included, and calls it from Google’s Servers. Note, this function needs to be located before the threaded-comments function in order for it to work.
-------------------------------------------
--------------------------------------------
Enabling threaded comments just by adding a snippet of code into the functions.php file. Note, This function needs to be located after the jQuery-inclusion function:
-------------------------------------------
--------------------------------------------
WordPress adds unnecessary text in the document <head>. Such as version numbers, WLW, RSD, and index links. To clean this up, add this snippet into the functions.php template:
------------------------------------------
--------------------------------------------
Add this code to the footer.php file. Note, replace the “UA-123456-1” with your actual GA code. There are three currently available Analytics options, and modify the code accordingly. This code is using the newer “ga.js” tracking code, Change to either of the other methods if using them.
-------------------------------------------
--------------------------------------------
One annoying thing that WordPress does is “Jump” the user to the location of the “<!--more-->” tag on the single-post-view when the “read more” link is clicked. If the jump was on the same page, yes. But to load a new page and then take the user halfway down the page? Add this function to stop the jumping:
-------------------------------------------
--------------------------------------------
The WordPress logo that is branding your various login pages? Change that to whatever you like. Create a custom log in image, name it “custom-login-logo.png”, and upload it to your theme’s /images/ directory. Then add this code:
-------------------------------------------
--------------------------------------------
Make sure that the path and image names match that of your setup. When creating your image keep in mind the properties of the original: 30px length, 31px height, transparent GIF format, and header background color of #464646.
A handy function for removing unwanted widget areas from your theme. Nice for customizing existing themes. Note, If you only want to disable widgets on your Home page, then remove the two comment slashes (“//”) from the second line.
-------------------------------------------
--------------------------------------------
This one is not for the average user. But if you really dislike the “Please update now..” message that appears on every page in the WordPress Admin when new versions are available? Add this code:
-------------------------------------------
--------------------------------------------
By default, WordPress body_class and post_class do not include the ID of the category of the current post. This custom function will add it:
-------------------------------------------
--------------------------------------------
RSH Web Service's WordPress Tutorials
Tweet Share Pin Email