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

#913735
  • 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 4 replies - 1 through 4 (of 4 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

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

You must be logged in to reply to this ticket.