Structured data helps search engines understand your articles. This tutorial shows how to generate valid BlogPosting JSON-LD from your PHP article data, safely escaped, and how to test it.
Structured data is a standardised way of describing a page to search engines. For a blog article, BlogPosting markup tells Google and other engines the headline, author, publication date and main image in an unambiguous format. It does not guarantee any special search feature, but it removes guesswork and helps search engines display your content accurately.
This tutorial shows how to output BlogPosting markup from a PHP site using JSON-LD, the format Google recommends.
What you need from your database
Most blog tables already contain everything required:
- Title (headline)
- Canonical URL of the article
- Featured image URL
- Published date and last-modified date
- Author name and author profile URL
- Short description or excerpt
Google's article structured data documentation lists the recommended properties. There are no strictly required ones, but headline, image, datePublished, dateModified and author are the ones that matter most.
Step 1: Build the data as a PHP array
Never build JSON by concatenating strings. Build an array and let PHP encode it, which handles quotes and special characters correctly.
<?php
// $post comes from your database query
$schema = [
'@context' => 'https://schema.org',
'@type' => 'BlogPosting',
'mainEntityOfPage' => [
'@type' => 'WebPage',
'@id' => $post['canonical_url'],
],
'headline' => mb_substr($post['title'], 0, 110),
'description' => $post['excerpt'],
'image' => [$post['image_url']],
'datePublished' => date(DATE_ATOM, strtotime($post['published_at'])),
'dateModified' => date(DATE_ATOM, strtotime($post['updated_at'])),
'author' => [
'@type' => 'Person',
'name' => $post['author_name'],
'url' => $post['author_url'],
],
'publisher' => [
'@type' => 'Organization',
'name' => 'Your Site Name',
'logo' => [
'@type' => 'ImageObject',
'url' => 'https://www.example.com/logo.png',
],
],
];
DATE_ATOM produces ISO 8601 dates with a timezone offset, which is the format search engines expect.
Step 2: Encode it safely
Use json_encode with flags that keep URLs readable and, importantly, escape characters that could break out of a script tag:
$json = json_encode(
$schema,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP
);
JSON_HEX_TAG converts < and > into Unicode escapes, so a title containing </script> cannot close your script block. This matters whenever titles or excerpts come from user input.
Step 3: Print it in the page
<script type="application/ld+json"><?= $json ?></script>
Place it in the <head> or at the end of the <body>; both work. Output it only on article pages, not on category or listing pages.
Step 4: Get the details right
- Use absolute URLs for the canonical, image and author fields.
- Match what is visible. The headline, author and dates in the markup must match what readers see. Markup that describes content not on the page can be treated as spam.
- Only change dateModified for real updates, not for every page save or typo fix.
- Give authors a real profile page and use it as the author URL. Clear authorship supports trust.
- Use large images: Google recommends high-resolution images in several aspect ratios where possible.
Step 5: Add breadcrumbs (optional but useful)
A separate BreadcrumbList block describes the page's position in your site, for example Home, then Category, then Article. Build it the same way: an array of ListItem entries with position, name and item URL, encoded with json_encode.
Step 6: Test it
- Paste a live article URL into Google's Rich Results Test to check Google can read it.
- Use the Schema Markup Validator for general schema.org validation.
- After deployment, watch the Enhancements and structured data reports in Google Search Console for errors across the site.
Common mistakes
- Dates without a timezone or in a local format such as 03/04/2026
- Relative image URLs
- Broken JSON from hand-built strings with unescaped quotes
- The same markup copied onto every page of the site
Structured data is one part of technical SEO. For the rest, work through our blog SEO audit checklist.










0 Comments
No comments yet — be the first to share what you think.