-
Hello Rank Math Support Team,
I am a plugin developer creating a custom frontend publishing tool for my website. I am trying to integrate with Rank Math to allow users to set the “Focus Keyword” from the frontend form.
I am running into a persistent issue where the focus keyword is not correctly displayed in the Block Editor UI after the post is programmatically created, even though the data appears to be saved to the database.
The Context
My plugin uses a frontend form that submits data to a custom WordPress REST API endpoint (/wp-json/my-plugin/v1/save-post). Inside this endpoint’s callback function, I am creating a new post and then attempting to save the focus keyword.The Code Used
After extensive debugging, I have confirmed that the following code is being executed successfully without any PHP errors. I am using the recommended \RankMath\Helper::update_post_meta() function after the post has been created.Here is the simplified logic from my REST API callback function:
/**
* Simplified logic inside my custom REST API endpoint callback.
*
* @param WP_REST_Request $request The REST API request object.
*/
function my_plugin_save_post_callback(WP_REST_Request $request) {// 1. Create the initial post to get a Post ID.
$post_data = [
‘post_title’ => ‘My Post Title from Frontend’,
‘post_content’ => ‘Some content here.’,
‘post_status’ => ‘draft’,
];
$post_id = wp_insert_post($post_data);// If post creation is successful, proceed.
if ($post_id && !is_wp_error($post_id)) {// 2. Get the keywords from the form submission.
// For this example, we’ll hardcode it.
$focus_keywords = ‘sikar news, today news’;// 3. Use the official Rank Math Helper function to save the keywords.
if (class_exists(‘RankMath\\Helper’)) {
\RankMath\Helper::update_post_meta(‘focus_keyword’, $focus_keywords, $post_id);
}
}// The function returns a success response.
}The Problem
Database Success: After this code runs, I can check the wp_postmeta table in the database and see that a meta key rank_math_focus_keyword has been correctly created for the new post with the value sikar news, today news.UI Failure: However, when I am redirected to the new post’s edit screen (/wp-admin/post.php?post=…&action=edit), the Rank Math “Focus Keyword” field in the Block Editor sidebar is empty.
The Workaround: If I then manually click the “Update” button in the editor, the page reloads, and the focus keywords suddenly appear correctly in the field.
This indicates that while the data is being saved to the database, the Rank Math UI is not correctly synchronized or initialized with this data when its save functions are called from a REST API context.
My Question
Is this a known limitation when interacting with Rank Math via a custom REST API endpoint? Is there a specific WordPress action I need to fire, or a different Rank Math function I should be using, to ensure the data is properly committed and recognized by the Block Editor UI on the initial page load after creation?Thank you for your time and any guidance you can provide.
Best regards,
Naveen Kumar
The ticket ‘Technical Issue: Programmatically Saving Focus Keywords via REST API’ is closed to new replies.