If you would like to enhance or improve the functionality of your WordPress site, then the PHP snippets below will give you a great head start.

You can use these snippets to change the layout and functionality of your theme and save yourself the trouble of line-by-line coding.

If you’re looking for more WordPress, take a look at this collection.

1. Add Post Meta Descriptions

WordPress does not support the use of meta description tags by default. Though metas have lost their significance in SEO, they can still influence the ranking of your blog in the search engines.

To make your site search engine friendly, you should open the header.php file of your theme and copy and paste the code below to the space between the opening &<head&> and closing &</head&> tags.

<meta name="description" content="
 <?php if ( (is_home()) || (is_front_page()) ) {
 echo ('Your main description goes here');
 } elseif(is_category()) {
 echo category_description();
 } elseif(is_tag()) {
 echo '-tag archive page for this blog' . single_tag_title();
 } elseif(is_month()) {
 echo 'archive page for this blog' . the_time('F, Y');
 } else {
 echo get_post_meta($post->ID, "Metadescription", true);
 }?>">

2. Split WordPress Content into Multiple Columns

Sometimes, you may want to create additional columns in your pages, but as we know, WP does not support this from the back-end. The best way of doing this is to use snippets to split page content. Locate your theme folder and add the following block of code within the opening and closing php tags.

// split content at the more tag and return an array
 function split_content() {
 global $more;
 $more = true;
 $content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more'));
 for($c = 0, $csize = count($content); $c < $csize; $c++) {
 $content[$c] = apply_filters('the_conte
 t', $content[$c]);
 }
 return $content;
 }

Once you do this and save the edited file, you will need to locate the specific theme files and comment out both the_content() and call split_content() function.

3. Redirect 404 Pages to Home Page

Although 404 Error Pages are an integral aspect of WordPress, they lower your page rankings significantly, thus making your blog or site ineffective. To help you out of your predicament, I suggest you copy the following snippet code to the .htacess file to redirect the 404 error pages to your home page.

< IfModule mod_alias.c >
RedirectMatch 301 ^/search/$ http://your-site.com/
RedirectMatch 301 ^/tag/$ http://your-site.com/
RedirectMatch 301 ^/category/$ http://your-site.com/
< /IfModule >

This hack will redirect search engine spiders way from the 404 error page to the home page and in the process, improve your rankings and site’s reputation online.

4. Define Posts Displayed in Home Page

Majority of bloggers display posts in more or less them same way on the home pages of their blogs. This is understandable since WordPress does not feature a default option to define how posts should be displayed. Fortunately, you can surmount this problem by using custom fields. To define posts in your home page, you can use either the full post or post excerpt only. You will need to access your index.php file and replace the default loop with the following custom code:

<?php if (have_posts()) :
 while (have_posts()) : the_post();
 $customField = get_post_custom_values("full");
 if (isset($customField[0])) {
 //Custom field is set, display a full post
 the_title();
 the_content();
 } else {
 // No custom field set, let's display an excerpt
 the_title();
 the_excerpt();
 endwhile;
 endif;
 ?>

5. Insert Google Maps into Pages

Have you been using iframes to embed Google maps in your contact page or sidebars? If so, chances are high that it will be rendered poorly because of the interference of the visual editor. To save yourself the trouble of using iframes, I recommend that you copy and paste the following short codes into the function.php page.

//Google Maps Shortcode
 function fn_googleMaps($atts, $content = null) {
 extract (shortcode_atts (array (
 "width" => '640',
 "height" => '480',
 "src" => ''
 ), $atts));
return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'"></iframe>';
}

Once you copy and paste the above code to your function.php, you will need to copy the short code below into the exact place where you want Google Maps to be displayed.

[googlemap width="200" height="200" src="[url]"]

6. Display Page Categories in a Drop Down Menu

Sometimes, you may want to add a cool drop down menu that will show all existing categories in your WordPress site. Here is a simple way of doing it. Copy and paste the code below into the index.php or sidebar.php file.

<form action="<?php bloginfo('url'); ?>/" method="get">
 <?php
 $select = wp_dropdown_categories('show_option_none=Select categoryamp;show_count=1amp;orderby=nameamp;echo=0');
 $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
 echo $select;
 ?>
 <noscript><input type="submit" value="View" /></noscript>
 </form>

7. Display Similar Posts without Plugins

It’s also possible to display similar posts in your blog so as to draw the attention of visitors and engage them. I know you will be tempted to use a plugin to display similar posts, but there is no need to overload your WordPress blog with plug ins when you can use tags and custom codes. Here is a powerful code to do display related posts quickly. Copy and paste it to the functions.php file.

<?php
 //for use in the loop, list 5 post titles related to first tag on
 //current post
 $tags = wp_get_post_tags($post->ID);
 if ($tags) {
 echo 'Related Posts';
 $first_tag = $tags[0]->term_id;
 $args=array(
 'tag__in' => array($first_tag),
 'post__not_in' => array($post->ID),
 'showposts'=>5,
 'caller_get_posts'=>1
 );
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <p><a href="<?php the_permalink() ?>" rel="bookmark" title="
 Permanent Link to <?php the_title_attribute(); ?>">
 <?php the_title(); ?></a></p>
 <?php
 endwhile;
 }
 }
 ?>

8. Change WP Login Logo

If you are tired of seeing the same old default “log in” logo, then you can change that to display a logo of your choice. To change the logo, you need to create and copy the new custom logo.png image into the image file in your root folder then copy and paste the following code into your functions.php file.

// login page logo
 function custom_login_logo() {
 echo '<style type="text/css">h1 a { background: url('.get_bloginfo('template_directory').'/companylogo.png) 50% 50% no-repeat !important; }</style>';
 }
 add_action('login_head', 'custom_login_logo');

9. Display Number of Facebook Fans

If you have a Facebook fan page then chances are high that you are thinking of displaying the total number of Facebook likes to visitors who access your WordPress site.

<?php
 $page_id = "YOUR PAGE-ID";
 $xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.queryamp;query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
 $fans = $xml->page->fan_count;
 echo $fans;
 ?>

In order for this code to work, you only need to replace your current page ID with your personal Facebook page id.

10. Create Custom Widgets

As much as WordPress themes come with various widgets, some users may want to add custom widgets that meets the needs of their blogs. If you are one of them, then here is a handy snippet for this purpose. Copy and paste the following code into your functions.php file.

class My_Widget extends WP_Widget {
 function My_Widget() {
 parent::WP_Widget(false, 'Our Test Widget');
 }
 function form($instance) {
 // outputs the options form on admin
 }
 function update($new_instance, $old_instance) {
 // processes widget options to be saved
 return $new_instance;
 }
 function widget($args, $instance) {
 // outputs the content of the widget
 }
 }
 register_widget('My_Widget');

11. Display Google Users’ Search Terms

Have you been trying to monitor and understand the search patterns of prospects and visitors who access your website from Google Search?If so, then you need a script that will display all terms that were searched by clients who came across your site. Paste the following code outside the header section.

<?php
 $refer = $_SERVER["HTTP_REFERER"];
 if (strpos($refer, "google")) {
 $refer_string = parse_url($refer, PHP_URL_QUERY);
 parse_str($refer_string, $vars);
 $search_terms = $vars['q'];
 echo 'Welcome Google visitor! You searched for the following terms to get here: ';

 echo $search_terms;
 };
 ?>

Editorial Team

Written by Editorial Team