403 Error Updating Rank Math Custom Fields via REST API

#987117
  • Resolved Chayan Fernandez
    Rank Math free

    Hello,

    I’m trying to update the custom fields “rank_math_title” and “rank_math_description” via the REST API using my admin account, but I keep receiving a 403 error stating that I’m not allowed to edit these fields.

    I’m not very good at coding, but I added this code to my functions.php:

    function register_rank_math_meta() {
        // Register Rank Math SEO Title
        register_meta( 'post', 'rank_math_title', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema' => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Title',
                    'context'     => array( 'view', 'edit' ),
                ),
                'auth_callback' => function() {
                    return current_user_can( 'manage_options' );
                },
            ),
        ) );
    
        // Register Rank Math SEO Description
        register_meta( 'post', 'rank_math_description', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema' => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Description',
                    'context'     => array( 'view', 'edit' ),
                ),
                'auth_callback' => function() {
                    return current_user_can( 'manage_options' );
                },
            ),
        ) );
    }
    add_action( 'init', 'register_rank_math_meta' );

    Despite these changes, I still get the 403 error when updating the fields via the API. Could you please advise what I need to do to resolve this issue?

    Thank you for your help.

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

    We apologize for the issue you are facing with the 403 error when trying to update the Rank Math custom fields via the REST API. It seems that the authorization callback you have set is restricting access to users who can manage options, which is typically limited to admin users.

    Here are a few things you can check or modify:

    1. Check User Permissions: Ensure that the user account you are using to make the API request has the necessary permissions. The current_user_can( 'manage_options' ) check means only users with admin capabilities can access these fields. If you want other roles to have access, you might need to adjust this condition.

    2. Modify Auth Callback: If you want to allow more user roles to update these fields, you can modify the auth_callback function. For example, you could change it to:

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

      This would allow any user who can edit posts to update these fields.

    3. Check REST API Endpoint: Make sure you are using the correct REST API endpoint and that your request is properly authenticated. You should be sending a valid authentication token with your request.

    4. Debugging: If the issue persists, consider enabling debugging in WordPress to see if there are any additional error messages that could provide more insight into the problem.

    If you continue to experience issues after trying these suggestions, please reply to this ticket with your site URL and any additional details about your setup, and a human agent from our support team will assist you further.

    Sources:
    How to Make Your Theme Rank Math Compatible » Rank Math

    The bot’s answer didn’t work

    Hello,

    You may need to add a callback inside the registration you are doing for the postmeta fields and return true inside that callback.

    The name of the callback should be auth_callback and it should have a function returning true.

    This is not specific to our plugin but to the metadata registration of WordPress.

    Let us know how this goes.

    Thank you for your answer.

    Here is what I did, still returns a 403 error :
    “403 – {‘code’: ‘rest_cannot_update’, ‘message’: ‘Désolé, vous n’avez pas l’autorisation de modifier le champ personnalisé rank_math_description.’, ‘data’: {‘key’: ‘rank_math_description’, ‘status’: 403}}”

    function register_rank_math_meta() {
        // Enregistrement du champ Rank Math SEO Title
        register_meta('post', 'rank_math_title', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema'        => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Title',
                    'context'     => array('view', 'edit'),
                ),
                'auth_callback' => function() {
                    return true;
                },
            ),
        ));
    
        // Enregistrement du champ Rank Math SEO Description
        register_meta('post', 'rank_math_description', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema'        => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Description',
                    'context'     => array('view', 'edit'),
                ),
                'auth_callback' => function() {
                    return true;
                },
            ),
        ));
    }
    add_action('init', 'register_rank_math_meta');
    

    Hello,

    Thank you for your patience

    Please replace the line for auth_callback with this one:

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

    Looking forward to helping you.

    Chayan Fernandez
    Rank Math free

    Hello,

    I was using current_user_can(‘edit_posts’) before and it didn’t work, I tried again and still not working. I forgot to precise, I am using a child theme of blocksy, and the function.php I am editing is the one of that child theme.

    As I said, the posts are being published (using an admin user application key), but without the meta description and with the same error 403.

    Any solutions ?

    Thank you

    Hello,

    Please try the following auth callback instead:

    'auth_callback' => function() {
        return current_user_can('edit_others_posts');
    }

    Let us know if the issue persists.

    Chayan Fernandez
    Rank Math free

    Hey,

    Same problem with this code :

    function register_rank_math_meta() {
        // Enregistrement du champ Rank Math SEO Title
        register_meta('post', 'rank_math_title', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema'        => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Title',
                    'context'     => array('view', 'edit'),
                ),
                'auth_callback' => function() {
                    return current_user_can('edit_others_posts');
                }
            ),
        ));
    
        // Enregistrement du champ Rank Math SEO Description
        register_meta('post', 'rank_math_description', array(
            'object_subtype'    => 'post',
            'type'              => 'string',
            'single'            => true,
            'sanitize_callback' => 'sanitize_text_field',
            'show_in_rest'      => array(
                'schema'        => array(
                    'type'        => 'string',
                    'description' => 'Rank Math SEO Description',
                    'context'     => array('view', 'edit'),
                ),
                'auth_callback' => function() {
                    return current_user_can('edit_others_posts');
                }
            ),
        ));
    }
    add_action('init', 'register_rank_math_meta');

    Hello,

    Sorry for the delay in getting back to you.

    Unfortunately, these types of code customization fall outside the scope of our support and we cannot provide assistance for them.

    This is not an issue with the plugin itself, so our team cannot debug custom code to perform REST API calls, even if those are for metadata from our plugin.

    Don’t hesitate to get in touch if you have any questions about the plugin.

Viewing 9 replies - 1 through 9 (of 9 total)

The ticket ‘403 Error Updating Rank Math Custom Fields via REST API’ is closed to new replies.