From the Knowledgebase

Display PDF Download and Views in a Page or Post

Note: this code will only work with the Premium and Secure versions of the plugin and only with files served from the /wp-content/uploads directory on your server.

You will need to write a bit of PHP for this feature. The code can be added to a custom or child theme's functions.php file or by using a plugin that inserts PHP code into your site such as My Custom Functions or Functionality*.

The PHP code:

// Code for PDF Embedder Counts

function pdfemb_get_attachment_id($url, $invertscheme=false) {
   global $wpdb;

   if ($invertscheme) {
      $url = set_url_scheme($url, preg_match('#^https://#i', $url) ? 'http' : 'https');
   }

   $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url));
   if (is_array($attachment) && isset($attachment[0]) && $attachment[0] > 0) {
      return $attachment[0];
   }
   return false;
}

function pdf_embedder_counts($atts, $content = "") {
   if (!isset($atts['url'])) {
      return '<b>PDF Embedder counts requires a url attribute</b>';
   }
   $url = $atts['url'];
   $post_id = pdfemb_get_attachment_id($url);
   if (!$post_id) {
      $post_id = pdfemb_get_attachment_id($url, true);
   }
   if ($post_id) {
      $count_views = get_post_meta($post_id, 'pdfemb-views', true);
      $count_downloads = get_post_meta($post_id, 'pdfemb-downloads', true);
      if (is_numeric($count_views)) {
         $content .= 'Views: '.$count_views;
      }
      if (is_numeric($count_downloads)) {
         $content .= ' Downloads: '.$count_downloads;
      }
   }
   return $content;
}

add_shortcode('pdf-embedder-counts', 'pdf_embedder_counts');

You will need to use the Classic editor block if using the blocks editor or a Text/HTML/Code module with a drag and drop editor. In the page or post content, use the following shortcode replacing the path-to-pdf.pdf with your document's file path:

[pdf-embedder-counts url="path-to-pdf.pdf"]

Add the document title, thumbnail, or a link to the embed page and the end result will be something similar to this (the stats will update whenever the page is loaded):

* WP-PDF.com has no affiliation with either of these plugins
Top