-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonld-article-schema.php
More file actions
53 lines (49 loc) · 1.94 KB
/
Copy pathjsonld-article-schema.php
File metadata and controls
53 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Plugin Name: JSON-LD Article Schema for Posts
* Description: Adds JSON-LD Article schema to WordPress posts.
* Version: 1.0
* Author: Lucas Bezerra
* Author URI: https://github.com/bezerra-lucas
*/
function jsonld_article_schema_output() {
if (is_single()) {
global $post;
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'Article',
'mainEntityOfPage' => array(
'@type' => 'WebPage',
'@id' => get_permalink($post->ID),
),
'headline' => get_the_title($post->ID),
'datePublished' => get_the_date('c', $post->ID),
'dateModified' => get_the_modified_date('c', $post->ID),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta('display_name', $post->post_author),
),
'publisher' => array(
'@type' => 'Organization',
'name' => get_bloginfo('name'),
'logo' => array(
'@type' => 'ImageObject',
'url' => 'https://www.example.com/path/to/your/logo.png', // Replace with your logo URL
),
),
'description' => get_the_excerpt($post->ID),
);
if (has_post_thumbnail($post->ID)) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_data = wp_get_attachment_image_src($thumbnail_id, 'full');
$schema['image'] = array(
'@type' => 'ImageObject',
'url' => $thumbnail_data[0],
'width' => $thumbnail_data[1],
'height' => $thumbnail_data[2],
);
}
echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES) . '</script>';
}
}
add_action('wp_head', 'jsonld_article_schema_output');