How to build an expiring function in WordPress?

In this code we are getting the Publish Date using get_the_date() to retrieve the publish date of the current post. and then calculating the difference by comparing the publish date with the current date.

After this we conditionally output the script. The condition is if the publish date is within the last 30 days, output the data layer script.

You can achieve your desired results with the help of given code.

<?php
// This is to get the author ID of the current post.
$author_id = get_post_field( 'post_author', get_queried_object_id() );

// This is to get the publish date of the current post.
$publish_date = get_the_date( 'Y-m-d', get_queried_object_id() );
$current_date = date('Y-m-d');

// This is to calculate the difference in days between the publish date and the current date.
$date_diff = ( strtotime( $current_date ) - strtotime( $publish_date ) ) / ( 60 * 60 * 24 );

if ( get_the_author_meta( 'display_name', $author_id ) === 'AUTHOR1' && $date_diff <= 30 ) {
    echo '<script> var dataLayer = []; dataLayer.push({"author": "ALIAS_AUTHOR_1"}); </script>';
}
?>

tech