how to change the duration in VideoObject Schema to ISO 8601 format

#913735
  • Resolved Osafo Daniel
    Rank Math free

    Hello team,

    I have an issue with automatically generated videos … when I inspect video posts on my blog, the duration does not appear in ISO 8601 format.

    For example, instead of PT00H2M39S, it appears as 159S .. is there a way to resolve It or is there a general PHP code I can apply to my site to resolve this.

    Best Regards,
    Osafo Daniel.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    Ideally, the duration should be in ISO 8601 format. If you’ve manually added the Video schema on your page, please update the date with this format: https://rankmath.com/kb/video-schema/#duration

    However, you can use the following code snippet to ensure that the duration property in the schema complies with the ISO 8601 standard. Here is the filter code you can use:

    add_filter( 'rank_math/snippet/rich_snippet_videoobject_entity', function( $entity ) {
        if ( empty( $entity['duration'] ) ) {
            return $entity;
        }
    
        $time_parts = explode( ':', $entity['duration'] ); 
        $hours = isset( $time_parts[0] ) ? (int) $time_parts[0] : 0;
        $minutes = isset( $time_parts[1] ) ? (int) $time_parts[1] : 0;
        $seconds = isset( $time_parts[2] ) ? (int) $time_parts[2] : 0;
    
       
        $entity['duration'] = 'PT' . 
            ( $hours > 0 ? $hours . 'H' : '' ) . 
            ( $minutes > 0 ? $minutes . 'M' : '' ) . 
            ( $seconds > 0 ? $seconds . 'S' : '' );
    
        return $entity;
    } );
    

    Here’s how you can add filters/hooks to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Hi Adetayo,

    Thanks for the swift response .. the code changes the video duration format but this time around it uses the time the video was shared to the current time as the duration

    For example, using the same link I shared; instead of PT00H2M39S (2 Munites 39 Seconds) it now appears as P6DT15H (6 Days 15 Hours)

    Hello,

    This might happen if the duration is being calculated dynamically based on time differences.

    Here’s an updated code snippet that assumes the duration is provided in the correct format (H:M:S or M:S):

    add_filter( 'rank_math/snippet/rich_snippet_videoobject_entity', function( $entity ) {
        if ( empty( $entity['duration'] ) ) {
            return $entity;
        }
    
        $time_parts = explode( ':', $entity['duration'] ); 
        $hours = count( $time_parts ) === 3 ? (int) $time_parts[0] : 0;
        $minutes = count( $time_parts ) >= 2 ? (int) $time_parts[count( $time_parts ) - 2] : 0;
        $seconds = (int) $time_parts[count( $time_parts ) - 1];
    
        $entity['duration'] = 'PT' . 
            ( $hours > 0 ? $hours . 'H' : '' ) . 
            ( $minutes > 0 ? $minutes . 'M' : '' ) . 
            ( $seconds > 0 ? $seconds . 'S' : '' );
    
        return $entity;
    } );

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    This code works … Thanks so much brother.

    One more thing do you have a code to solve “Missing Url” in Author

    Hello,

    To resolve that particular error, you need to ensure that the author archives are enabled in Rank Math SEO. You can check and enable this feature by navigating to Rank Math SEO → Titles & Meta → Authors → Author Archives as shown in the screenshot below:

    Rank Math support

    Hope that helps. Thank you.

    Osafo Daniel
    Rank Math free

    Works fine .. Thanks so much!!

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘how to change the duration in VideoObject Schema to ISO 8601 format’ is closed to new replies.