in , ,

Useful and Awesome WordPress Hacks and Code Snippets

In that scenarios, there is needed to code a lot which consumes lot of time as well as effort. These provided code recipes will be handy to use in that situation. We assure you these reusable code snippets and hacks, each displaying specific function will reduce your time being consumed in coding a WordPress Theme and extend the capabilities of your WordPress site.

So, get ready to walk through this sophisticated list of the most exiting, useful and awesome WordPress hacks and code snippets to make your blogger life easier. Have a fun and wish you a happy coding!!!

Add Google+ button to your posts automatically

Everybody knows that Google+ is a new “social” service started by Internet giant Google which when added to your posts may let your visitors to follow your post or share it to other group of people. Thus if you wish to let your post having an added amount of visitors, you should add a Google+ button to all of your entries automatically. we are providing below a short snippets of code which you can simply paste into your functions.php file. Once the file is saved, the Google+ button will automatically be displayed near your posts.

add_filter(‘the_content’, ‘wpr_google_plusone’);
function wpr_google_plusone($content) {
$content = $content.'<div><g:plusone size=”tall” href=”‘.get_permalink().'”></g:plusone></div>’;
return $content;
}
add_action (‘wp_enqueue_scripts’,’wpr_google_plusone_script’);
function wpr_google_plusone_script() {
wp_enqueue_script(‘google-plusone’, ‘https://apis.google.com/js/plusone.js’, array(), null);
}

source

Change WordPress default FROM email address

It is well known fact that WordPress is a very flexible blogging platform, but one problem with it is that, here, one can’t modify the FROM email adress by default. But you can do it manually by adding a precise snippets of code as given below; you need simply to paste the following snippet into your functions.php file putting the desired email name and address in the code.

add_filter(‘wp_mail_from’, ‘new_mail_from’);
add_filter(‘wp_mail_from_name’, ‘new_mail_from_name’);

function new_mail_from($old) {
return ‘admin@yourdomain.com’;
}
function new_mail_from_name($old) {
return ‘Your Blog Name’;
}
source

Redirect RSS feeds to Feedburner

Feedburner is one of the best way to to keep oneself aware of the fact that how many people have subscribed to your RSS feeds. Today we are sharing a short code snippet that let you to automatically redirect all WordPress feeds to your Feedburner feeds instead of tweaking your theme to replace links to WordPress built-in feed.

In the below code snippets just edit line 4 and replace the given feed URL with the desired URL of yours, and once done, paste the code in your functions.php file and save the file.

add_action(‘template_redirect’, ‘cwc_rss_redirect’);
function cwc_rss_redirect() {
if ( is_feed() && !preg_match(‘/feedburner|feedvalidator/i’, $_SERVER[‘HTTP_USER_AGENT’])){
header(‘Location: http://feeds.feedburner.com/catswhocode’);
header(‘HTTP/1.1 302 Temporary Redirect’);
}
}

source

Track post views without using a plugin

If you wish to know that how many visitors have read your post and how many are reading on a daily basis, then you can know this by setting the below code snippets in your functions.php file. Generally it is done by using the plug-ins, but the below given code can enable you to track the post views without using a plug-in.

function getPostViews($postID){
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
return “0 View”;
}
return $count.’ Views’;
}
function setPostViews($postID) {
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}

After pasting the above code, paste the code below within the single.php within the loop:

<?php setPostViews(get_the_ID()); ?>

And then finally, paste the snippet below anywhere within the template where you would like to display the number of views:

<?php echo getPostViews(get_the_ID()); ?>

source

Display a thumbnail from a YouTube using a shortcode

Now, we are sharing a precise code snippets which will be especially beneficial for those who generally used to display YouTube videos on their blog. The below given code will let you to display the preview thumbnail from a YouTube video using YouTube API to your readers. Have a look over the code given below:

/*
Shortcode to display youtube thumbnail on your wordpress blog.
Usage:
[youtube_thumb id=”VIDEO_ID” img=”0″ align=”left”]
VIDEO_ID= Youtube video id
img=0,1,2 or 3
align= left,right,center
*/
function wp_youtube_video_thumbnail($atts) {
extract(shortcode_atts(array(
‘id’ => ”,
‘img’ => ‘0’,
‘align’=>’left’
), $atts));
$align_class=’align’.$align;
return ‘<img src=”<a href=”http://img.youtube.com/vi/’.$id.’/’.$img.’.jpg&quot” rel=”nofollow”>
http://img.youtube.com/vi/’.$id.’/’.$img.’.jpg&quot</a>; alt=”” />’;

}
add_shortcode(‘youtube_thumb’, ‘wp_youtube_video_thumbnail’);

One the code is completed, it can be used. It accept 3 parameters: The video ID, the image size (0 for 480*360px, 1 for 120*90) and the image alignment.

[youtube_thumb id=”rNWeBVBqo2c” img=”0″ align=”center”]

source

Display number of Facebook fans in full text

If your blog is having a Facebook page, you might be wishing to display the number of Facebook fans in full text to your readers. The below given short code will let you to display how many fans you have; you just need to paste it on any of your theme files, where you’d like the count to be displayed.

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

source

Display search terms from Google

The below given code will really give you an interesting result; it will let you to know what search terms your visitors entered before arriving to your website. It will display the search terms from Google used by your visitors till they have entered your website. This snippet can also be used in order to log what user searched before arriving to your website. Just simply paste the code on any of theme files, where you’d like to display the search terms.

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

source

Retrieve a remote page using WordPress

If you want to get the content, as well as file info, of a remote file in order to use it on your WordPress install, you can do it with the help of below given code recipe. You need to use the wp_remote_get() function to retrieve the desired URL. The underlying code example shows how to retrieve an url and display its content as well as the file info which can be used anywhere on your template files.

$response = wp_remote_get( ‘http://foo.com/file.txt’ );
if( is_wp_error( $response ) ) {
echo ‘Something went wrong!’;
} else {
echo ‘Response:<pre>’;
print_r( $response );
echo ‘</pre>’;
}
source

Easily display external files using a short code

Sometimes while blogging, there is felt a need to include a file from a remote website. The following code demonstrated below will create a short code by using which you can be able to include any file you want from your WordPress post editor.

function show_file_func( $atts ) {
extract( shortcode_atts( array(
‘file’ => ”
), $atts ) );

if ($file!=”)
return @file_get_contents($file);
}
add_shortcode( ‘show_file’, ‘show_file_func’ );

Once you saved your functions.php file after pasting the above code in it , you can use the shortcode using the following syntax:

[show_file file=”http://www.somesite.com/somepage.html”]

source

Email contributors when their posts are published

Most of the blogging sites used to run multi-author blogs; in that scenario, most of the time it is felt that if any post get published, it can be automatically make known to other contributors or authors. The below code snippet will let you to do so. If the code is  pasted in your functions.php file, it will start a automated process and it results that an automated email will be sent to any contributors when any posts gets published.

function wpr_authorNotification($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);

$message = “
Hi “.$author->display_name.”,
Your post, “.$post->post_title.” has just been published. Well done!
“;
wp_mail($author->user_email, “Your article is online”, $message);
}
add_action(‘publish_post’, ‘wpr_authorNotification’);

source

Display snapshots of external websites using a short code

In most of the cases,it happens that bloggers shows links of several external sites in the blog. The below provided code will enable bloggers to put a snapshot of any website just by using this short code and the website URL. Add the below provided functions into your functions.php file.

<?php
function bm_sc_mshot ($attributes, $content = ”, $code = ”) {
extract(shortcode_atts(array(
‘url’ => ”,
‘width’ => 250,
), $attributes));
$imageUrl = bm_mshot ($url, $width);
if ($imageUrl == ”) {
return ”;
} else {
$image = ‘<img src=”‘ . $imageUrl . ‘” alt=”‘ . $url . ‘” width=”‘ . $width . ‘”/>’;
return ‘<div><a href=”‘ . $url . ‘”>’ . $image . ‘</a></div>’;
}
}
function bm_mshot ($url = ”, $width = 250) {
if ($url != ”) {
return ‘http://s.wordpress.com/mshots/v1/’ . urlencode(clean_url($url)) . ‘?w=’ . $width;
} else {
return ”;
}
}
add_shortcode(‘browsershot’, ‘bm_sc_mshot’);
?>

Once the pasting of code is done, you can use the [browsershot] shortcode on WordPress editor, as shown below:

[browsershot url=”http://link-to-website” width=”foo-value”]

source

List sites from your network

The below given code snippet is really a quite useful function for those running a network of websites using the features available in WordPress 3.+. This will assist you to get the entire list of all the sites from your network which is really pretty and easy too to use this function.

function wp_list_sites( $expires = 7200 ) {
if( !is_multisite() ) return false;

// Because the get_blog_list() function is currently flagged as deprecated
// due to the potential for high consumption of resources, we’ll use
// $wpdb to roll out our own SQL query instead. Because the query can be
// memory-intensive, we’ll store the results using the Transients API
if ( false === ( $site_list = get_transient( ‘multisite_site_list’ ) ) ) {
global $wpdb;
$site_list = $wpdb->get_results( $wpdb->prepare(‘SELECT * FROM wp_blogs ORDER BY blog_id’) );
// Set the Transient cache to expire every two hours
set_site_transient( ‘multisite_site_list’, $site_list, $expires );
}

$current_site_url = get_site_url( get_current_blog_id() );
$html = ‘
<ul id=”network-menu”>’ . “\n”;
foreach ( $site_list as $site ) {
switch_to_blog( $site->blog_id );
$class = ( home_url() == $current_site_url ) ? ” : ”;
$html .= “\t” . ‘
<li id=”site-‘ . $site->blog_id . ‘” ‘=”” .=”” $class=””>
<a href=”‘ . home_url() . ‘”>’ . get_bloginfo(‘name’) . ‘</a></li>
‘ . “\n”;
restore_current_blog();
}
$html .= ‘</ul>
<!–// end #network-menu –>’ . “\n\n”;
return $html;
}

Once the code is done, the following code will display all sites from your network. Just simply paste it on any of theme files, where you wish the list to be displayed.

<?php
// Multisite Network Menu
$network_menu = wp_list_sites();
if( $network_menu ):
?>
<div id=”network-menu”>
<?php echo $network_menu; ?>
</div>

<!–// end #network-menu –>
<?php endif; ?>

source

Add post class if the post has a thumbnail

The below provide code snippet will let you overcome from the situation when confusion arises generally while styling your theme when some of the post have a post thumbnail and those who don’t. It will simplify your front-end coding and let you to know which post has “has_thumb" CSS class and which don’t have.

function has_thumb_class($classes) {
global $post;
if( has_post_thumbnail($post->ID) ) { $classes[] = ‘has_thumb’; }
return $classes;
}
add_filter(‘post_class’, ‘has_thumb_class’);

source

What do you think?

Written by webgranth

Comments

Leave a Reply

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

Loading…

0

Comments

0 comments

Gallery Showcasing Progress Trackers in Web Design

Online Photo Editing Websites Presents a Few More Reason For Your Smile