One of the things I love about WordPress is the easiness to empower it with (almost) endless features thanks to plugins. With 38000+ free plugins and an uncountable number of premium and custom ones, there’s a WordPress plugin for every need and budget. And if you look online to get WordPress tips, tricks or hacks, you'll surely end on a blog post that’s covering a list of (great) plugins that will provide you with what you were looking for. Actually, it's quite difficult to find a great list of WordPress tips, tricks and hacks that doesn't count a plugin in it.

I know, they are powerful tools, but plugins aren't always required to enable a specific feature because WordPress alone can do cool things too.

So my question is: how many interesting features can be enabled on a WordPress website without installing a single plugin? I've put together a list of the best WordPress code snippets (or WordPress hacks, you choose) that you can play with right away and see for yourself you don't always need a plugin.

The following piece of codes can break your website, so please pay attention and make a backup before getting your hands dirty with them.

WordPress Code Tweaks: Admin and dashboard

Change the login logo with yours

Let's start with one about branding: if you ever wanted to change the WordPress logo on the login page with yours (or a client's), that's the code you'll need. It's easy: just open your functions.php file and paste the following:

function my_custom_login_logo() {
    echo '<style type="text/css">
        h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; }
    </style>';
}

add_action('login_head', 'my_custom_login_logo');

Reinforcing your brand communication with this little tweak is just the first step, you'll see how many things you can do without installing plugins. [via]

Have you ever wanted to "feel" the WordPress dashboard closer to your business? How about featuring a new client's logo in there? That would be cool but installing a new plugin just for that sounds like too much. How about I tell you can do this with no plugin at all? Open the functions.php file and paste this:

function custom_admin_logo() {
  echo '<style type="text/css">
          #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/admin_logo.png) !important; }
        </style>';
}
add_action('admin_head', 'custom_admin_logo');

You now should put your logo in the wp-images folder and call it admin_logo.png. [via]

Disable WordPress Login Hints

Keeping your WordPress website secure is important (see how to improve it), and there's a little thing that will make a hacker's life more difficult: not providing detailed error messages on the WordPress login page. To disable these warnings you need to open your functions.php and copy this:

function no_wordpress_errors(){
  return 'GET OFF MY LAWN !! RIGHT NOW !!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

This way you no longer provide useful hints that could be exploited for malicious activities. [via]

Keep logged in WordPress for a longer period

When you work on public wi-fi networks, or not on your computer, it's always good practice to log out from those devices. But when you're on your home network and your computer, getting kicked out after a while might be annoying to some. How about extending the time of your WordPress login session? This code will do the trick, just open the functions.php file and copy it:

add_filter( 'auth_cookie_expiration', 'stay_logged_in_for_1_year' );
function stay_logged_in_for_1_year( $expire ) {
  return 31556926; // 1 year in seconds
}

By the default, WordPress will keep you logged in for 2 weeks when you check the "Remember Me" option at login. You can set the expiry date of the authorization login cookie by replacing the "31556926" with your preferred time span. [via]

Replace "Howdy" with "Logged in as" in WordPress bar

If the default "Howdy" is too informal or you just want another message on the WordPress menu bar, head over to your functions.php and add this:

function replace_howdy( $wp_admin_bar ) {
    $my_account=$wp_admin_bar->get_node('my-account');
    $newtitle = str_replace( 'Howdy,', 'Logged in as', $my_account->title );
    $wp_admin_bar->add_node( array(
        'id' => 'my-account',
        'title' => $newtitle,
    ) );
}
add_filter( 'admin_bar_menu', 'replace_howdy',25 );

You just need to insert your new message as the 2nd element within the $newtitle array, and you're done. [via]

Change the footer text on WordPress dashboard

Branding is about messaging and consistency. So if you're building up a website for a client, you'd want them to feel happy about what you did and feel important too. So, besides having their logo on their login page and dashboard, how about adding their tagline or some cool text on the dashboard footer too? Cool, just open up the functions.php file and go with this:

function remove_footer_admin () {
  echo "Your own text";
} 

add_filter('admin_footer_text', 'remove_footer_admin');

Now it's all about your creativity to impress them, make them smile, or even quote some motivational words. [via]

Add a shortcode to widget

Shortcodes are super-useful because they can just replace what that could be a long piece of code with a little line between square brackets. By default, WordPress widgets aren't enabled to manage shortcodes and they handle them like regular text. But there's the possibility to empower WordPress widgets with the ability to use shortcodes as well thanks to the following piece of code added in the functions.php file:

add_filter('widget_text', 'do_shortcode');

This code tweak will make you take advantage of shortcodes on other great tools WordPress comes with: widgets. [via]

7 WordPress admin and dashboard hacks you can do without plugins Click To Tweet

WordPress Code Tweaks: Posts and Pages

Require a featured image before you can publish posts

Publishing a blog post or a page on WordPress is easy but in many cases words alone aren't enough: news, product pages, etc. they all need images to stand out and communicate better. So how about making it mandatory for your users to add a featured image to their post or page unless they aren't able to publish it? Great, open functions.php and apply the following code:

add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');

function wpds_check_thumbnail( $post_id ) {
  // change to any custom post type 
  if( get_post_type($post_id) != 'post' )
      return;

  if ( ! has_post_thumbnail( $post_id ) ) {
    // set a transient to show the users an admin message
    set_transient( "has_post_thumbnail", "no" );
    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'wpds_check_thumbnail');
    // update the post set it to draft
    wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));

    add_action('save_post', 'wpds_check_thumbnail');
  } else {
    delete_transient( "has_post_thumbnail" );
  }
}

