Hello,
I would like to register the reading time of a post each time a post is saved.
I have this JS function :
if ($('.entry-content').length > 0) {
var txt = $('.entry-content')[0].textContent,
wordCount = txt.replace(/[^\w ]/g, '').split(/\s+/).length;
var readingTimeInMinutes = Math.floor(wordCount / 228) + 1;
var readingTimeAsString = readingTimeInMinutes;
$('.reading-time').html(readingTimeAsString);
}
It’s working when I’m on the single.php page, but I would like this value to be displayed on the Home.php page.
How would you proceed to
1. select the text inside ‘.entry-content’
2. get the reading time
3. register it an acf field
4. display it on the homepage ?
I would like to execute it, each time a post is saved.
I know I should use https://www.advancedcustomfields.com/resources/acf-save_post/, but I don’t know how to pass JS values to PHP.
Thanks!
Why use JavaScript at all?
Create a field for reading time in ACF.
create an acf/save_post action.
get the post content you want use to calculate, make the calculation and then use update_field() to save this value to the post.
then you can just display this field where it needs to be shown.