Advertisement
Guest User

WordPress Plugin Simple Stats

a guest
Jan 10th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2. /**
  3. Plugin Name: Rics Simple Page Stats
  4. Description: Simple plugin to count and show a total number hits (Unique visitors or page-views) to individual Pages and Posts without using any third party code. Based on the great plugin "srs-simple-hits-counter".
  5. Author: Rics
  6. Version: 0.1
  7. Original Author: Atif N
  8. Original Author URI: http://sandyrig.com
  9.  
  10. Use it as you wish!
  11.  
  12.  */
  13.  
  14. // Exit if accessed directly
  15. if ( !defined( 'ABSPATH' ) ) exit;
  16.  
  17. // Start Session
  18. add_action('init', 'rics_start_session', 1);
  19. function rics_start_session() {
  20.     if(!session_id()) {
  21.         session_start();
  22.     }
  23. }
  24.  
  25. // UPDATE COUNTER
  26.  
  27. add_action('wp_head','rics_simple_hits_counter');
  28. function rics_simple_hits_counter(){
  29.  
  30.     $url = home_url(add_query_arg(array()));
  31.     $id = url_to_postid($url);
  32.  
  33.     if( !isset( $_SESSION["rics_counter_increased_$id"] ) || ( isset( $_SESSION["rics_counter_increased_$id"] ) && $_SESSION["rics_counter_increased_$id"] != 'yes' ) ) {
  34.         $rics_visitors = intval( get_option("rics_visitors_count_$id") );
  35.         update_option("rics_visitors_count_$id", $rics_visitors+1);
  36.         $_SESSION["rics_counter_increased_$id"] = 'yes';
  37.     }
  38.  
  39.     $rics_pageViews = intval( get_option("rics_pageViews_count_$id") );
  40.     update_option("rics_pageViews_count_$id", $rics_pageViews+1);
  41. }
  42.  
  43. // ADD THE "FRONT END" TO EVERY PAGE
  44.  
  45.  add_filter( 'the_content', 'rics_simple_stats_panel' );
  46.  function rics_simple_stats_panel( $content ) {
  47.     if ( isset($_GET["stats"]) ) {
  48.         $url = home_url(add_query_arg(array()));
  49.         $id = url_to_postid($url);
  50.         $html  = '<div id="rics_hidden_stats"><strong class="rics_stats_title">ESTATÍSTICAS DE ACESSO</strong><br>';
  51.         $html .= "Visitantes únicos: &nbsp; " . intval(get_option("rics_visitors_count_$id"));
  52.         $html .= "<br>";
  53.         $html .= "Visualizações: &nbsp; " . intval(get_option("rics_pageViews_count_$id"));
  54.         $html .= '</div>';
  55.         $content = $content . $html;
  56.     }
  57.     return $content;
  58. }
  59.  
  60. // ADD STYLES
  61.  
  62. add_action('wp_enqueue_scripts','rics_load_plugin_css');
  63. function rics_load_plugin_css(){
  64.     wp_enqueue_style( 'rics-styles', plugins_url( 'rics-styles.css' , __FILE__ ) );
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement