The Mechanics of wp_head Structured Data Injection
Many developers make the mistake of enqueueing JSON-LD via wp_enqueue_scripts or injecting it through client-side footer scripts. The gold standard recommended by Google Search Central is direct, server-rendered script output inside the document head.
Key Engineering Principles
add_action('wp_head', function () {
if (!is_singular()) return;
global $post;
$schema = [
'@context' => 'https://schema.org',
'@graph' => [
[
'@type' => 'Article',
'@id' => get_permalink($post->ID) . '#article',
'headline' => get_the_title($post->ID),
'datePublished' => get_the_date('c', $post->ID),
'dateModified' => get_the_modified_date('c', $post->ID),
'mainEntityOfPage' => get_permalink($post->ID),
]
]
];
echo '<script type="application/ld+json">' .
wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) .
'</script>' . PHP_EOL;
}, 1);