Skip Navigation

[Gelöst] Create Time to Read

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to use a PHP function that counts the number of characters in custom fields, then use that number to calculate an estimated reading time. I'm using this post as a template: https://birchtree.me/blog/reading-time-wp-php/

Solution:
You can use the types_render_field function in PHP to generate the HTML markup for each field, and add that to the content string like so:

function reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $content .= types_render_field('field_slug', array( "id"=>$post->ID) );
    $content .= types_render_field('other_field_slug', array( "id"=>$post->ID) ); // copy and paste this line as needed for additional fields
    $word_count = str_word_count( strip_tags( $content ) );
    ... code continues ...

If the HTML applied to each field should be stripped, use the get_post_meta function instead:

function reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $content .= get_post_meta($post->ID, 'wpcf-field_slug', true );
    $content .= get_post_meta($post->ID, 'wpcf-other_field_slug', true ); // copy and paste this line as needed for additional fields
    $word_count = str_word_count( strip_tags( $content ) );
    ... code continues ...

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created vor 6 Jahre, 2 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Artikel
#616039

I have seen these "Time to Read" plugins where it will grab the text from your page and estimate the length of time it will take to read the post, like this one: https://wordpress.org/plugins/reading-time-wp/

But this reads the post content section, and I have stripped that out with Toolset CPT.

Do you have a way that would allow me to add in all my custom fields (or even automatically grab all of the ones on the page) and then calculate the time to read in the same fashion.... allowing me to insert a shortcode on my page to display?

I might be able to modify this with your guidance: hidden link

Thanks for your help in advance.

#616133

Looks like you could append all your custom field content to $content variable to have their values counted in the $word_count variable. I don't know of a good way to automatically add all custom field values...you must add code for each field. Something like this (untested, just a guess):

function reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $content .= types_render_field('field_slug', array( "id"=>$post->ID) );
    $content .= types_render_field('other_field_slug', array( "id"=>$post->ID) ); // copy and paste this line as needed for additional fields
    $word_count = str_word_count( strip_tags( $content ) );
    ... code continues ...

We have documentation for the types_render_field function here, and examples for different types of fields:
https://toolset.com/documentation/customizing-sites-using-php/functions/

Note that types_render_field will add formatting and markup to the custom field values. If you want the raw values from the database, you should use get_post_meta instead. Types custom fields are stored with a prefix of "wpcf-", so the code might look like this:

function reading_time() {
    $content = get_post_field( 'post_content', $post->ID );
    $content .= get_post_meta($post->ID, 'wpcf-field_slug', true );
    $content .= get_post_meta($post->ID, 'wpcf-other_field_slug', true ); // copy and paste this line as needed for additional fields
    $word_count = str_word_count( strip_tags( $content ) );
    ... code continues ...

https://developer.wordpress.org/reference/functions/get_post_meta/

#625090

Yup, got it. Just had to define global $post.

Thanks!

#625368

Great, let me know if you run into any other problems with this code.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.