10 Powerful PHP Snippets for Adding Extra Functionality to WordPress

//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>’; }
<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

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:

1. Add Post Meta Descriptions

[googlemap width=”200″ height=”200″ src=”[url]”]

6. Display Page Categories in a Drop Down Menu

< 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 >
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.
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

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.
// 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

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.
<?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; ?>
If you’re looking for more WordPress, take a look at this collection.
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.
<?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

<?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

You can use these snippets to change the layout and functionality of your theme and save yourself the trouble of line-by-line coding.
<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 onchange=’return this.form.submit()’>”, $select); echo $select; ?> <noscript><input type=”submit” value=”View” /></noscript> </form>

7. Display Similar Posts without Plugins

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.
// 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.

 <?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; }; ?> 

Posted by WordPress Guru