6 WordPress Tricks and Hacks You Probably Don’t Know

Learn about some WordPress tricks and hacks and the step-by-step process to apply them. Required no plugins.

In this post, I’ll explain the step-by-step process about some WordPress tricks that you can apply directly without any plugins.

Things to remember before applying these hacks

  • Create a child theme and perform all these actions there. If you’re unaware to child theme, read this article.
  • Always take backup of your files and database. No, there is no risk of losing any data, but it’s always preferred to take backup than saying sorry to you.

So, let’s get into the real meat

1. Limiting Post Revisions

Every time you saves draft or update a post, WordPress revision system store a copy of same into the database. If you make some changes into the post and want to get back to the previous draft, you always have options to restore the previous version. By default, WordPress store unlimited revisions. But you actually don’t need this.

Though there are some plugins which remove those unwanted revisions from the database, however, you can control this with just a line of code.

Below are the steps:

Step 1: Navigate to WordPress root directory and open wp-config.php file into the editor.

Step 2: Add the following code and save the file.

define ('WP_POST_REVISIONS', 3);

The number 3 in the above code is to limit the maximum number of post versions can save into the database. If you don’t want to save any revisions, you can set this to 0.

2. Stop Directory Listing

Open the browser and enter the following address:

http://yourwebsite.com/wp-includes/

If you see the result similar to below screenshot, then your website is open to directory traversal attack. By default server displays all the content of the directory on the browser if doesn’t find an index file in that folder.

directory traversal

Again you just need to insert a line of code. Here are the steps:

Step 1: Navigate to WordPress root directory and open .htaccess file into the editor.

Step 2: Add the following code and save the file.

Options -Indexes

Now the user will get 403 Forbidden message.

3. Protect against Images hot-linking

Images hot-linking is when other websites directly use the images from your server via URL. Every time those websites load, they fetch the images from your server which consumes your server bandwidth.

Here are the steps to protect against this:

Step 1: Navigate to WordPress root directory and open .htaccess file into the editor.

Step 2: Copy and paste the following code in there and save the file.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?your-site.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?your-other-domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://i.imgur.com/g7ptdBB.png [NC,R,L]

This code will stop others referring to your images directly. To know more about this, read this article.

4. Hide WordPress Version

Exploring your WordPress version is a risk of attack. Some nasty hackers can find (or build) exploit specific to the version. For this reason, it is better to hide the version so that it would be difficult for them to exploit your WordPress website.

Here are the steps to hide your WordPress version:

Step 1: Navigate to the child theme and open functions.php file into the editor.

Step 2: Now copy and paste the following code in there and save the file.

function wpbeginner_remove_version() {
return '';
}
add_filter('the_generator', 'wpbeginner_remove_version');

Note: Just hiding the version can’t save you from the trouble. It is strongly recommended that you keep your WordPress version updated.

5. Custom CSS Per Post

Sometimes you may need to give a specific style to a particular post. In that case, you use the plugin that gives the power of adding custom CSS per post. However, you can achieve the same result with just some lines of code.

Below are the steps:

Step 1: Navigate to the child theme and open functions.php into the editor.

Step 2: Now copy and paste the following code in there and save the file.

//Custom CSS Widget
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
    add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'post', 'normal', 'high');
    add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'page', 'normal', 'high');
}
function custom_css_input() {
    global $post;
    echo '<input type="hidden" name="custom_css_noncename" id="custom_css_noncename" value="'.wp_create_nonce('custom-css').'" />';
    echo '<textarea name="custom_css" id="custom_css" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_css',true).'</textarea>';
}
function save_custom_css($post_id) {
    if (!wp_verify_nonce($_POST['custom_css_noncename'], 'custom-css')) return $post_id;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
    $custom_css = $_POST['custom_css'];
    update_post_meta($post_id, '_custom_css', $custom_css);
}
function insert_custom_css() {
    if (is_page() || is_single()) {
        if (have_posts()) : while (have_posts()) : the_post();
            echo '<style type="text/css">'.get_post_meta(get_the_ID(), '_custom_css', true).'</style>';
        endwhile; endif;
        rewind_posts();
    }
}

The above code will add a separate section below the post to add post specific style.

6. Add Google Analytics

Google Analytics is basically a monitoring system for website traffic. It captures traffic pattern, visitors behavior, bounce rate and much more.

There are plenty of plugins for Google Analytics in the WP repository. But if you want something simple and okay with checking reports on the analytics web interface, then there is an amazing WordPress trick. You just need a script given by Google and paste it into your function file.

Here are the steps:

Step 1: Navigate to the child theme and open functions.php into the editor

Step 2: Copy and paste the following code.

<?php
add_action('wp_footer', 'add_googleanalytics');
function add_googleanalytics() { ?>
// Paste your Google Analytics code from Step 3 here
<?php } ?>

Step 3: Open Google Analytics account and copy website tracking script.

g analytics script

Step 4: Paste this script at the place mentioned in Step 2.

Step 5. Save the file.

Now don’t forget to share about the WordPress tricks and hacks you knew and applied.

Photo credit: Pixabay

Leave a Reply

Your email address will not be published. Required fields are marked *