Title: adding schema to post date
Last modified: September 1, 2016

---

# adding schema to post date

 *  Resolved [schemaishard](https://wordpress.org/support/users/schemaishard/)
 * (@schemaishard)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/)
 * I am installing schema into my site and I am getting a bit stuck with the datePublished
   property.
 * I am trying to get :itemprop=”datePublished” to show up whenever the get_the_date
   function runs, but all that happens is i seem to overide all the info that is
   in the get_the_date function.
 * here is an example of the code i have installed in my functions.php:
 *     ```
       /* adds schema to post date */
       add_filter('get_the_date', 'schema_get_the_date');
           function schema_get_the_date($attr) {
           $attr['itemprop'] = 'datePublished';
           return $attr;
       }
       ```
   
 * how can i get that itemprop to be able to appear next to the date the post was
   published so that it can register as schema?

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632389)
 * ‘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.
 *  Thread Starter [schemaishard](https://wordpress.org/support/users/schemaishard/)
 * (@schemaishard)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632395)
 * 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?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632412)
 * 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.
 *  Thread Starter [schemaishard](https://wordpress.org/support/users/schemaishard/)
 * (@schemaishard)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632419)
 * 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!
 *  Thread Starter [schemaishard](https://wordpress.org/support/users/schemaishard/)
 * (@schemaishard)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632420)
 * the code is from a plugin that takes the data from using the get_the_time function
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632453)
 * 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.

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

The topic ‘adding schema to post date’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [get_the_date](https://wordpress.org/support/topic-tag/get_the_date/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/adding-schema-to-post-date/#post-7632453)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
