“`tool_code
from datetime import datetime
def generate_yoast_schema(
title: str,
description: str,
url: str,
primary_category: str,
image_url: str = None,
image_alt_text: str = None,
date_published: str = None,
date_modified: str = None,
author_name: str = “🔶AUTHOR NAME🔶”,
author_url: str = “🔶AUTHOR URL🔶”
):
“””Generates a Yoast-compatible schema.org JSON-LD string for a news article.Args:
title: Teh title of the article.
description: A brief description of the article (around 150 characters).
url: The canonical URL of the article.
primary_category: The primary category or topic of the article.
image_url: URL of the main image (optional).
image_alt_text: Alt text for the main image (required if image_url is provided).date_published: The original publish date in ISO 8601 format (YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS).
date_modified: The last modified date in ISO 8601 format (YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS).author_name: The name of the author.
author_url: The URL of the author’s page or profile.
Returns:
A JSON-LD string representing the schema.org markup.
“””
schema = {
“@context”: “https://schema.org”,
“@graph”: [
{
“@type”: “Article”,
“@id”: url + “#article”,
“isPartOf”: {“@id”: url},
“author”: {
“@id”: author_url + “#identity”
},
“headline”: title,
“datePublished”: date_published,
“dateModified”: date_modified,
“mainEntityOfPage”: {“@id”: url},
“wordCount”: “🔶WORD COUNT (INTEGER)🔶”,
“commentCount”: “🔶COMMENT COUNT (INTEGER)🔶”,
“publisher”: {
“@id”: “🔶PUBLISHER URL🔶#organization”
},
“image”: {
“@id”: image_url + “#primaryimage” if image_url else None
},
“thumbnailUrl”: image_url if image_url else None,
“articleSection”: [
primary_category
],
“inLanguage”: “en-US”,
“copyrightYear”: datetime.now().year,
“copyrightHolder”: {
“@id”: “🔶COPYRIGHT HOLDER URL🔶#organization”
},
“contentLocation”: {
“@type”: “Place”,
“name”: “🔶CITY, STATE/REGION🔶”
},
“description”: description,
“potentialAction”: [
{
“@type”: “CommentAction”,
“name”: “Comment”,
“target”: [
url + “#respond”
]
}
]},
{
“@type”: “WebPage”,
“@id”: url,
“url”: url,
“name”: title,
“isPartOf”: {
“@id”: “🔶WEBSITE URL🔶#website”
},
“about”: {
“@id”: url + “#article”
},
“primaryImageOfPage”: {
“@id”: image_url + “#primaryimage” if image_url else None
},
“datePublished”: date_published,
“dateModified”: date_modified,
“description”: description,
“breadcrumb”: {
“@id”: “🔶BREADCRUMB URL (usually the homepage)🔶#breadcrumb”
},
“inLanguage”: “en-US”,
“potentialAction”: [
{
“@type”: “ReadAction”,
“target”: [
url
]
}
]},
{
“@type”: “ImageObject”,
“@id”: image_url + “#primaryimage” if image_url else None,
“url”: image_url,
“contentUrl”: image_url,
“caption”: image_alt_text,
“inLanguage”: “en-US”,
“width”: “🔶IMAGE WIDTH (INTEGER)🔶”,
“height”: “🔶IMAGE HEIGHT (INTEGER)🔶”
} if image_url else None,
{
“@type”: “BreadcrumbList”,
“@id”: “🔶BREADCRUMB URL (usually the homepage)🔶#breadcrumb”,
“itemListElement”: [
{
“@type”: “listitem”,
“position”: 1,
“name”: “Home”,
“item”: “🔶WEBSITE URL🔶”
},
{
“@type”: “listitem”,
“position”: 2,
“name”: primary_category,
“item”: “🔶CATEGORY URL🔶”
},
{
“@type”: “ListItem”,
“position”: 3,
“name”: title,
“item”: url
}
]
},
{
“@type”: “WebSite”,
“@id”: “🔶WEBSITE URL🔶#website”,
“url”: “🔶WEBSITE URL🔶”,
“name”: “🔶SITE NAME🔶”,
“description”: “🔶SITE DESCRIPTION🔶”,
“inLanguage”: “en-US”,
“potentialAction”: [
{
“@type”: “SearchAction”,
“target”: “🔶SEARCH URL (e.g., https://example.com/?s={search_term_string})🔶”,
“query-input”: “required name=search_term_string”
}
]
},
{
“@type”: “Organization”,
“@id”: “🔶PUBLISHER URL🔶#organization”,
“name”: “🔶PUBLISHER NAME🔶”,
“legalName”: “🔶PUBLISHER LEGAL NAME🔶”,
“url”: “🔶PUBLISHER URL🔶”,
“logo”: {
“@type”: “ImageObject”,
“@id”: “🔶PUBLISHER LOGO URL🔶#logo”,
“url”: “🔶PUBLISHER LOGO URL🔶”,
“width”: “🔶PUBLISHER LOGO WIDTH (INTEGER)🔶”,
“height”: “🔶PUBLISHER LOGO HEIGHT (INTEGER)🔶”,
“caption”: “🔶PUBLISHER NAME🔶 logo”,
“inLanguage”: “en-US”
},
“image”: {
“@id”: image_url + “#primaryimage” if image_url else None
},
“sameAs”: [
“🔶PUBLISHER FACEBOOK PAGE (Optional)🔶”,
“🔶PUBLISHER TWITTER PAGE (Optional)🔶”,
“🔶PUBLISHER INSTAGRAM PAGE (Optional)🔶”,
“🔶PUBLISHER LINKEDIN PAGE (Optional)🔶”
]
},
{
“@type”: “Person”,
“@id”: author_url + “#identity”,
“name”: author_name,
“url”: author_url,
“sameAs”: [
“🔶AUTHOR FACEBOOK PAGE (Optional)🔶”,
“🔶AUTHOR TWITTER PAGE (Optional)🔶”,
“🔶AUTHOR LINKEDIN PAGE (Optional)🔶”
]
}
]}
# Remove any None values to keep the JSON clean
schema[“@graph”] =[itemforiteminschema[itemforiteminschema[“@graph”] if item is not None]import json
return json.dumps(schema, indent=2)
“`
“`tool_code
# Example usage:
title = “Pre-workout Supplements: Benefits, Risks, and What You Need to Know”
description = “Pre-workout supplements are popular in Brazil, promising increased focus and performance. But are they safe? A look at the benefits and risks.”
url = “https://example.com/pre-workout-supplements-risks-benefits”
primary_category = “Health & Fitness”
image_url = “https://example.com/wp-content/uploads/2024/01/pre-workout-supplements.jpg”
image_alt_text = “Various pre-workout supplement containers”
date_published = “2024-01-26”
date_modified = “2024-01-27”
schema_json = generate_yoast_schema(
title=title,
description=description,
url=url,
primary_category=primary_category,
image_url=image_url,
image_alt_text=image_alt_text,
date_published=date_published,
date_modified=date_modified
)
print(schema_json)
“`
