Making WordPress.org

Changeset 12202


Ignore:
Timestamp:
11/04/2022 06:30:19 AM (23 months ago)
Author:
coffee2code
Message:

Photo Directory, Head: Output image structured data to single photo pages.

Fixes #6543.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/head.php

    r11679 r12202  
    1616        add_filter( 'document_title_separator', [ __CLASS__, 'document_title_separator' ] );
    1717        add_filter( 'wp_resource_hints',        [ __CLASS__, 'wp_resource_hints' ], 10, 2 );
     18        add_action( 'wp_head',                  [ __CLASS__, 'json_ld_schema' ], 1 );
    1819    }
    1920
     
    6970    }
    7071
     72    /**
     73     * Outputs structured data.
     74     */
     75    public static function json_ld_schema() {
     76        $schema = false;
     77
     78        // Schema for individual photo pages.
     79        if ( is_singular( Registrations::get_post_type() ) && 'publish' === get_post_status( get_queried_object_id() ) ) {
     80            $contributor_name = get_the_author_meta( 'display_name', get_queried_object()->post_author );
     81            $schema = [
     82                "@context"           => 'https://schema.org',
     83                "@type"              => 'ImageObject',
     84                'contentUrl'         => wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0] ?? '',
     85                'license'            => 'https://creativecommons.org/share-your-work/public-domain/cc0/',
     86                'acquireLicensePage' => "https://wordpress.org/photos/license/",
     87                'creditText'         => $contributor_name,
     88                'creator' => [
     89                    '@type'          => 'Person',
     90                    'name'           => $contributor_name,
     91                ],
     92                'copyrightNotice'    => $contributor_name,
     93            ];
     94        }
     95
     96        // Print the schema.
     97        if ( $schema ) {
     98            echo PHP_EOL, '<script type="application/ld+json">', PHP_EOL;
     99            // Output URLs without escaping the slashes, and print it human readable.
     100            echo wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );
     101            echo PHP_EOL, '</script>', PHP_EOL;
     102        }
     103    }
     104
    71105}
    72106
Note: See TracChangeset for help on using the changeset viewer.