Author Topic: 10 Useful Hacks & Tips to take your WordPress blog to next level  (Read 3864 times)

abhimanyu

  • Newbie
  • *
  • Posts: 10
    • View Profile
1. Automatically Set the Featured Image in WordPress

This code will automatically set the first image as Featured Image in WordPress. Add the following code in functions.php file.

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');


2. How to load jQuery in Footer
Add jquery or any other script to footer. Add this in function.php file:

<?php
/**
 * Prints jQuery in footer on front-end.
 */
function ds_print_jquery_in_footer( &$scripts) {
    if ( ! is_admin() )
        $scripts->add_data( 'jquery', 'group', 1 );
}
add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );
 
?>

3. Get the first link from the blog posts
Sometimes you may need the link to display in roundup or sidebar. This simple code lets you copy the first link from the blog posts.
Add this code in functions.php file

function get_content_link( $content = false, $echo = false )
{
    if ( $content === false )
        $content = get_the_content();
 
    $content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links );
    $content = $links[1][0];
 
    if ( empty($content) ) {
        $content = false;
    }
 
    return $content;
}


Usage:

<h2><a href="<?php echo get_content_link( get_the_content() ); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">Comment »</a>

4. Add Author Box
Author box plays a big role in building engagement and generating leads. These days, every WordPress theme comes with an author box but there are still a few themes that doesn’t come with an author box.
Add this code in the single.php file, after the content() tag.

<div id="author-info">
                    <div id="author-avatar">
            <?php echo get_avatar( get_the_author_meta('user_email'), '80', '' ); ?>
            </div>
            <div id="author-description"><h3><?php the_author_link(); ?></h3>
            <?php the_author_meta('description'); ?>
                        </div>
</div>

Add the css in style.css of your theme

#author-info { float: left; padding: 20px;background-color: #f0f0f0; margin-bottom:10px; -moz-border-radius: 5px;border-radius: 5px;}
#author-info .avatar { position: relative; top: 0; left: 0; float: left; }
#author-description { margin-left: 100px; }
#author-description h3 { margin-top:0px;margin-bottom:10px;}

5. Activate Link Manager in WordPress 3.5
In the latest version (WordPress 3.5), they have disabled the link manager in the admin area. The link manager can be easily added back by adding this code snippet in your theme functions file.

<?php
 
    //Activate the Link Manager built in to the WordPress admin
    add_filter( 'pre_option_link_manager_enabled', '__return_true' );
 
?>


6. Add Custom Post Type & Taxonomies

Not everyone is good at building websites or adding a new functionality. That being said, WordPress is used to build different type of sites and one of the requirement is using custom post type & taxonomies feature.

7. Display Related Posts with Thumbnails in WordPress

Show Related posts with thumbnails with the nRelate plugin.
Installing this plugin is as simple as activating it, and you can leave the rest to nrelate. Once activated, the nrelate servers will immediately begin analyzing your website content and associating similar articles.
Download the plugin


8. Get the first image from the post automatically and display it


Fetch the first image from the blog posts and display it in the loop. You can use it, for displaying thumbnails in sidebar, footer, related posts, etc.


Paste this code in your functions.php file.

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1]
  • ;

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}


Next, paste this code in the loop to fetch the first image from the blog posts

<?php echo catch_that_image() ?>

9. Delete Post Revisions to Improve your blog speed


WordPress by default creates revisions of your posts automatically. Whenever you save a page or post, the older version is retained so that you can revert it back at any time.

To delete all previous revisions, you will need to visit PHPMyAdmin and run the following SQL query.

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';

10. Remove All HTML tags in comments


To disable the HTML tags in comments, paste the following code in functions.php file.

add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');
Regards,
Abhimanyu Rawat