Amazing & fresh WordPress hacks

It's been a long time that I haven't published a compilation of super useful WordPress hacks. Well, here's a list of amazing and fresh snippets from my personal bookmarks. Enjoy!

Delete post revisions using your functions.php file

Post revisions are super useful sometimes, but from time to time you might want to clean your database. Here’s a super easy way to delete all posts revisions.

Open you functions.php file and paste the following code:

$wpdb->query( "DELETE FROM $wpdb->posts WHERE post_type = 'revision'" );

Save the file and open your blog homepage to run the code. Once done, there’s no need to keep the code snippet in your functions.php file, as it will always delete all post revisions. So simply remove it.

→ Source:…

How to remove login shake effect when error occurs

Don’t like the “shake” effect which occurs each time a login error takes place? Here is a super easy snippet to remove it for good.

Simply paste the following code into your functions.php file:

function wps_login_error() {
        remove_action('login_head', 'wp_shake_js', 12);
add_action('login_head', 'wps_login_error');

→ Source:

How to automatically add paragraph tags

By default, WordPress automatically adds paragraph tags to the content and the excerpt, using the wpautop() function. If you need to automatically add some

tags to any text, you can use the function as shown in today’s example.

In order to add paragraph tags to any text, simply use the wpautop() function, as shown below:

$my_text = 'Lorem ipsum dolor sit amet
consectetur adipiscing elit. 
Nulla pretium libero eget gravida rutrum.';

echo wpautop( $my_text );

→ Source:

How to clean up wp_head() without a plugin

WordPress adds a lot of stuff through wp_head() hook included in most WordPress themes. Some of this stuff is useful, but some other isn’t. Here’s a quick recipe to clean up the wp_head() easily without using a plugin.

Paste the following lines of code into your functions.php file:

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );

→ Source:

Redirect to a custom page after registration

Here is a very handy code snippet to redirect the user to a custom page after registration on your WordPress blog or website. Super useful for those using WP as a CMS!

Simply drop this snippet in your functions.php or a plugin.

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

→ Source:…

Force specific pages to be SSL secure

If SSL is enabled on your web server, you should definitely use it to protect your blog. Activating SSL on your specific pages on a WordPress blog is definitely easy: just read on.

Just add the following snippet to the functions.php file of your WordPress theme and specify the post or page ID desired.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    return $force_ssl;
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

→ Source:

Setup different admin and theme languages

If like me, you’re blogging in a language which is not your mother tongue, you might like to have the dashboard in your language. Here is a quick tip to use different languages on your WordPress blog or website.

Simply set the desired locale (on line 6) then add the code to your functions.php file.

     // setup one language for admin and the other for theme
     // must be called before load_theme_textdomain()

     function set_my_locale($locale) {
          $locale = ( is_admin() ) ? "en_US" : "it_IT";
          setlocale(LC_ALL, $local );
          return $locale;
     add_filter( 'locale', 'set_my_locale' );

→ Source:

Automatically link Twitter usernames in WordPress

Are you using Twitter a lot? Today’s recipe is a cool piece of code to automatically link Twitter usernames on your posts, pages, and comments.

Paste the code below into your functions.php file:

function twtreplace($content) {
	$twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
	return $twtreplace;

add_filter('the_content', 'twtreplace');   
add_filter('comment_text', 'twtreplace');

Once you saved the file all Twitter usernames in posts and comments will automatically be linked to their Twitter profiles. Usernames have to be written under the form @username.

→ Source:

Automatically spam comments with a very long url

Spam is definitely a problem for bloggers and most of you probably receive more than 100 spam comments per hour. Here is a simple recipe to automatically mark as spam all comments with an url longer than 50 characters.

Open your functions.php file and paste the code below in it. This code will automatically mark as spam all comments with an url longer than 50 characters. This can be changed on line 4.


  function rkv_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;

  add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );


→ Source: