rankmath / seo-by-rank-math

Rank Math is a revolutionary WordPress SEO Plugin that combines the features of many SEO tools and lets you multiply your traffic in the easiest way possible :bulb: :chart_with_upwards_trend: →
https://rankmath.com
107 stars 52 forks source link

Helper methods for retrieving SEO title and description #79

Closed UVLabs closed 3 years ago

UVLabs commented 3 years ago

We want to add support for custom SEO title and description created in Rankmath to be shared by our plugin: https://wordpress.org/plugins/tweet-old-post/

I was able to do this is Yoast using this guide: https://developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/

$title = YoastSEO()->meta->for_post($post_id)->title;
$description = YoastSEO()->meta->for_post($post_id)->description;

I can see that you previously answered something similar here: https://wordpress.org/support/topic/get-title-description-and-canonical-url-programmatically/ But that seems to be for frontend posts and seems to return empty when used on save_post WordPress hook.

Are there any methods available to set the post ID so this can also work from admin and via cron? These methods must also replace the variables used by the user (such as %title% %sep%) with actual data. Obviously I can't just pull the set SEO title and description from the DB because they might contain variables in them.

surajv commented 3 years ago

Hi @UVLabs, sorry for the late reply. Please try these:

Title RankMath\Helper::::replace_vars( '%seo_title%', $post_object );

Description RankMath\Helper::::replace_vars( '%seo_description%', $post_object );

UVLabs commented 3 years ago

seo_description

Hi @UVLabs, sorry for the late reply. Please try these:

Title RankMath\Helper::::replace_vars( '%seo_title%', $post_object );

Description RankMath\Helper::::replace_vars( '%seo_description%', $post_object );

For some reason this isn't working for me. I've tried both of them

$content = RankMath\Helper::replace_vars( '%seo_title%', get_post($post_id) ); $content = RankMath\Helper::replace_vars( '%seo_description%', get_post($post_id) );

But both returned empty results?

This is the snippet I have entered just as a test

image

Any idea what's going on? I can confirm the class exists, not receiving any errors simply an empty result

surajv commented 3 years ago

That's strange, it is working fine on our setups, could you please open a support ticket on https://support.rankmath.com so our devs can take a look?

hamd64 commented 3 years ago

Same issue, it works fine when used inside a normal page load but doesn't when used inside an ajax call.

My guess: It's related to variables not set (line no: 212 - function: Replacer->get_variable_by_id() )

Ref file: https://github.com/rankmath/seo-by-rank-math/blob/master/includes/replace-variables/class-replacer.php

hamd64 commented 3 years ago

For anyone coming here in the future: Here's the solution

It was indeed because of the variables not set (as I mentioned above). Reason: https://github.com/rankmath/seo-by-rank-math/blob/c3da067bb096c794d954dad6f932a73e6d6428a5/includes/replace-variables/class-manager.php#L132

Solution:

saving: $rank_math_vars = rank_math()->variables; retrieve: rank_math()->variables = $rank_math_var;

For my case:

Obvious tip: If you also storing it inside an HTML field: do serialize() and base64_encode() it.