Skip to main content

Add a Hit Counter – Formidable Forms

Recently a question was asked in the Formidable Forms Slack group by a user who needed to add a hit counter to a Formidable Forms View. The user also asked if there was a hook that could be used to help achieve this task and that got us thinking about how this could be done.

The code snippet below is the nice simple solution that will work well across multiple scenarios as it uses the frm_after_display_content hook and a shortcode to display the number of times a View has been ‘viewed’.

Here’s a quick demo (try refreshing the page a few times):

There are no views with that ID

The Code

Add the code below to your functions.php file or to a new snippet in the Code Snippets plugin.

  • Change 7808 to the ID of your View
  • Add the shortcode [hit_counter] to the After Content section of your view.
add_filter('frm_after_display_content', 'view_hit_counter', 30, 4);
function view_hit_counter($after_content, $display, $show, $atts) {
	if ($display - > ID == 7808) { //Change 7808 to the ID of your View
		$filename = "count.txt"; // the text file to store count
		$fp = fopen($filename, 'r'); // Open the file for reading current count
		$count = fread($fp, filesize($filename)); // Get exiting count
		fclose($fp); //close file
		$count = $count + 1; // Add 1 to the existing count
		$total = "<p>This page has been viewed <strong>" . $count . "</strong> times.</p>"; // Display the number of hits
		$fp = fopen($filename, 'w'); // Reopen to modify content
		fwrite($fp, $count); // write the new count to file
		fclose($fp); // close file
		$after_content = str_replace('[hit_counter]', $total, $after_content);
	}
	return $after_content;
}

How this works:

The script above creates a file called ‘count.txt‘ in the root directory of your site and uses this to store the number of page hits your view has had.

Each time someone visits a page containing this view the number is retrived, +1 is added and it’s saved back to the server.

This script is designed to work with any View that has a ‘Before Content‘ and ‘After Content‘ section and uses Formidable’s  frm_after_display_content hook to display the hit counter in the ‘After Content‘ section of your view.

If you want to display the hit counter in the ‘Before Content‘ section you can change the hook to frm_before_display_content on line 1 of the code snippet and also change $after_content to $before_content on lines 2, 13 & 16 (4 places in total).

Single Entry Views

When using a Single Entry View you won’t have the ‘Before Content’ or ‘After Content’ sections so the script above won’t work. Instead you can use the frm_display_entry_content hook although a few other changes are required to make it work with a single entry view which are shown in the example below.

add_filter('frm_display_entry_content', 'frm_get_row_num', 20, 7);
function frm_get_row_num($new_content, $entry, $shortcodes, $display, $show, $odd, $atts) {
	if ( $display->ID==11910) {
		//get a field value from the current user - Change 1792 to a field ID in the form to get it's value
		$user_entry=FrmProEntriesController: :get_field_value_shortcode(array('field_id'=> 1792, 'user_id'=> 'current'));
		$filename = $user_entry . "_count.txt"; // the text file to store count
		$fp = fopen($filename, 'r'); // Open the file foe reading current count
		$count = fread($fp, filesize($filename)); //Get exiting count
		fclose($fp); //close file
		$count = $count+1; //Add 1 to the existing count
		$total = "<p>This post has been viewed <span>" . $count. "</span> times.</p>"; //Display the number of hits
		$fp = fopen($filename, 'w'); //Reopen to modify content
		fwrite($fp, $count); //write the new count to file
		fclose($fp); //close file        
                $new_content = str_replace('[row_num]', $total, $new_content); 
	}
	return $new_content;
}

This script is a little different in that it looks for a value from the form entry and uses it in the file count name to make sure that each entry has it’s own set of view stats.

You will need to pick a field that is unique to each entry like a time stamp or a User ID field and replace 1792 in the code above with your field ID.