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.

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.

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

13

Considering the first 5 answers were by the OP and the question seems more geared at collecting an array of responses rather than a single, definitive answer, this should be a community wiki.
– EAMann♦Sep 9 '10 at 14:43

17

All answers not related to a theme should be removed. This thread is a good example for bad coding practices.
– fuxia♦Jun 13 '11 at 12:09

@NetConstructor.com The pure number of page views is not an indicator for quality. We should encourage specific questions with specific answers and good coding practices. This thread is the opposite.
– fuxia♦Feb 22 '12 at 2:44

6

@NetConstructor.com Discuss it on Meta where people can see your arguments better. :)
– fuxia♦Feb 24 '12 at 17:10

108 Answers
108

Enable Hidden Admin 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 admin user and hide it for all other users.

Fantastic for development! I use the options table frequently to store DB versions for my plug-ins ... using phpMyAdmin to reset to an old DB version to test an upgrade script is a pain ... this will make it so much easier!!!
– EAMann♦Sep 15 '10 at 16:03

3

You can also get to the same options page (when logged in) by going to yoursite/wp-admin/options.php
– j08691Oct 9 '15 at 19:55

Regarding the last snippet to this answer. This is a great addition as I was adding these manually for each post type. The only issue I have with this is that it adds the data after the default "category" and "tag" entry. Could you update your answer to move the default "category" or "tag" ones down or remove them so that they can be added manually?
– NetConstructor.comFeb 22 '11 at 20:06

@NetConstructor.com I don't think I understand your request. If I do, then I think it would be a bit more of a difficult thing to do, and don't really have time right now to figure out how to do it.
– jaredwilliFeb 23 '11 at 1:01

Include custom post types in the search results - I guess, now you can do this with exclude_from_search param of register_post_type...
– Krzysiek DróżdżDec 16 '13 at 16:13

This is far less than ideal. It will only work if the admin's login is still the default 'admin', which it shoudln't be for security reasons. Instead you should check for a specific capability that you want people to have to see messages.
– jerclarkeSep 11 '10 at 20:28

Thats why I added the comment to the code where you can change the admin username. How would you improve/rewrite it?
– NetConstructor.comSep 12 '10 at 9:07

The best way is to remove the global $user_login and get_currentuserinfo() and instead use current_user_can in your if clause. It's only 1 line instead of 3 and its the standard way. You can check for the specific capability that would be needed to ACT on the messages, in this case there's 'update_core' and 'update_plugins'.
– jerclarkeSep 12 '10 at 14:00

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.

The "Remove the WordPress Version Info for Security" code doesn't actually do anything to increase the security of your site. It doesn't even stop exposure of the WP version being used on your site.
– Joseph ScottFeb 11 '11 at 6:38

1

Not true Joseph, if your WordPress version is exposed then people can see if you are running an older version, thus exposing your vulnerabilities. It is always a good decision to remove that from all WordPress installs. Personally, I don't even know why they put it there in the first place as it IS a security issue.
– JeremyMay 12 '14 at 15:51

Looking at its usage in wp_save_post_revision() there doesn't seem to be a way to distinguish based on post types. There's no filter or anything on the value, though there probably should be.
– jerclarkeSep 12 '10 at 14:24

thanks Jeremy - To anyone else, if you know how to do this please post it here.
– NetConstructor.comSep 14 '10 at 7:18

1

personally I prefer 10. I know it's double but always when I need a revision it's always older then 5
– janwSep 4 '12 at 12:04

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, I just have a need to mis-case WordPress from time to time and found the filter somewhat annoying.

Customize the order of the admin menu

tested on: Wordpress 3.0.1

This code will allow you to reorganize the order of elements in the admin menu. All that you need to do is click on an existing link in the admin menu and copy everything before the /wp-admin/ URL. The order below represents the order the new admin menu will have.

