Hello world!

1 min read

[post-views]

———————————-

function site_estimated_reading_time($content=”, $wpm = 300) {
$clean_content = strip_shortcodes($content);
$clean_content = strip_tags($clean_content);
$word_count = str_word_count($clean_content);
$time = ceil($word_count / $wpm);
return $time;
}

function mysite_readingtime_shortcode() {
$readtime = site_estimated_reading_time( get_the_content() );
$returntext = ‘<div>RD: %d minutes</div>’;

$return sprintf($returntext, $readtime);
}
add_shortcode(‘readingtime’,’mysite_readingtime_shortcode’);

————————————

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

We also update the meta using the functionupdate_post_meta(). I usually prefer using this over add_post_meta() because, if there’s no meta in place, update_post_meta() will add it anyway.

Now we check if $processed_like is true. If the post meta updated, then it will be true, because update_post_meta() returns true on success. If it is true, we set the current permalink as the redirect.

The reason we have to do a redirect is that if we didn’t and just loaded the page with ?post_action=like still in the URL, if the user copied that URL or simply refreshed the page, it would add another like.

Thanks for following along with the tutorial and I hope it’s been helpful to you!

If you’ve got any questions about the above, please leave a comment below and I’ll get back you in anyway I can.

Leave a Comment

Your email address will not be published. Required fields are marked *