Skip to content

Commit 8d52fa7

Browse files
committed
fix: enable rankmath metadata updates
1 parent 6d128be commit 8d52fa7

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/wordpress_markdown_blog_loader/blog.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,15 @@ def to_wordpress(self, wp: Wordpress) -> dict:
386386
)
387387

388388
metadesc = self.og_description if self.og_description else self.excerpt
389-
logging.warning("facebook and twitter description, canonical url and focus keywords can not be set")
390-
#result["meta"] = {
391-
# "rank_math_facebook_description": metadesc,
392-
# "rank_math_twitter_description": metadesc,
393-
#}
394-
# if self.canonical:
395-
# result["meta"]["rank_math_canonical_url"] = self.canonical
396-
#
397-
# if self.focus_keywords:
398-
# result["meta"]["rank_math_focus_keyword"] = ','.join(self.focus_keywords.split())
389+
result["meta"] = {
390+
"rank_math_facebook_description": metadesc,
391+
"rank_math_twitter_description": metadesc,
392+
}
393+
if self.canonical:
394+
result["meta"]["rank_math_canonical_url"] = self.canonical
395+
396+
if self.focus_keywords:
397+
result["meta"]["rank_math_focus_keyword"] = ','.join(self.focus_keywords.split())
399398

400399
return result
401400

@@ -432,6 +431,9 @@ def from_wordpress(
432431
"rank_math_canonical_url", blog.canonical
433432
)
434433

434+
if keywords := post.get("meta", {}).get("rank_math_focus_keyword"):
435+
blog.focus_keywords = keywords.split(",")
436+
435437
if post.permalink_template and not blog.permalink_template:
436438
# keeping the permalink template registered in the blog metadata.
437439
# Wordpress does not return the template that was set.

src/wordpress_markdown_blog_loader/upload.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,20 @@ def upsert_post(wp: Wordpress, blog: Blog) -> int:
3939

4040
if blog.og_image:
4141
og_image = wp.upload_media(f"{blog.slug}-og-banner", blog.og_image_path)
42-
post_og_images = []
42+
post_og_images = set()
4343
for name in ["rank_math_facebook_image","rank_math_facebook_image"]:
44-
post_og_images.append(post.get("meta", {}).get(name))
45-
if not next(
46-
filter(lambda b: b and b.get("url") == og_image.url, post_og_images), None
47-
):
48-
logging.warning("opengraph image %s is uploaded, but metadata is not updated", og_image.url)
49-
# logging.info("updating opengraph image to %s", og_image.url)
50-
# updated_post = wp.update_post(
51-
# blog.guid,
52-
# {
53-
# "meta": {
54-
# "rank_math_facebook_image": og_image.url,
55-
# "rank_math_twitter_image": og_image.url,
56-
# }
57-
# },
58-
# )
44+
post_og_images.add(post.get("meta", {}).get(name))
45+
if not og_image.url not in post_og_images:
46+
logging.info("updating opengraph image to %s", og_image.url)
47+
updated_post = wp.update_post(
48+
blog.guid,
49+
{
50+
"meta": {
51+
"rank_math_facebook_image": og_image.url,
52+
"rank_math_twitter_image": og_image.url,
53+
}
54+
},
55+
)
5956

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

0 commit comments

Comments
 (0)