Bret Crutchfield

Frontend Developer for Morley Group

Bret Crutchfield

Frontend Developer for Morley Group

10 deceptively simple wordpress code snippets9 min read

There are 39,000+ plugins currently in the wordpress repository, so it’s easy to immediately google search for a plugin that can solve a particular problem for you, but this isn’t always the best solution. For example, multiple plugins can cause conflicts with each other, and they can certainly add to the loading time of your whole wordpress installation. Even one plugin could cause over 100 extra http requests, all of which take time and can bog down your hosting server, especially if using a shared hosting environment. Notwithstanding, almost every hacked wordpress site I’ve cleaned was compromised by an out of date plugin, and plugins that perform minor tasks are generally the ones that are not updated.

 

I wanted to take a moment and showcase a few snippets of code for very common wordpress customizations that I see requested time and time again in Facebook groups I help out in. All code goes into your functions.php file unless noted otherwise.

!important; always use a child theme when making modifications, so that you can safely update your parent theme without losing your hard work!

 

  • Change the WP logo with yours. (or your clients)

 

Probably single handedly the most requested feature I come across is changing the logo that is default on wp-login.php, and while there is an insubordinate amount of plugins dedicated to this task, you can easily save some time and frustration by adding this code to functions.php

function login_logo() {
   echo ‘<style type=”text/css”>
       h1 a { background-image:url(‘.get_bloginfo(‘template_directory’).‘url-to-logo.jpg) !important; }
   </style>’;
}

add_action(‘login_head’, ‘login_logo’);

  1.  Disable wordpress login errors.

Hackers love wordpress login errors. It let’s them know what they got wrong, and more importantly, what they got right. One small code tweak that has huge benefits to your sites well-being is to simply disable them. As an added bonus, we can even provide them a custom message, but keep in mind, legitimate users would see this as well if they use an incorrect username/password.

function disable_login_errors(){
 return ‘Did you get permission from your mother?’;
}
add_filter( ‘login_errors’, ‘disable_login_errors’ );

  1. Replace that cheeky “howdy” message when logged in to WP.

Surprisingly, I see this come up every so often, and there is even a plugin for it! I say surprisingly, but believe it or not, this was the first piece of backend customization I ever did (back on wordpress 1.5) Save some plugin space and use this code instead:

function no_howdy( $wp_admin_bar ) {
   $my_account=$wp_admin_bar->get_node(‘my-account’);
   $newtitle = str_replace( ‘Howdy,’, Welcome back‘, $my_account->title );
   $wp_admin_bar->add_node( array(
       ‘id‘ => ‘myaccount‘,
       ‘title‘ => $newtitle,
   ) );
}
add_filter( ‘admin_bar_menu‘, ‘no_howdy‘,25 );

  1. Remove “created with wordpress” from admin footer

If you don’t like WordPress saying howdy everytime you login, you probably aren’t too keen on the thank you printed at the footer of every admin page. Once again, a search in the wordpress repository shows several plugins dedicated to this small task, that really only needs the following code:

function change_footer_admin () {
 echo “Created with love”;
}

add_filter(‘admin_footer_text’, ‘change_footer_admin’);

  1. Limit wordpress revisions

The revisions feature can be a lifesaver, and certainly has saved me hours of recreating content over the years, but by default, WordPress stores an infinite amount of revisions to page/post edits. This combined with new drag and drop builders can spell frustration for your database. (Those revisions gotta go somewhere right?)  Help your database out with this one line of code in wp-config.php

define( ‘WP_POST_REVISIONS’, 3 );

Pick a number you’re comfortable with, but almost anything is better than the current to ‘infinity and beyond’ mindset Buzz Lightyear had when implementing this feature into wordpress core.

  1. Change the post excerpt length.

Often, the default excerpt length doesn’t look good with your homepage/blog page design. Easily configure it with this snippet:

function new_excerpt_length( $length ) {
 return 20;
}
add_filter( ‘excerpt_length’, ‘new_excerpt_length’, 600 );

  1. Exclude pages from search

Nothing can trash your search results page and destroy user experience faster on most sites than bogging it down with irrelevant results from your homepage, etc. Limit searches to only your single posts with this code:

function post_search_filter($query) {
 if ($query->is_search) {
   $query->set(‘post_type’, ‘post’);
 }
 return $query;
}

add_filter(‘pre_get_posts’,‘post_search_filter’);

  1. Redirect to a set page after users register for an account.

Very useful to send new users to a thank you page, or a page detailing the next steps they should take rather than dumping them into the wordpress backend.

function registration_redirect(){

   return home_url(‘/thank-you-page’);

}

add_filter(‘registration_redirect’, ‘registration_redirect’);

  1.  Run shortcodes in a text widget

Often, users want to include a shortcode included in a plugin on the sidebar of their site. The text widget doesn’t allow this by default, but can easily be changed with this snippet:

add_filter(‘widget_text’, ‘do_shortcode’);

  1. Use Google search.

No list would be complete without an obligatory use Google. WordPress powers over 27% of the web, which brings it to around 75,000,000 websites. Along the line, someone has had the same problem you are having and made a post about it.

Have a wordpress topic you’d like to see us write about? Send us a message and we’ll look to include it next week!