15 Fresh WordPress Code Snippets Worth Checking Out

      

WordPress is a very functional content management system but you can always integrate new functions and features in your wordpress blog.This depends on how you interact with your users.

Today we are featuring fresh wordpress code snippets-some already known- to make your blog more functional and stand out from the crowd.

Use your blog gravatar as favicon

If you want to use your gravatar as your favicon then simply add the following code in your function.php file and modify.

<?php
function GravatarAsFavicon() {
	//We need to establish the hashed value of your email address
	$GetTheHash = md5(strtolower(trim('you@yourdomain.com')));
	echo 'http://www.gravatar.com/avatar/' . $GetTheHash . '?s=16';
}
?>

Then add the following code in your header.php file

<strong><link rel="shortcut icon" href="<?php GravatarAsFavicon(); ?>" /></strong>

Source

Display your latest tweets on your WordPress blog without any plugin

Paste the above code in your theme file where your latest tweet to be displayed

<?php
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=catswhocode');
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'>
<?php echo $item->get_title(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Source

Set the content type email from text/plain to text/html

Add the below code in your functions.php file

function wps_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','wps_set_content_type' );

Source

Add custom post types to archives page

Add the below snippet in your functions.php file.

function add_custom_types_archive( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
 'post', 'your-custom-post-type-here'
            ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'add_custom_types_archive' );

Source

Replace widget title with custom title and HTML

Adding this snippet to the functions.php will let you use a filter to replace the widget title with custom title and or html.

add_filter('widget_title', 'change_widget_title', 10, 3);
function change_widget_title($title, $instance, $wid){
    return $title = str_replace('Widget Title', '<span style="color: red">Custom</span>', $title);
}

Source

Display attachment thumbnail with image metadata

Adding this snippet within the loop of your index.php template file will display a list of all post attachments with the following metadata (Credit, Camera, Focal Length, Aperture, ISO, Shutter Speed, Time Stamp, Copyright).

<?php
if($images =& get_children( 'post_type=attachment' )){
   foreach($images as $id => $attachment ){
	   echo '<div>';
	   echo wp_get_attachment_image( $id, 'thumb' )."<br />";
           $meta = wp_get_attachment_metadata($id);
           echo "Credit:  ".$meta[image_meta][credit]."<br /> ";
           echo "Camera:  ".$meta[image_meta][camera]."<br />";
           echo "Focal length:  ".$meta[image_meta][focal_length]."<br />";
           echo "Aperture:  ".$meta[image_meta][aperture]."<br />";
           echo "ISO:  ".$meta[image_meta][iso]."<br />";
           echo "Shutter speed:  ".$meta[image_meta][shutter_speed]."<br />";
           echo "Time Stamp:  ".$meta[image_meta][created_timestamp]."<br />";
           echo "Copyright:  ".$meta[image_meta][copyright];
	   echo '</div>';
   }
}
?>

Source

Check if user is using StumbleUpon

Display a special message to only your stumbleupon visitors.This conditional tag checks if the user comes from StumbleUpon.Add the below snippet in your header.php file
Note: this doesn’t work if the user are using a browser toolbar, since it checks the URL for stumbleupon.com.

<?php if( strpos($_SERVER[HTTP_REFERER], "stumbleupon.com" ) == true ): ?>
    <div class="welcome-stumbleupon">
        <p>Hello StumbleUpon user!</p>
    </div>
<?php endif; ?>

Source

Display the comment count using a shortcode

Adding this snippet to the functions.php of your wordpress theme will let you display the comment count for any post by simply using this shortcode [comments id=”23″ ]

function comments_shortcode($atts) {
	extract( shortcode_atts( array(
		'id' => ''
	), $atts ) );
	$num = 0;
	$post_id = $id;
	$queried_post = get_post($post_id);
	$cc = $queried_post->comment_count;
		if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments';
		else : $cc = $cc.' Comment';
		endif;
	$permalink = get_permalink($post_id);
	return '<a href="'. $permalink . '" class="comments_link">' . $cc . '</a>';
}
add_shortcode('comments', 'comments_shortcode');

Source

Automatically add a Google+ button to your posts

Just add the below code into your functions.php file.

add_filter('the_content', 'wpr_google_plusone');
function wpr_google_plusone($content) {
	$content = $content.'<div class="plusone"><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

How to automatically email guest authors when their posts are published

If you your blog has too many guest authors it is hard to e-mail them that their posts are live.So just copy and paste the below code in your functions.php file.

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

Add your own meta copyright to your WordPress blog

If you want to add your own copyright meta just paste the following code into your function.php file and modify Line 5

add_action("wp_head", "my_copyright_meta");

function my_copyright_meta() {
   if(is_singular()){
      echo "<meta name="copyright" content="© Me, 2011">";
   }
}

Source

How to remove the WordPress.org link from Login / Register Page

If you want to remove WordPress.org link from logo of your wordpress blog then just add following links of code inside your themes functions.php file.

<?php
/* WordPress Change Login page logo link */
function change_login_page_url( $url ) {
    $link_url='http://mywebsite.com';
    return $link_url;
}
add_filter( 'login_headerurl', 'change_login_page_url' );
?>

Source

How to change WordPress editor font

If you want t chanege the WordPress visual Editor font then simply add the following code in yoyr function.php file.You can modify the code with your favorite font.

add_action( 'admin_head-post.php', 'devpress_fix_html_editor_font' );
add_action( 'admin_head-post-new.php', 'devpress_fix_html_editor_font' );

function devpress_fix_html_editor_font() { ?>
<style type="text/css">#editorcontainer #content, #wp_mce_fullscreen { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; }</style>
<?php }

Source

Implement a maintenance mode on your WordPress blog

Paste the following code into your functions.php fileYou can modify the message. Don’t forget to remove the code once you’re done with maintenance

function wpr_maintenance_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('Maintenance, please come back soon.');
    }
}
add_action('get_header', 'wpr_maintenance_mode');

Source

 

Change the default from addresses when WordPress sends out its emails

Add this to functions.php to change the default from addresses when WordPress sends out its emails

<?php
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');

function new_mail_from($old) {
 return 'no-reply@example.com';
}

function new_mail_from_name($old) {
 return 'Dave Thomas';
}
?>

Source

5 Comments

  1. Anagorn