Useful Code Snippets for WordPress Users

In the collection below you can find some amazingly helpful WordPress code snippets which you can use and implement into your WordPress blog. Sure you can find plugins to meet just about every requirement imaginable but if you can find a simple code snippet to get the job done, it’s usually the path of least resistance.

Increase WordPress Memory Limit

Some web hosts don’t allow you to make changes to php.ini file, thats where wp-config file comes to the rescue! To increase your WordPress memory limit, add this code to your wp-config.php file. You can change ’64′ to any value.

define('WP_MEMORY_LIMIT', '64M');

Disable or Limit Number of Revisions

If you don’t want to use revisions here’s a handy code to do it, add this code to wp-config.php file.

define( 'WP_POST_REVISIONS', false );

If you still want to enable revisions but want to limit the number of revisions for each post or page you can simply do it with this code:

define( 'WP_POST_REVISIONS', 5 ); // 5 is the number of revisions

Automatically Delete Posts in Trash

Add this code to your wp-config.php file.

define( 'EMPTY_TRASH_DAYS', 10 ); // 10 days

Set Auto Save Interval

Add this single code to your wp-config.php to define your own auto save interval.

define('AUTOSAVE_INTERVAL', 400); // 400 seconds

Force WordPress to perform direct upgrades and updates

Insert this code into wp-config.php to force direct update for WordPress or plugins. This way, you don’t have to enter your FTP username and password every time you want to update.

define('FS_METHOD','direct');

Change your excerpt length

Just add the following lines of code into your functions.php file, with the value 75 as the excerpt length.

add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($len) { return 75; }

Login with username or email address

Simple code snippet that lets users login using an email address or with their username. Adding this snippet to the functions.php of your WordPress theme.

function login_with_email_address($username) {
        $user = get_user_by('email',$username);
        if(!empty($user->user_login))
                $username = $user->user_login;
        return $username;
}
add_action('wp_authenticate','login_with_email_address');
function change_username_wps_text($text){
       if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
         if ($text == 'Username'){$text = 'Username / Email';}
            }
                return $text;
         }
add_filter( 'gettext', 'change_username_wps_text' );

Redirect a successful registration to specific page

This code snippet will allow you to redirect a successful registration to specific page. Just add the following lines of code into your functions.php file. Don’t forget to change my-page to your page link.

function wps_registration_redirect(){
return home_url( '/my-page/' );
}
add_filter( 'registration_redirect', 'wps_registration_redirect' );

Display the content of an external file in posts using shortcode

This code snippet will allow you to include the content of an external file within your page using shortcode. Just add the following lines of code into your functions.php file.

function show_file_func( $atts ) {
  extract( shortcode_atts( array(
    'file' => ''
  ), $atts ) );
  if ($file!='')
    return @file_get_contents($file);
}
add_shortcode( 'show_file', 'show_file_func' );

How to use shortcode to call external file:

[show_file file="http://webtechmag.com/externalfile.html"]