Skip to content

Commit

Permalink
fix: enable rankmath metadata updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanholsteijn committed Feb 12, 2025
1 parent 6d128be commit 8d52fa7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
22 changes: 12 additions & 10 deletions src/wordpress_markdown_blog_loader/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,15 @@ def to_wordpress(self, wp: Wordpress) -> dict:
)

metadesc = self.og_description if self.og_description else self.excerpt
logging.warning("facebook and twitter description, canonical url and focus keywords can not be set")
#result["meta"] = {
# "rank_math_facebook_description": metadesc,
# "rank_math_twitter_description": metadesc,
#}
# if self.canonical:
# result["meta"]["rank_math_canonical_url"] = self.canonical
#
# if self.focus_keywords:
# result["meta"]["rank_math_focus_keyword"] = ','.join(self.focus_keywords.split())
result["meta"] = {
"rank_math_facebook_description": metadesc,
"rank_math_twitter_description": metadesc,
}
if self.canonical:
result["meta"]["rank_math_canonical_url"] = self.canonical

if self.focus_keywords:
result["meta"]["rank_math_focus_keyword"] = ','.join(self.focus_keywords.split())

return result

Expand Down Expand Up @@ -432,6 +431,9 @@ def from_wordpress(
"rank_math_canonical_url", blog.canonical
)

if keywords := post.get("meta", {}).get("rank_math_focus_keyword"):
blog.focus_keywords = keywords.split(",")

if post.permalink_template and not blog.permalink_template:
# keeping the permalink template registered in the blog metadata.
# Wordpress does not return the template that was set.
Expand Down
29 changes: 13 additions & 16 deletions src/wordpress_markdown_blog_loader/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@ def upsert_post(wp: Wordpress, blog: Blog) -> int:

if blog.og_image:
og_image = wp.upload_media(f"{blog.slug}-og-banner", blog.og_image_path)
post_og_images = []
post_og_images = set()
for name in ["rank_math_facebook_image","rank_math_facebook_image"]:
post_og_images.append(post.get("meta", {}).get(name))
if not next(
filter(lambda b: b and b.get("url") == og_image.url, post_og_images), None
):
logging.warning("opengraph image %s is uploaded, but metadata is not updated", og_image.url)
# logging.info("updating opengraph image to %s", og_image.url)
# updated_post = wp.update_post(
# blog.guid,
# {
# "meta": {
# "rank_math_facebook_image": og_image.url,
# "rank_math_twitter_image": og_image.url,
# }
# },
# )
post_og_images.add(post.get("meta", {}).get(name))
if not og_image.url not in post_og_images:
logging.info("updating opengraph image to %s", og_image.url)
updated_post = wp.update_post(
blog.guid,
{
"meta": {
"rank_math_facebook_image": og_image.url,
"rank_math_twitter_image": og_image.url,
}
},
)

if blog.image:
banner = wp.upload_media(f"{blog.slug}-banner", blog.image_path)
Expand Down

0 comments on commit 8d52fa7

Please sign in to comment.