Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active October 17, 2021 11:34
Show Gist options
  • Save EdwardBock/fa659b67bcf4f568289d0c49c175f42b to your computer and use it in GitHub Desktop.
Save EdwardBock/fa659b67bcf4f568289d0c49c175f42b to your computer and use it in GitHub Desktop.
Public render function of the reading time plugin with overwritable template
<?php
function reading_time_render($post_id){
// https://medium.com/write-better-wordpress-code/do-not-use-post-meta-fec12a7661
$db = new \PublicFunctionOrg\WordPress\ReadingTime\Database();
$reading_time = $db->get($postId);
if(!$reading_time) return;
// https://medium.com/write-better-wordpress-code/provide-overwritable-templates-from-your-plugin-c5205f8a3138
$templates = new \PublicFunctionOrg\WordPress\Templates();
include $templates->get_template_path("reading-time-template.php");
}
function reading_time_get_content($post_id){
ob_start();
// https://gist.github.com/EdwardBock/fa659b67bcf4f568289d0c49c175f42b
reading_time_render($postId);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
<?php
/**
* @var int $reading_time
*/
echo "<p>Estimated reading time: $reading_time minutes</p>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment