WordPress Tweaks for Posts And Pages


There are a lot of easy WordPress tweaks that you can do to up the security of your WP blog, help your SEO or change the way that your blog feels. I’ve decided to start a list of all those easy tweaks that you can do. These are all easy things which don’t require any plugin at all, just a little copy pasting.

We love WordPress – but not all of us are ready to settle with all its default settings and displays – particularly how posts are displayed.

Whether you are looking to change your post display to enhance user experience or to increase revenue or page impressions, chances are there is a way to do it without plugin, and most of the snippets listed here are easy to implement, in most time you just need to copy and paste the provided code.

Change Your Excerpt Length

By adding this code to your functions.php file, you can set the excerpt length to whatever you want.

add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($len) { return 75; } //change the 75

Change Excerpt Length Depending on The Category

By adding this code to your functions.php file, you can set the excerpt length based on which category you are posting in.

add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
    if(in_category(14)) { //change the category ID
        return 13;
    } else {
    return 60;
    
}

Add “Read More” Permalink to The End of “the_excerpt”

Adding the snippet below into the functions.php file of your WordPress theme will add a “read more” permalink at the end of the_excerpt.

1
2
3
4
function excerpt_readmore($more) {
    return '... <a href="'. get_permalink($post->ID) . '">' . 'Read More' . '</a>';
}
add_filter('excerpt_more', 'excerpt_readmore');

Twitter Style “Time Ago” Dates

Most people don’t realize that WordPress has a built-in function to display the date using the “Time Ago” format. The snippet below can be pasted to anywhere within the loop to display the date with this format.

Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago';

Redirect to Single Post When Search Query Returns Single Result

Put this snippet into the functions.php file of your WordPress theme to redirect your search to the post automatically when WordPress only returns a single search result.

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

Set Maximum Post Title Length

Adding this PHP code into the functions.php file of your WordPress theme will set a maximum number of words that can be displayed within your post title.

function maxWords($title){
    global $post;
    $title = $post->post_title;
    if (str_word_count($title) >= 10 ) //set this to the maximum number of words
    wp_die( __('Error: your post title is over the maximum word count.') );
}
add_action('publish_post', 'maxWords');

Automatically Create Meta Description From “the_content”

Adding this snippet into the functions.php file of your WordPress theme will automatically create a meta description from your WordPress post. Make sure that you have it in the header.php of your WordPress theme or this snippet will not function properly.

function create_meta_desc() {
    global $post;
    if (!is_single()) { return; }
    $meta = strip_tags($post->post_content);
    $meta = strip_shortcodes($post->post_content);
    $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
    $meta = substr($meta, 0, 125);
    echo "<meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');

Create Your Own Popular Posts in The Sidebar

To create a sidebar widget that displays popular posts, add this PHP code into your sidebar.php file.

<h2>Popular Posts</h2>
<ul>
    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5"); //change the 5 to change the number of posts shown
    foreach ($result as $post) {
        setup_postdata($post);
        $postid = $post->ID;
        $title = $post->post_title;
        $commentcount = $post->comment_count;
        if ($commentcount != 0) { ?>
            <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
            <?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
    <?php } } ?>
</ul>

Show Related Posts Without a Plugin

The code below will make your WordPress site display related posts based on the current post tags. You need to place it inside single.php, or anywhere you want to show the related posts.

<?php
      $tags = wp_get_post_tags($post->ID);
      if ($tags) {
            $tag_ids = array();
            foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
            $args=array(
            'tag__in' => $tag_ids,
            'post__not_in' => array($post->ID),
            'showposts'=>5, //number of related posts that will be shown
            'caller_get_posts'=>1
            );
      $my_query = new wp_query($args);
            if( $my_query->have_posts() ) {
                  echo '<h3>Related Posts</h3><ul>';
                  while ($my_query->have_posts()) {
                        $my_query->the_post();
                        ?>
                        <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
                  <?php
                  }
                  echo '</ul>';
            }
      }
?>

Display Post Thumbnail In Your RSS Feed

Introduced in WordPress 2.9, the the_post_thumbnail() function is very useful to add and display a thumbnail attached to a post. The bad news is there’s no built-in method to display the thumbnail in your RSS feed. The function below will solve this problem. Simply paste it into your functions.phpfile, save it, and the post thumbnail will be automatically displayed in your RSS feed.

    // show post thumbnails in feeds

function diw_post_thumbnail_feeds($content) {   global $post;   if(has_post_thumbnail($post-        >ID)) {      $content = ‘<div>’ . get_the_post_thumbnail($post-    >ID) . ‘</div>’ . $content;    }   return $content;}add_filter(‘the_excerpt_rss’, ‘diw_post_thumbnail_feeds’);add_filter(‘the_content_feed’, ‘diw_p   ost_thumbnail_feeds’);

Post Formats – More Creative Ways For A Theme

The syntax below gives some of the possible post formats which can then be chosen and used directly in the article, and what you need to do is to put the code into your functions.php file of your theme.

add_theme_support( ‘post-formats’, array( ‘aside’, ‘audio’, ‘image’, ‘video’ ) );

Some technical SEO tweaks

Change your permalink structure

The most import tweak that most people suggest you to do is to change your permalinks. While I totally agree and admit that this is one of the first things I do when setting up a new blog, I don’t suggest you change it to the standard %category%/%postname%/. Instead change it to the following; for the reason why just check what WordPress has to say about this on their Permalink Page.

  %post_id%/%postname%/

WWW. or non-WWW.

Just pick one and stick with it. Fill this in as your Site url and your blog url, at your General Settings.

Check for HTTP-Refferer, to prevent spammers

Some spambots usually don’t have their HTTP-refferers set. To block people who don’t have this set add the following lines to your theme’s functions.php

function check_HTTP_ref() {
    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == “”) {
        wp_die( __('Please enable referrers in your browser!') );
    }
}
add_action('check_comment_flood', 'check_HTTP_ref');
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s