WordPress: Simple Post Views without a Plugin using Post Meta Part 3 – [ Shortcode support]

Simple Post Views without Plugin
Posts in this series
  1. Simple Post Views without a Plugin in WordPress
  2. Simple Post Views without Plugin in WordPress Part 2
  3. WordPress: Simple Post Views without a Plugin using Post Meta Part 3 - [ Shortcode support]

In Previous part we added few parameters to our my_get_post_views function which gave us a lot more flexibility in using our post view counter function.What we can do next is we can add wordpress shortcode support to our  Simple Post Views system.

After adding short code functionality to our code we will be able to control on which page we want to display the Post view counter or record post views,suppose in some case you only want to record post views for certain posts or page,you can use use this shortcode and embed it directly in to your desired location in wordpress post editor

This way you do not have to edit php file to put post view recording function in it.So here is the code which will add the shortcode facility

function post_views_func($atts)
{
	$post_id = isset($atts['id']) ? intval($atts['id']) : 0;
	$hide_on_zero = isset($atts['nozero']) ? $atts['nozero'] : false;
	$append_singular = isset($atts['singular']) ? $atts['singular'] : 'View';
	$append_plural = isset($atts['plural']) ? $atts['plural'] : 'Views';
	$enclosed_html = isset($atts['html']) ? $atts['html'] : '%s';
	$update_view = isset($atts['update']) ? $atts['update'] : false;
	$views = my_get_post_views($post_id, $update_view, $hide_on_zero, $append_singular, $append_plural, $enclosed_html);
	return $views;
}

add_shortcode('post_views', 'post_views_func');

now you can use short code in your wordpress post editor like this:

[post_views update= ‘1’  nozero=’0′ singular=’view’ plural=’views’ html='<h1>%s</h1>’ ]

Let me Briefly summaries short code parameters

update= ‘1’  Short will also update post view counter no need to  call function my_set_post_views ()

nozero=’0′ If you do not want 0 views to be displayed set it to 1 otherwise 0

singular=’view’ Text to append if number of view is in singular.

plural=’views’ Text to append if number of view is in plural.

html='<h1>%s</h1>’ you can set html formatting your self ,just put %s where you want the views to appear

 

Final Complete Snippet with Shortcode support:

Replace your old snippet in functions.php with this new one ,also if you are going to use shortcode and it’s parameter update=’1′ you do not need to call function my_set_post_views in your template file.

function my_get_post_views($post_id=0, $update_view = false, $hide_on_zero = false, $append_singular = "View", $append_plural = "Views", $enclosed_html = "%s")
{
	$count = 0;
	$post_id = !$post_id ? get_the_ID() : $post_id;
	if ($post_id) {
		$meta_count_key = 'my_views_count';
		$count = get_post_meta($post_id, $meta_count_key, true);
		if ($count == '') {
			delete_post_meta($post_id, $meta_count_key);
			add_post_meta($post_id, $meta_count_key, '0');
		}

		$count = intval($count);
	}

	if ($count) {
		if ($count > 1) {
			$views = sprintf($enclosed_html, $count . " " . $append_plural);
		}
		else {
			$views = sprintf($enclosed_html, $count . " " . $append_singular);
		}

		$views = sprintf($enclosed_html, $count . " " . ($count > 1 ? $append_plural : $append_singular));
	}
	else {
		if ($hide_on_zero) {
			$views = "";
		}
		else {
			$views = sprintf($enclosed_html, $count . " " . $append_singular);
		}
	}

	if ($update_view) {
		my_set_post_views($post_id);
	}

	return $views;
}



function my_set_post_views($post_id = 0)
{
	$post_id = !$post_id ? get_the_ID() : $post_id;
	if (!$post_id) {
		return;
	}

	$meta_count_key = 'my_views_count';
	$count = get_post_meta($post_id, $meta_count_key, true);
	if ($count == '') {
		$count = 0;
		delete_post_meta($post_id, $meta_count_key);
		add_post_meta($post_id, $meta_count_key, '0');
	}
	else {
		$count++;
		update_post_meta($post_id, $meta_count_key, $count);
	}
}



function post_views_func($atts)
{
	$post_id = isset($atts['id']) ? intval($atts['id']) : 0;
	$hide_on_zero = isset($atts['nozero']) ? $atts['nozero'] : false;
	$append_singular = isset($atts['singular']) ? $atts['singular'] : 'View';
	$append_plural = isset($atts['plural']) ? $atts['plural'] : 'Views';
	$enclosed_html = isset($atts['html']) ? $atts['html'] : '%s';
	$update_view = isset($atts['update']) ? $atts['update'] : false;
	$views = my_get_post_views($post_id, $update_view, $hide_on_zero, $append_singular, $append_plural, $enclosed_html);
	return $views;
}

add_shortcode('post_views', 'post_views_func');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

 

I hope you liked this series of artical on how to add Post Views without a Plugin using Post Meta  in wordpress.Please post comment if you have any question ,and dont forget to share this article if you think it will be helpful for someone else

You may also like: