Technical Issue: Programmatically Saving Focus Keywords via REST API

#1070547
  • Resolved Naveen Parmuwal
    Rank Math free

    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

Viewing 1 replies (of 1 total)
  • Hello,

    We apologize for the issue you are facing with saving focus keywords via the REST API. It seems like you’re on the right track by using the ank_math_helper::update_post_meta() function to save the focus keywords after creating the post.

    However, if the focus keyword is not displaying correctly in the Block Editor UI, it could be due to a few reasons:

    1. Meta Key: Ensure that you are using the correct meta key for saving the focus keyword. The meta key for Rank Math’s focus keyword is rank_math_focus_keyword. Make sure you are using this key when saving the data.

    2. Post Status: Since you are creating the post with a status of draft, ensure that your implementation correctly handles drafts and that the Block Editor can read the meta data from drafts.

    3. Cache Issues: Sometimes, caching plugins or server-side caching can cause issues with displaying updated data. Try clearing any caches to see if that resolves the issue.

    4. Rank Math Hooks: After saving the post and its metadata, you might need to trigger any necessary hooks or actions that Rank Math uses to refresh its data in the editor. Check if there are any specific hooks that need to be called after updating the focus keyword.

    If you continue to experience issues, please reply to this ticket with more details about your implementation, including any relevant code snippets or error messages, and a human agent from the Rank Math Support team will assist you further.

    Thank you for raising this concern!

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math

    This is an automated reply from Rank Math Support Bot. If you need further assistance, please reply to this topic and a human support agent will reply to you shortly.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘Technical Issue: Programmatically Saving Focus Keywords via REST API’ is closed to new replies.