Useful Wordpress Snippets

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only.

There are loads of things that can be achieved very easily in Wordpress if you know how. Many of these things can be done in just one line of code, so here are a few that I have found useful in the past and want to keep a record of for use in the future.

Shortcodes in Wordpress Templates

Shortcodes are useful to easily evoke a function e.g. embedding a youtube video in a post. If however you want this handy functionality in you template instead of just a post/page you can using:

<?php echo do_shortcode('[shortcode]'); ?>

Shortcodes in Widgets

If you want to use shortcodes in your widgets you can enable the ability by adding the following line to your functions.php file.

add_filter('widget_text', 'do_shortcode');

Disable Admin Bar

Are you like me at times distracted by the admin bar introduced in WP3? We can disable it with three lines added to functions.php.

wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

Turn off Revisions

Wordpress uses a great revision system that saves revisions to the database if however the database space is an issue for you it can be disabled by adding the following line to the wp-config.php file:

define('WP_POST_REVISIONS', false);

Limit Revisions

Want to keep the useful revisions feature we can limit the number of revisions kept to keep database size down by adding the following line to the wp-config.php file. The example limits the system to only keep the last five.

define('WP_POST_REVISIONS', 5);

Austosave Interval

Wordpress auto save automatically when you are editing a post or page. The default auto saves every 60 seconds this can create a lot of overhead. We can change this for something more sensible like 10 minutes (600 seconds) by adding this line to your wp-config.php file.

define('AUTOSAVE_INTERVAL', 600);

Reset Admin password MySQL query

If you have lost your admin password and can’t use the forgotten password feature you can reset the admin password using the following MySQL snippet. You can’t simply change the database record as Wordpress hashes the password so instead run this query.

UPDATE `wp_users` SET `user_pass` = MD5( 'new_password_here' ) WHERE `wp_users`.`user_login` = 'admin_username';

Remove Generator Meta Tag

To remove the generator meta tag you can add the following line to your functions.php file:

remove_action('wp_head', 'wp_generator');

Memory Limit

Wordpress imposes its own memory limit of 32MB to guard against rogue plugins. Your server may also impose a limit that should be changed using php.ini. If you want to increase this limit for Wordpress specifically you can by adding the following line to your wp-config.php file (where 64MB is the new limit):

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

This article was posted on 15 April 2011 in Snippets, Wordpress

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.