function wpds_thumbnail_error() {
  // check if the transient is set, and display the error message
  if ( get_transient( "has_post_thumbnail" ) == "no" ) {
    echo "<div id='message' class='error'><p><strong>You must add a Featured Image before publishing this. Don't panic, your post is saved.</strong></p></div>";
    delete_transient( "has_post_thumbnail" );
  }
}

Isn't this useful? Of course, you can set your custom alert message by editing whatever fits your needs. [via]

Reduce Post Revisions

Revisions are the WordPress built-in time machine to the edits of your content. By default, there's no limit to the number of post revisions that are stored in your database, but "infinite" is a huge number that hardly would be useful to the purpose of your website. That's why you can set a specific number of revisions you want to be saved. For this tweak, you should open the wp-config.php file and add:

define( 'WP_POST_REVISIONS', 3 );

Pick a number that works for you and put it in it. If you want to disable the storage of revisions (and just have the autosave), use "-1" instead.[via]

Delay posting to my RSS feeds for 60 minutes

Imagine this: you just published the latest post on your blog and the RSS feed is already sending it to your subscribers, when you see there's a typo in the headline. That's a bummer, if only you had some more time to check everything out better... With this piece of code you can! Specifically, you delay the posting to your RSS feeds so you'll have enough time to check for the last time. If you like the idea, open the functions.php file and add this:

function Delay_RSS_After_Publish($where) {
  global $wpdb;

  if (is_feed()) {
    $now = gmdate('Y-m-d H:i:s');
    $wait = '60';
    $device = 'MINUTE';
    $where.=" AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
  }
  return $where;
}

add_filter('posts_where', 'Delay_RSS_After_Publish');

This is useful to check if there are typos, broken links etc. To change the delayed timeframe, just edit the $wait = '60'; with another value that better works for you. [via]

Change the length of excerpts

For some scenarios, the default excerpts don't fit the layout, so you need to change it accordingly. If that's what you need, with your functions.php file opened, copy in it the following code:

