Amazing & fresh WordPress hacks

by Jean. 13 Comments -

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: http://www.trickspanda.com/2014/01/how-to-delete-wordpress…

How to remove login shake effect when error occurs

Don’t like the “shake” effect which occurs each time a login error occurs? 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: http://wordpress.org/support/topic/plugin-simplemodal-login-remove-shake-effect

How to automatically add paragraph tags

By default, WordPress automatically add 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: http://www.smashingmagazine.com/2013/09/26/powerful-wordpress-tips-and-tricks/

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: http://www.themelab.com/remove-code-wordpress-header/

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: http://wordpress.stackexchange.com/questions/19692/how-to-redirect-a-sucessful…

Force specific pages to be SSL secure

If SSL is enabled on your webserver, 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: http://wpsnipp.com/index.php/functions-php/force-specific-pages-to-be-secure-ssl-https/

Setup different admin and theme languages

I 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.

<?php
     // 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: http://wp-snippet.com/different-admin-and-theme-languages

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=\"http://twitter.com/$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: http://snipplr.com/view/70977/automatically-link-twitter-usernames-in-wordpress/

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 chars. This can be changed on line 4.

<?php

  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: http://css-tricks.com/snippets/wordpress/spam-comments-with-very-long-urls