skip to Main Content

I’m trying to remove date properties from Article schema generated by the Yoast SEO plugin.

In their developer docs the wpseo_schema_article filter is set as an example for manipulating with Article graph piece. However even with this type="application/ld+json":

<script type="application/ld+json">
{
   "@context":"https://schema.org",
   "@type":"Article",
   "mainEntityOfPage":{
      "@type":"WebPage",
      "@id":"https://www.myproscooter.com/etwow-electric-scooters-review/"
   },
   "headline":"E-Twow Electric Scooters 2021 Review",
   "image":{
      "@type":"ImageObject",
      "url":"https://www.myproscooter.com/wp-content/uploads/2020/12/elek-scoot.jpg",
      "width":700,
      "height":400
   },
   "datePublished":"2020-12-08T08:52:13",
   "dateModified":"2021-01-12T11:30:10",
   "author":{
      "@type":"Person",
      "name":"Jason"
   },
   "publisher":{
      "@type":"Organization",
      "name":"MyProScooter",
      "logo":{
         "@type":"ImageObject",
         "url":"https://www.myproscooter.com/wp-content/uploads/2021/01/MPS-Logo-228x60.png"
      }
   }
}
</script>

When I try to access and manipulate data like this:

add_filter( 'wpseo_schema_article', 'remove_article_dates' );


function remove_article_dates( $data ) {

    file_put_contents(WP_CONTENT_DIR.'/helper-seo.txt','DATA PRE FILTER: '.print_r($data,true),FILE_APPEND);

    unset($data['datePublished']);
    unset($data['dateModified']);

    return $data;
}

Nothing gets logged into helper-seo.txt nor do dates get unset in the Article schema; as if the filter is ignored totally.

What’s more confusing is that manipulation with dates in Webpage Schema works and is similar to the above:

add_filter( 'wpseo_schema_webpage', 'remove_webpage_dates');

function remove_webpage_dates( $data ) {

    unset($data['datePublished']);
    unset($data['dateModified']);

    return $data;
}

The other stuff I’ve tried include:

add_filter( 'wpseo_schema_article_date_published', '__return_false' );

add_filter( 'wpseo_schema_article_date_modified', '__return_false' );

Which isn’t reflecting into Article schema at all. How to remove these properties sucessfully?

3

Answers


  1. i am using this code, it will work

    add_filter ( 'wpseo_schema_webpage' , 'remove_breadcrumbs_property_from_webpage' , 11 , 1 ) ;
    function remove_breadcrumbs_property_from_webpage( $data ) {
        if (array_key_exists('datePublished', $data)) {
            unset($data['datePublished']);
            unset($data['dateModified']);
        }
        return $data;
    }
    
    Login or Signup to reply.
  2. Try this exact code. I’m sure it will work for you. This cleared my problems at least.

    // Remove DatePublished
    add_filter( 'wpseo_schema_graph_pieces', 'remove_datePublished_from_schema', 11, 2 );
    add_filter( 'wpseo_schema_webpage', 'remove_datePublished_property_from_webpage', 11, 1 );
    
    /**
     * Removes the DatePublished graph pieces from the schema collector.
     *
     * @param array  $pieces  The current graph pieces.
     * @param string $context The current context.
     *
     * @return array The remaining graph pieces.
     */
    function remove_datePublished_from_schema( $pieces, $context ) {
        return array_filter( $pieces, function( $piece ) {
            return ! $piece instanceof YoastWPSEOGeneratorsSchemadatePublished;
        } );
    }
    
    /**
     * Removes the DatePublished property from the WebPage piece.
     *
     * @param array $data The WebPage's properties.
     *
     * @return array The modified WebPage properties.
     */
    function remove_datePublished_property_from_webpage( $data ) {
        if (array_key_exists('datePublished', $data)) {
            unset($data['datePublished']);
        }
        return $data;
    }
    Login or Signup to reply.
  3. None of those solutions worked for me, and I finally had to modify the Yoast plugin file wordpress-seosrcpresentersopen-grapharticle-modified-time-presenter.php and change line 32 return $this->presentation->open_graph_article_modified_time; to this instead return null; and yes I realize that’ll get overwritten by future plugin updates.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search