Programming

The Most Useful WordPress Hacks

Programming Tutorial

WordPress is no new name to the ears of passionate bloggers, writers and designers. The advanced platform offers a plethora of features that can be used well to create an impressive website. Often, we need to regulate several features to offer a user-friendly navigation to our visitors. While newbie developers and designers install plugins, experts believe in using efficient WordPress hacks through coding.

If you are looking for some of the most useful WordPress hacks, here’s how you can enhance the performance and offer better navigation to your visitors.

Inserting Ads within Post Content

In many blogs, we find ads placed between a post content. Usually these ads are placed after the first or second paragraph. These ads are highly effective as they appear while a visitor goes through the post. Now, you can insert this type of ads in two ways.

First, you can insert a code manually at the time of posting a content. Just choose where you want the ad to appear and insert the following code manually.

<?php
 
//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	
	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}
	
	return $content;
}
 
// Parent Function that makes the magic happen
 
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
	$closing_p = '</p>';
	$paragraphs = explode( $closing_p, $content );
	foreach ($paragraphs as $index => $paragraph) {

		if ( trim( $paragraph ) ) {
			$paragraphs[$index] .= $closing_p;
		}

		if ( $paragraph_id == $index + 1 ) {
			$paragraphs[$index] .= $insertion;
		}
	}
	
	return implode( '', $paragraphs );
}

In the above-mentioned code, you need to insert the ad’s code in place of ‘Ads Code Goes Here’. Apart from this, you may use plugins if you may need to change the advertisers frequently. The ad management plugins like Insert Post Ads can be installed and activated conveniently.

Highlighting New Posts

If you have loyal visitors that navigate to your site every day, they probably go through a lot of content. Hence, adding special effects to your latest posts would be an effective idea as it would help the readers identify the new posts easily.

You can add special effects to the posts that are published 24 hours ago or lesser. To do so, you just need to add the following code in place of the loop in your index.php file.

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); 
        $currentdate = date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
        $postdate = get_the_time('Y-m-d');
        if ($postdate==$currentdate) { 
            echo '<div class="post new">';
        } else {
            echo '<div class="post">';
        } ?>
        <a href="<?php the_permalink() ?>" rel="bookmark">
        <?php the_title(); ?></a>
        <?php the_time('j F Y'); ?>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

This will add a New tag to your latest posts. Also, you can regulate your stylesheet in the following way:

.post{
    /* CSS style for "normal" posts */
}

.post.new {
    /* CSS style for newer posts */
}

Deleting Post Revisions

Post revisions is one of the most useful WordPress features. But as everything has a dark side, the drawback of post revisions is that it adds up space on your database. It is possible to delete post revisions manually; but it is an exhaustive and boring task if your posts have multiple revisions.

Hence, we have come up with a useful WordPress hack where you can delete post revisions in batches. All you need to do is copy the following snippet in SQL window. You may access it by digging into your phpmyadmin and selecting your database.

DELETE FROM wp_posts WHERE post_type = "revision";

Refusing Spam Comments

You just hate irrelevant and nuisance comments on your posts, don’t you? Although Akismet blocks some comments, not all spammy comments are detected by this tool. Hence, if you are looking for a better way to refuse spam comments from your posts, it’s time to insert the following code in your functions.php file.

function in_comment_post_like($string, $array) { 
	foreach($array as $ref) { if(strstr($string, $ref)) { return true; } } 
	return false;
}
function drop_bad_comments() {
	if (!empty($_POST['comment'])) {
		$post_comment_content = $_POST['comment'];
		$lower_case_comment = strtolower($_POST['comment']);
		$bad_comment_content = array(
			'viagra', 
			'hydrocodone',
			'hair loss',
			'[url=http', 
			'[link=http', 
			'xanax',
			'tramadol',
			'russian girls',
			'russian brides',
			'lorazepam',
			'adderall',
			'dexadrine',
			'no prescription',
			'oxycontin',
			'without a prescription',
			'sex pics',
			'family incest',
			'online casinos',
			'online dating',
			'cialis',
			'best forex',
			'amoxicillin'
		);
		if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
			$comment_box_text = wordwrap(trim($post_comment_content), 80, "\n  ", true);
			$txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a');
			fwrite($txtdrop, "  --------------\n  [COMMENT] = " . $post_comment_content . "\n  --------------\n");
			fwrite($txtdrop, "  [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");
			fwrite($txtdrop, "  [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");
			fwrite($txtdrop, "  [REFERER  ] = " . $_SERVER['HTTP_REFERER'] . "\n");
			fwrite($txtdrop, "  [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");
			fwrite($txtdrop, '--------------**********------------------'."\n");
			header("HTTP/1.1 406 Not Acceptable");
			header("Status: 406 Not Acceptable");
			header("Connection: Close");
			wp_die( __('bang bang.') );
		}
	}
}
add_action('init', 'drop_bad_comments');

Using the above-mentioned code will help you to refuse any comment that includes any word that is listed under the $bad_comment_content array.

Creating Author Info Section

It is always a remarkable gesture to give your writers an author credit on your blog. Blogs on which multiple writers post content often have an author bio section. Usually, this section consists of the author’s name, profile picture, short bio and a link to their website (if they have any).

If you want to give the credibility of publishing posts to your writers, you may create an author info section in your posts. The author info section generally sits at the end of a post. Thus, you may add this section by inserting the following code in the single.php WordPress file.

<div id="author-info">
    <div id="author-image">
    	<a href="**Author Website**">**Author Gravatar**</a>
    </div>   
    <div id="author-bio">
        <h4>Written by <a href="**Author Website**">**Author Name**</a></h4>
        <p>**Author Description**</p>
    </div>
</div><!--Author Info-->

WordPress is a bundle of interesting features. You can learn and explore creative and useful WordPress hacks to enhance the performance, appearance and navigation of your site.

  • Written by
  • Finepoint Design
  • November 21, 2015 / 125 Views

Drop your Message

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