Advertisement
geminilabs

[site-reviews] mapping custom schema types

May 27th, 2020
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. /**
  2.  * Changes the schema type based on the current page category
  3.  * In this example, we are using "CreativeWork" as the default custom schema type in the settings
  4.  * Paste this in your active theme's functions.php file.
  5.  * @param array $schema
  6.  * @return array
  7.  */
  8. add_filter('site-reviews/schema/CreativeWork', function ($schema, $args) {
  9.     $taxonomy = 'category'; // Change this if you are using a custom taxonomy
  10.     $mappedSchemaTypes = [ // [category name] => [schema type]
  11.         'Books' => 'Book',
  12.         'Movies' => 'Movie',
  13.         'Podcast' => 'PodcastSeries',
  14.         'TV Series' => 'TVSeries',
  15.         'Video Games' => 'VideoGame',
  16.     ];
  17.     if (is_array($categories = get_the_terms(get_the_ID(), $taxonomy))) {
  18.         foreach ($categories as $category) {
  19.             if (!array_key_exists($category->name, $mappedSchemaTypes)) {
  20.                 continue;
  21.             }
  22.             $schemaType = $mappedSchemaTypes[$category->name];
  23.             $schema['@type'] = $schemaType;
  24.             $schema = apply_filters('site-reviews/schema/'.$schemaType, $schema, $args);
  25.             break;
  26.         }
  27.     }
  28.     if (is_wp_error($categories)) {
  29.         glsr_log()->error($categories->get_error_message());
  30.     }
  31.     return $schema;
  32. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement