As with many others who are now viewing this post, I have been reading various blogs, forums, and discussion groups to learn and improve my WordPress skills. Over the past 12 months I have been on a mission to substitute my use of plugins by adding code to my functions.php
file instead.
While I completely agree that plugins are very useful in many situations, my experience proved that in 90% of usage cases although a plugin might exist, actually utilizing it could create unnecessary complications and compatibility issues. Additionally in a great deal of cases such plugins added menus and other admin elements which I don’t want or need.
More often than not I have found that by analyzing the code of plugins I was able to strip out the piece of code I wanted and hard code it into my functions.php
. This provided me with the exact functionality I needed without having to include unnecessary elements.
So, the purpose of this post is my attempt to engage you, the reader/admin/developer, to share with me and other here any code bits which you find useful and have added to your theme’s function.php
file to extend or enhance WordPress without utilizing a plugin.
When you submit a response here please kindly give each code bit a title, let us know if with what version of WordPress you know its compatible with, include whatever description you feel best describes its function and (if applicable) include a link to the original plugin or source where you found the information.
I am looking forward to all your responses and will of course continually add my own new finds whenever I find them.
Please vote on the question and any answers you find useful by clicking on the up arrow on the left hand side of the question or answer.
Enable Hidden Administration Feature displaying All Site Settings
Tested on: WordPress 3.1 RC3
This little piece of code does something pretty cool. It will add an additional option to your settings menu with a link to “all settings” which will show you a complete list of all the settings you have within your database related to your WordPress site. The code below will only made this link visible to an administrator user and hide it for all other users.
Modify the Login Logo & Image URL Link
Tested on: WordPress 3.0.1
This code will allow you to easily modify the WordPress Login page Logo as well as the href link and title text of this logo.
EDIT: If you want to use the site logo to replace the login logo, you can use the following to dynamically pull that information (tested on WP3.5):
Include custom post types in the search results.
Add your custom post types to your sites main RSS feed by default.
Include custom post types in “Right Now” admin dashboard widget
This will include your custom post types and the post counts for each type in the “Right Now” dashboard widget.
Remove Update Notification for all users except ADMIN User
Tested on: WordPress 3.0.1
This code will ensures that no users other than “admin” are notified by WordPress when updates are available..
Changed version to only show update notification for admin users (as opposed to just the user ‘admin’):
Loading jQuery from the Google CDN
Tested on: WordPress 3.0.1
Remove the WordPress Version Info for Security
Tested on: WordPress 3.0.1
Add Spam & Delete Links to Comments on Front End
Tested on: WordPress 3.0.1
This makes it way easier to manage comments from the front end by adding spam and delete links.**
Delay the public posting to RSS Feed
Tested on: WordPress 3.0.1
Finally, I like to delay posting to my RSS feeds for 10-15 minutes because I always find at least a couple errors in my text. Other uses are in case you want content to be exclusive to your site for a day or a week before pushing it out to your RSS readers.
Set a maximum number of post revisions to avoid DB bloat.
Tested on: WordPress 3.0.1
The default is infinite, and this will set it to only remember the last five edits:
For what it’s worth, there are a ton of great ideas for CONSTANTS that can be set on the Codex page Editing wp-config.php.
WordPress Profiling tools
I like to add profiling tools in a separate file, which I then include from functions.php when needed:
Sharpen Resized Images (only JPEG)
This function sharpens resized JPEG images. An example of a difference:
Remove Default WordPress Meta Boxes
Tested on: WordPress 3.0.1
This code will allow you to remove specific Meta Boxes which WordPress adds by default to the default Add/Edit Post and Add/Edit Page screens.
Remove “WordPress” to “WordPress” filter
Tested on: WordPress 3.0.1
There was a filter added with WordPress version 3.0 that automatically converts all instances of “WordPress” (no capital P) to “WordPress” (with a capital P) in post content, post titles, and comment text. Some people see this as intrusive, but I just have a need to mis-case WordPress from time to time and found the filter somewhat annoying.
Customize the Dashboard
Remove these dashboard widgets…
Add a custom widget called ‘Help and Support’
This is the content for your custom widget
Add Custom User Profile Fields
Place the code below into your functions.php file to add custom user profile fields. Edit or add lines as you see fit.
Remember not to remove the line: return $contactmethods; otherwise this won’t work.
To display custom fields you can use one of the two methods listed below.
Option 1:
Option 2:
Function to change the length of Exerpt
Tested on: WordPress 3.0.1
By default all excerpts are capped at 55 words. Utilizing the code below you can override this default settings:
This example changes the excerpt length to 100 words, but you can use the same method to change it to any value.
Customize the order of the administration menu
Tested on: WordPress 3.0.1
This code will allow you to reorganize the order of elements in the administration menu. All that you need to do is click on an existing link in the administration menu and copy everything before the /wp-admin/ URL. The order below represents the order the new administration menu will have.
Add Thumbnails in Manage Posts/Pages List
You can add this to your functions to display to the Manage/Edit Post and Pages List a new column with the thumbnail preview.
Remove pings to your own blog
Tested on: WordPress 3.0.1
Enable GZIP output compression
Normally the server should be set up to do this automatically, but a lot of shared hosts donât do this (probably to increase client bandwidth usage).
Display DB Queries, Time Spent and Memory Consumption
Tested on: WordPress 3.0.1
Then this code below the code above which will automatically insert the code above into the footer of your public website (make sure your theme is calling
wp_footer
):It can be called multiple times.
Unregister WordPress Default Widgets
Tested on: WordPress 3.0.1