RankMath and wordpress API

#880506
  • Resolved Thomas
    Rank Math pro

    Hello,

    I am creating my wordpress blog post through wordpess API, is it possible to update rank math seo title, meta descriptiom, slug, focuskeywords at the same time?

    Thanks,
    Thomas

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hello,

    We apologize for the issue you are facing. While the context does not provide specific details about using the WordPress API to update Rank Math SEO settings like the title, meta description, slug, and focus keywords simultaneously, it is generally possible to update post metadata through the WordPress REST API.

    To achieve this, you would typically need to send a request to the appropriate endpoint for updating posts and include the necessary metadata fields for Rank Math in your request body. However, for precise implementation details, we recommend checking the Rank Math documentation or the WordPress REST API documentation.

    If you have further questions or need more specific guidance, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further.

    Thank you for raising your concern!

    Hello,

    Thank you for contacting support.

    We don’t have any API calls that you can use for this purpose but since we save the data in custom fields on the database you can update the following fields using the WP REST API calls:

    
    rank_math_focus_keyword
    rank_math_title
    rank_math_description
    

    It’s important to note that to update the data of our plugin using the REST API you need to first register the metadata fields that you want to update and then use the WordPress REST API to update those custom fields using something similar to this: https://torquemag.io/2015/07/working-with-post-meta-data-using-the-wordpress-rest-api/

    Don’t hesitate to get in touch if you have any other questions.

    Hi, i’m trying to specifying meta Title and description with a rest API call (with python):

    
            post_data = {
                'title': post_elements['title'],
                'content': content,
                'excerpt': post_elements['excerpt'],
                'status': 'draft' if back_matter['status'].lower() == 'draft' else 'publish',
                'categories': categories,
                'tags': tags,
                'meta': {
                    'rank_math_title': 'Your title',
                    'rank_math_description': 'Your description'
                }            
            }
    

    The call request is working and no errors are returned, but i do not see values specified by me.
    I’m using the last version of wordpress and the last version of rank math.

    Hello,

    It looks like the custom fields might not be registered correctly. You’ll need to ensure that the meta fields (rank_math_title and rank_math_description) are registered for the REST API using register_meta() in your theme or plugin. Here’s a quick reference: https://developer.wordpress.org/reference/functions/register_meta/

    After registering, your REST API call should work as expected.

    Let us know how that goes. Looking forward to helping you.

    Hi Jeremy,
    thank you for the reply.

    I added this code to my functions.php file:

    
    function register_rank_math_meta() {
        // Registrazione del titolo RankMath
        register_meta('post', 'rank_math_title', [
            'object_subtype'   => 'post', // Specifica il tipo di post
            'type'             => 'string', // Tipo di dato
            'single'           => true, // Se è un singolo valore
            'sanitize_callback' => 'sanitize_text_field', // Callback di sanificazione
            'show_in_rest'     => [
                'schema' => [
                    'type'        => 'string',
                    'description' => 'RankMath SEO Title',
                    'context'     => ['view', 'edit'],
                ],
                'permission_callback' => function() {
                    return current_user_can('edit_posts');
                },
            ],
        ]);
    
        // Registrazione della descrizione RankMath
        register_meta('post', 'rank_math_description', [
            'object_subtype'   => 'post',
            'type'             => 'string',
            'single'           => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'     => [
                'schema' => [
                    'type'        => 'string',
                    'description' => 'RankMath SEO Description',
                    'context'     => ['view', 'edit'],
                ],
                'permission_callback' => function() {
                    return current_user_can('edit_posts');
                },
            ],
        ]);
    }
    add_action('init', 'register_rank_math_meta');
    

    But when i try to post, i get this permission error:
    Status code: 403, Response: {“code”:”rest_cannot_update”,”message”:”Sorry, you are not allowed
    to edit the rank_math_title custom
    field.”,”data”:{“key”:”rank_math_description”,”status”:403},”additional_data”:[{“key”:”rank_math_title”,”status”:403}],”additional_errors”:[{“code”:”rest_cannot_update”,”message”:”Sorry,
    you are not allowed to edit the rank_math_description custom
    field.”,”data”:{“key”:”rank_math_description”,”status”:403},”additional_data”:[{“key”:”rank_math_title”,”status”:403}]}]}

    The user has “editor” role and can create and edit posts. I tried even with Administrator role, but the error is still here.

    Any idea? Is there a specific permission that must be activated?

    Kind regards.
    Bart

    Hello,

    Your code to register the custom fields is missing the auth_callback property which is required to prevent this permission issue.

    Inside that callback, you can make a check to one of the multiple capabilities of WordPress that would apply to the user who will be submitting the requests via the API.

    Don’t hesitate to get in touch if you have any other questions.

    Hi Miguel,
    the permission callback is already defined in the register_meta parameters:

    
                'permission_callback' => function() {
                    return current_user_can('edit_posts');
                },
    

    Am I missing something?

    Hello,

    There’s no argument called permission_callback the only argument available is called auth_callback hence why we mentioned that it is missing.

    Thank you.

    Thank for the reply Miguel, i updated my functions.php, but in the meantime i developed a little plug-in to achieve the same thing with a dedicated call.
    This is a better solution for me because I do not want to use a child-theme. The plug-in supports Title, Description and Canonical URL.

    I just want to share the code with your community to help who need the same, but your system is blocking me (probably for the code i want to share…)

    Hello,

    It’s great to hear that you’ve developed a plugin to handle the updates for the SEO fields. Regarding sharing the code, our system might block certain content for security reasons.

    However, if you’d like, you can share your code by using a Pastebin

    Let us know if you need any further assistance.

    Thanks.

    Here is the correct link to the plugin source code (i thought it was possible to embed the paste bin)

    Hello,

    Thank you for sharing this with us. If you need help with anything else, please do let us know.

    Thank you for choosing Rank Math

    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 12 replies - 1 through 12 (of 12 total)

The ticket ‘RankMath and wordpress API’ is closed to new replies.