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):

ID Date Name Services
7807 1 Nov 2021 Terry Jones Form building
7806 1 Nov 2021 Chris Web design
7805 1 Nov 2021 Terry Form building
7804 1 Nov 2021 Terry Form building
7803 1 Nov 2021 Chris Adam Web design, Branding
5129 8 Sep 2020 terry vegetables Web design
5006 27 Aug 2020 Wally Walden Form building, Graphic design
5005 27 Aug 2020 Michael Moses Web design, Form building
5004 27 Aug 2020 Gary Goblin Branding, Software
5003 27 Aug 2020 Chris Adams Web design, Software
5002 27 Aug 2020 Steve Smith Web design, Form building, Software, Other
5001 27 Aug 2020 Minesh Kothari Web design, Branding
5000 27 Aug 2020 Chris Web design
4999 27 Aug 2020 Minesh Kothari Web design, Branding
4998 27 Aug 2020 Minesh Kothari Web design, Branding
4997 27 Aug 2020 Terry Test Form building
4996 27 Aug 2020 Steve Smith Web design
4995 27 Aug 2020 Terry Test Form building
3370 28 Feb 2020 Terry Test Form building
3369 28 Feb 2020 Steve Smith Web design
3368 28 Feb 2020 Chris Web design
3367 28 Feb 2020 Minesh Kothari Web design, Branding
3366 28 Feb 2020 Chris Web design
3365 28 Feb 2020 Terry Test Form building
3364 28 Feb 2020 Chris Web design
3363 28 Feb 2020 Steve Smith Web design
3351 26 Feb 2020 Terry Test Form building
3350 26 Feb 2020 Steve Smith Web design
2304 15 Apr 2019 Minesh Kothari Web design, Branding
2289 10 Apr 2019 Chris Web design
2284 8 Apr 2019 Chris Web design, Branding

This post has been viewed 5285 times.

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.