‘get_the_date’ filter passes the post date’s HTML string to your callback, it is not an array. What you return should also be an HTML string. For schema, you need to include whatever HTML tags and attributes are required for your schema’s format. Note that the date itself may not be in the proper format for your schema, you may need to use PHP date functions to translate it to the correct format.
Thanks for the reply,
Is there a way I can get the html from the get_the_date function and then add my own html to it?
Sure, the HTML is already in $attr based on your current code. You can do something like this if the date format is appropriate:
return $opening_schema . $attr . $closing_schema;
Assuming of course you’ve defined $opening_schema and $closing_schema before this line.
More information can be collected by your callback if need be, $d is the PHP format string used and $post is the current post object.
add_filter('get_the_date', 'schema_get_the_date', 10, 3 );
function schema_get_the_date( $attr, $d, $post ) {
$opening_schema = 'opening schema tags, attributes here';
$closing_schema = 'closing schema tags here';
return $opening_schema . $attr . $closing_schema;
}
For example, if the current date string lacks information to properly format it, you can get the original date from the post object. There could also be other schema data in the post that may be useful.
Thank you for your help bcworkz. It was much apprecited. After about 4 days of trial and error, with your help I was able to decode how these filters work. In the end this is the code I ended up with
add_filter('cherry_get_the_post_date', 'schema_cherry_get_the_post_date', 10, 3 );
function schema_cherry_get_the_post_date( $output) {
$opening_schema = ' itemprop="datePublished" ';
return '<span' . $opening_schema . $output . '</span>';
}
Again, thanks mate!
the code is from a plugin that takes the data from using the get_the_time function
Happy to help!
I remember being totally confused with how filters and actions worked when I first started with WP. Just could not wrap my head around it for a good while. Then suddenly it just dawned on me how they worked and it all seemed so simple after that. Went from being totally baffled to full understanding in about 3 minutes. But only after being baffled for months.