Archived topic
add_action not working or working twice!
3 replies · Started by Giannis on April 1, 2022
function get_word_count(){
ob_start();
$word_count = str_word_count(get_the_content());
$time_to_read = count(preg_split('/\s+/', get_the_content())) / 150;
echo '<div class="timetoread"><span>Χρόνος Ανάγνωσης: '.round($time_to_read, 0).' Λεπτά</span></div>';
echo 'this?';
return ob_get_clean();
}
add_action( 'generate_after_entry_header', 'get_word_count');
This is not working. If I ommit the ob_start() and/or ob_get_clean , it works twice! Once, under the title and once under the toc.
Hi Giannis,
You shouldn’t need ob_start() and return ob_get_clean();
May we know how you’re adding this?:
function get_word_count(){
$word_count = str_word_count(get_the_content());
$time_to_read = count(preg_split('/\s+/', get_the_content())) / 150;
echo '<div class="timetoread"><span>Χρόνος Ανάγνωσης: '.round($time_to_read, 0).' Λεπτά</span></div>';
echo 'this?';
}
add_action( 'generate_after_entry_header', 'get_word_count');
I tested the code and I’m not getting the same issue.
Can you try the code without the TOC plugin? Can you also try using a different hook than generate_after_entry_header?
Kindly let us know how it goes. :)
so...
without ob. on generate_after_entry_title:
It works twice again! the thing is that I want my toc plugin! :p
And , without ob , it is not working as a shortcode placed inside the textcontent (ok I can live with it, no big problem.)
so this is what I get..
single: https://prnt.sc/l8PPnnGmbXKA (works 3 times!)
archive: https://prnt.sc/1QHrq75-1jNf (works as intended)
Hi Giannis,
Can you try using another hook other than generate_after_entry_header?
And can you link us to your site with the TOC plugin activated so we can see the structure of the post?