I actually have that issue quite often. If I reference an internal link to another post on my WP blog, I get a trackback or pingback (don't remember which) from myself. It's annoying.
– Sahas KattaDec 18 '10 at 23:12

Same here. I have a news / magazine blog and link to other articles quite often.
– StevenFeb 13 '11 at 13:24

correct but this one works differently and fixes various issues which get_the_image does not take into account
– NetConstructor.comSep 30 '10 at 11:19

3

What does it do differently than the get_the_image script?
– mattDec 5 '10 at 4:47

1

@matt -- In wordpress there are different ways that images can be added to posts and i think the get_the_image script just looks at one of those. This checks to see if there is a featured image and uses that one first if available, next I think it checks for the first image added to the post content and if that is not found it checks the media gallery for the image with the highest sort order (at least thats how I remember the order going).
– NetConstructor.comFeb 22 '11 at 20:01

could you please split up each of these into seperate answers within the next few weeks. I was going to do it for you but did not want to make it seem like I am taking credit for your answers. In any case - I am trying to keep this organized so users can easily find the information they are looking for. Thanks in advance
– NetConstructor.comJan 26 '11 at 14:25

I was just using the code "Restrict ADMIN menu items based on username, replace username with an actual user's name" which is great but could you update the code to also show how this can be done for a specific "user role". I think this would be very useful!
– NetConstructor.comFeb 22 '11 at 20:13

Sorry NetConstructor I just saw your comment now. For user role I would use "current_user_can". I don't have time to test it but when I do, I will add it.
– WyckMar 4 '11 at 3:47

Remove Plugin Update Notice ONLY for INACTIVE plugins

function update_active_plugins($value = '') {
/*
The $value array passed in contains the list of plugins with time
marks when the last time the groups was checked for version match
The $value->reponse node contains an array of the items that are
out of date. This response node is use by the 'Plugins' menu
for example to indicate there are updates. Also on the actual
plugins listing to provide the yellow box below a given plugin
to indicate action is needed by the user.
*/
if ((isset($value->response)) && (count($value->response))) {
// Get the list cut current active plugins
$active_plugins = get_option('active_plugins');
if ($active_plugins) {
// Here we start to compare the $value->response
// items checking each against the active plugins list.
foreach($value->response as $plugin_idx => $plugin_item) {
// If the response item is not an active plugin then remove it.
// This will prevent WordPress from indicating the plugin needs update actions.
if (!in_array($plugin_idx, $active_plugins))
unset($value->response[$plugin_idx]);
}
}
else {
// If no active plugins then ignore the inactive out of date ones.
foreach($value->response as $plugin_idx => $plugin_item) {
unset($value->response);
}
}
}
return $value;
}
add_filter('transient_update_plugins', 'update_active_plugins'); // Hook for 2.8.+
//add_filter( 'option_update_plugins', 'update_active_plugins'); // Hook for 2.7.x

This is not necessarily a good idea - an inactive plugin is still present in the filesystem, and an insecure one can still be utilised to hack the site. Plugins should always be kept up-to-date.
– Tim MaloneApr 11 '16 at 20:45

Enable Error Debugging And Logging To Use On Live Sites

This is a piece of code I wrote to make use of the WP_DEBUG constants that are normally disabled by default. Well I created a way to not only enable WP_DEBUG so you can use it on a live site with no negative side-effects, but I also made use of the other debugging constants for forcing errors to be displayed, and for creating a log file of the errors and Notices in the /wp-content directory.

Drop this code in your wp-config.php file ( AFTER YOU SAVE A BACKUP JUST IN CASE ) and then you can pass the ?debug=1, 2, or 3 parameters at the end of any url on your site.

?debug=1 = shows all errors/notices
?debug=2 = forces them to be displayed
?debug=3 = creates a debug.log file of all errors in /wp-content dir.

It's sometimes handy to add/remove from an existing role rather than removing and re-adding one. Again, you only need to uncomment it, reload a page and then comment it again. This will store the role/capability properly in the options table. (This allows you, the developer to control them and removes the overhead of the bulky plugins that do the same thing.) Here I'm changing the author role to delete their published posts (the default), but allowing them the capability to edit their published posts (which isn't possible for this role by default)-- using *add_cap* or *remove_cap*.

I keep a spreadsheet with the grid from the Codex page for sites that modify this way, so I can remember how things are set, though leaving the commented out code in your functions.php file will work to. Don't leave these examples uncommented, or it will write to the database with each page load!

The functions I mention above write to a field in the options database. Commenting and uncommenting them is the way to go. There are plugins for user roles, but if you use the functions mention above, you cannot leave these functions running, and you DO NOT need to set them more than once, or set them based on if a specific user is accessing something. If you want that, set that user up with a specific, unique role. And refer to the codex, everything I write above is 100% correct if you do it without a plugin. For nearly every case, you only need to set the user roles once.
– tomcat23Feb 2 '11 at 8:58

@tomcat23: To illustrate, i wrapped it up in a function to only add the role, when it doesn't already exists. Another note: I guess it would be easier to place the role somewhere in the role hierarchy, with retrieving the caps from some built in role and then add/remove the capabilities from the built in role. Would make it more clear and easier to remember if it's caps are placed somewhere between ex. admin and editor. - I hope you don't mind that i edited your answer. If you do, pls role it back. :)
– kaiser♦Mar 1 '11 at 16:25

1

@tomcat23 - Water under the bridge at this point. All I'm saying is, I'm not interested in placing blame, just in having peace for everyone moving forward. :)
– MikeSchinkelMar 13 '11 at 6:55

Can't this already be edited on the PHP side so it doesn't get output at all?
– hakreFeb 14 '11 at 19:13

It's working fine here in 3.0+ versions for sure, but why not in older versions? Check if any other plugin you use is responsible for this. The text here replaced with JQuery, maybe a JQuery plugin?
– PhilipFeb 14 '11 at 20:25