function custom_excerpt_length( $length ) {
  return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Tweak the "return 20" line by adding the number of words you'd like to be shown in your excerpts. Remember: the WordPress default value for excerpt length is 55. [via]

Change the post auto-save interval

The auto-save feature is your safety net: automagically WordPress saves your work, so you don't have to worry about it in the event of a browser crash or a blackout. For some users though, the 1 minute default could be a little too much and they keep interrupting their work to push the save button. For these little crazy monkeys (count me in), it'd be great to decrease the time span between auto-saves without a plugin. And this is easily achieved by adding the following code to the wp-config.php:

define( 'AUTOSAVE_INTERVAL', 45 );

Of course the code tweak works the other way around, so if you want to increase the auto-save interval, just set a higher value like 120 seconds or even more, you adrenaline junkies :D.[via]

5 WordPress posts and pages hacks you can do without plugins Click To Tweet

WordPress Code Tweaks: Search

Show the number of results found

Search results pages are important for a website UX, so it should be pretty useful to the user. Problem is many search results page give no information about how many pages on what I'm searching for there are in a website. Thanks to the following line of code in your search.php file, you'll be able to show how many items are related to that search:

<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

This way a generic and uninformative title such as "Search Results" becomes a valuable one by providing the exact number of articles related to the search like "Search Result for search terms – 12 Articles". [via]

Exclude categories from search

If you want to have a deeper control over the results users can get, you might need a way not to show specific categories within the results page. The use cases are endless: promotional material, press release, translated content, portfolio items, just to name a few. Interested in how to do that? Open your functions.php file and add:

function SearchFilter($query) {
  if ( $query->is_search && ! is_admin() ) {
    $query->set('cat','8,15'); 
  }
  return $query; 
}
add_filter('pre_get_posts','SearchFilter');

With the possibility to exclude specific categories from your search results page, you'll also improve the quality of the information you deliver to your users and readers. [via]

Exclude pages from search

Same story goes for pages we you might want to keep out of results. So, add the following code to your functions.php file:

function modify_search_filter($query) {
  if ($query->is_search) {
    $query->set('post_type', 'post');
  }
  return $query;
}

add_filter('pre_get_posts','modify_search_filter');

You can use this on your blog to let readers just browse and look for your single posts, leaving out all pages they've no intertest in reading. [via]

15 Cool WordPress hacks you can do without plugins Click To Tweet

Wrapping things together

See? No need to install new plugins and you "earned" 15 neat features to test and, if you like them, implement directly on your website or on your clients'. The point here is, even if it's easy and there's no backfire in installing a new piece of software anytime you want, plugins can quickly pile up and make your website clunky in the future for many reasons. So you might want to pay attention to install just those that are crucial to your website or business.

I know this list is far from being exhaustive, and we'd love to hear from you: what WordPress tweaks would you add to this list? What features can be quickly and easily enabled on WordPress without a plugin? Share your thoughts and knowledge here in the comments!Quality: The Codeable Differene

  • Let me just add that it is a good practice to implement functions.php as a plugin: http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users

    • Thanks for the feedback Bruno, that’s great insight for a follow-up article.

  • Every one of those would make way more sense to put into a plugin so it would survive a theme change.

    There are even plugins in the directory now to help create a custom functionality plugin for those that are intimidated by creating a plugin of their own. I wouldn’t recommend them, but I’d recommend that over dump all this into a theme functions.php file.

    • Exactly!

    • Hey Jon,
      Good point here. Just want to say, the goal of my post was to show some (of the many) useful features users can enable by directly adding code to a WP website. This is far from being a “best practice” article, nor it was intended in the first place. Your feedback is a great topic for a follow-up article (together with Bruno’s). Thanks for sharing it!

    • Assuming you’re using a child theme, just create a functions.php file in your child theme and no worries about losing anything in a parent theme update.

    • Or add them to your mu-plugins folder.
      I’ve been using a lot of these “hacks” for years. Because they’re simple, and I want them to always run, I keep them in the must use folder: https://codex.wordpress.org/Must_Use_Plugins

      For example, if I wanted to run all the hacks in this post, I’d create a new file: myhacks.php; then add all the snippets in to that file; then save it to mu-plugins. That’s how easy it is.

      Since these are easy customizations you’d want to use on any/all your WordPress installs, it’s as easy as adding your myhacks.php file to those installs.

      • I wouldn’t put sitewide hacks like this in mu only because personally I’d like to be able to deactivate them when debugging, BUT you might want to checkout an mu
        plugin I’m working on with a friend: https://github.com/joeguilmette/wp-local-toolbox

        It’s really geared towards developer hacks for local/staging environments, some of which (disable plugins) need to run before standard plugins, but it’s turning into a nice framework for extending as you see fit.

    • Bill Dyszel

      There are situations, in large organizations, for example, when corporate IT policy forbids plugins and you have no control over the policy. This kind of information is helpful.

  • Dee see

    Do you mean, if i have means of implementing all my installed plugins into my function.php file, is a best option? Please prof. can you explain precisely, what are the main advantages of using function.php file for more functionalities in WordPress instead of installing plugins?

    • Have a look at the link I posted in my comment – Justin explains it well :)

  • If you would have spent 5 mins more writing this article and making all the code used much more consistent, the article would have been so much better!

    You should ALWAYS prefix code snippets with something unique, for example your initials. Instead you copied the code snippets verbatim from their sources, which means that sometimes they come with prefixes and sometimes not at all.

    You could also have spent some time properly internationalising your code to make the snippets more useful for the other 60% (or more) of the people that use WordPress in another language than English…

  • Natalie

    Regarding Excerpts, is there a way to enforce them to respect paragraphs and other formatting?

  • How do you make it so there are NO dates on any of your posts? So that they can all be evergreen?

  • ap

    or everything is in one plugin :) https://wordpress.org/plugins/useful-tweaks/

  • somen pal

    Nice post but you can also try some other work around available that could be brought into operation without making use of tools and plug-ins.

    Check out those with http://www.buzzlouder.com/top-5-wordpress-hacks-you-need-to-know

  • somen pal

    Nice work moreover you can consider this post for some extra knowledge

    http://www.buzzlouder.com/top-5-wordpress-hacks-you-need-to-know

  • Maks Sherstobitow

    Hi there,

    Thanks for a good article.

    I suppose you have a mistake here “define(‘widget_text’, ‘do_shortcode’);” in “Add a shortcode to widget” section. In source it’s “add_filter(‘widget_text’, ‘do_shortcode’);”.

    The latter seems to be correct.

    • yep, thanks for spotting it. I updated the string ;)

  • “Great list! Thanks. I use many of these.

    One plugin I use on all my sites is WORDFENCE – free security plugin. Does a nice job. I am amazed at how many notices I get of people trying to get into my sites. To me, Wordfence and iTheme Security are must haves on all sites.”

  • Amica Catherine

    Thanks

    • You’re more than welcome. Thanks for stopping by.

      • Amica Catherine

        No problem

  • I installed anti spam plug in that you recommended and in the last few days I have no spam in my folder to get rid of. This is great, as I was constantly emptying my spam folder, and now I can devote my time to other things. Thanks so much for the tip. I will be trying others…..

  • Some of the code tweaks I have been wondering to do. Thanks for share. Good info.

  • brilliant list, though I better comfortable in editing the theme files instead of wordrpess configuration files.