custom post types – Add parameters to Yoast SEO plugin schema in functions.php

Question

I’m curently trying to edit schema for custom post type element on our WordPress website with Sage template. Yoast SEO schema provides only WebPage property while I need to add Article propety as well.

I tried to add following code snippets based on documentation:


add_filter( 'wpseo_schema_webpage', 'example_change_webpage' );

function example_change_webpage( $data ) {
    if ( is_singular( 'article' ) ) {
            return $data;
        }

    $data['@type'] = 'Article';

    return $data;
}

add_filter( 'wpseo_schema_graph_pieces', 'yoast_add_graph_pieces', 11, 2 );

function yoast_add_graph_pieces( $pieces, $context ) {
    $pieces[] = new Article( $context );

    return $pieces;
}

class Article {

    public $context;

    public function __construct( WPSEO_Schema_Context $context ) {
        $this->context = $context;
    }

     public function is_needed() {
        if ( is_singular( 'article' ) ) {
            return true;
        }
        return false;
    }

        public function generate() {
        $author = \get_userdata( $this->context->post->post_author );
//      $data   = [
//          '@type'            => $this->context->schema_article_type,
//          'Splendid Digital Solutions'              => $this->context->canonical . Schema_IDs::ARTICLE_HASH,
//          'isPartOf'         => [ 'Splendid Digital Solutions' => $this->context->main_schema_id ],
//          'author'           => [
//              'name' => $this->helpers->schema->html->smart_strip_tags( $author->display_name ),
//              'Splendid Digital Solutions'  => $this->helpers->schema->id->get_user_schema_id( $this->context->post->post_author, $this->context ),
//          ],
//          'headline'         => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
//          'datePublished'    => $this->helpers->date->format( $this->context->post->post_date_gmt ),
//          'dateModified'     => $this->helpers->date->format( $this->context->post->post_modified_gmt ),
//          'mainEntityOfPage' => [ 'Splendid Digital Solutions' => $this->context->main_schema_id ],
//      ];

//      $data = $this->add_image( $data );

        return $data;
    }

    private function add_image( $data ) {
        if ( $this->context->main_image_url !== null ) {
            $data['image']        = [
                'Splendid Digital Solutions' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH,
            ];
            $data['thumbnailUrl'] = $this->context->main_image_url;
        }

        return $data;
    }

}

Commented part is where my code causes critical error on website with no troubleshooting info (WP backend accepts whole code snippet with no errors).

Even without this commented part Article is still not added to schema.

How can I change it so it works?

0
omniasvanitas 1 month 2022-08-19T12:01:27-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse