Useful Code Snippets for WordPress

In this post we have useful code snippets for WordPress. These snippets will enhance or modify your WordPress website in some way. They are very easy to use. All you have to do is add or modify the relevant template with the code detailed in this article.

Search a Specific Post Type

function SearchFilter($query) {
  if ($query->is_search) {
    // Insert the specific post type you want to search
    $query->set('post_type', 'feeds');
  }
  return $query;
}
 
// This filter will jump into the loop and arrange our results before they're returned
add_filter('pre_get_posts','SearchFilter');

How to redirect to post if search results only returns one post

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
            exit;
        }
    }
}

Set Minimal Comment Limit In WordPress

add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
	$minimalCommentLength = 20;
	if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength )
        {
		wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
        }
	return $commentdata;
}

Increase WordPress Memory Limit

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

Custom Class To Last Post In A Loop

// Add a class to the last post in a loop
function last_post_class($classes){
	global $wp_query;
	if(($wp_query->current_post+1) == $wp_query->post_count) $classes[] = 'last';
	return $classes;
}
add_filter('post_class', 'last_post_class');

Allow PHP in WordPress text widgets

function php_text($text) {
 if (strpos($text, '< ' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}
add_filter('widget_text', 'php_text', 99);

Move WordPress Admin Bar to the Bottom

function fb_move_admin_bar() {
    echo '
    <style type="text/css">
    body { 
    margin-top: -28px;
    padding-bottom: 28px;
    }
    body.admin-bar #wphead {
       padding-top: 0;
    }
    body.admin-bar #footer {
       padding-bottom: 28px;
    }
    #wpadminbar {
        top: auto !important;
        bottom: 0;
    }
    #wpadminbar .quicklinks .menupop ul {
        bottom: 28px;
    }
    </style>';
}
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' );

Redirect your author archive link to your “About” page

add_filter( 'author_link', 'my_author_link' );
 
function my_author_link() {
	return home_url( 'about' );
}

Delete all Spam Comments

DELETE FROM wp_comments WHERE comment_approved = 'spam';

Add titles to previous_post_link & next_post_link

Template file to edit: single.php

$p = get_adjacent_post(false, '', true);
if(!empty($p)) echo '<div class="prev"><a href="' . get_permalink($p->ID) . '" title="' . $p->post_title . '">' . $p->post_title . '</a></div>';
 
$n = get_adjacent_post(false, '', false);
if(!empty($n)) echo '<div class="next"><a href="' . get_permalink($n->ID) . '" title="' . $n->post_title . '">' . $n->post_title . '</a></div>';

I hope you found this list useful. If you know of any other great WordPress snippets, please feel free to share them in the comment area :)

Leave a Reply

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