Issue retrieving meta tags via Rank Math API in Headless CMS

#895286
  • Resolved Vikas Bailwal
    Rank Math free

    Hi Rank Math Team,

    We are using WordPress as the backend with a React frontend, and we have enabled Headless CMS support in Rank Math. The REST API endpoint for a specific URL is enabled from the settings.

    However, we’re encountering an issue where we’re not receiving the meta tags added by Rank Math for a specific URL on our frontend. In the WordPress backend, the meta tags are displayed correctly on the pages, but when we integrate the API request in the frontend to fetch the meta tags, they are not being retrieved.

    Could you please provide a step-by-step guide on how to correctly implement this functionality in React and WordPress? Any detailed explanation of the correct way to set this up would be highly appreciated.

    Thank you in advance for your assistance!

    Best regards,

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

    We apologize for the issue you are facing with retrieving meta tags via the Rank Math API in your Headless CMS setup. Thank you for raising this concern.

    To help you troubleshoot and implement the functionality correctly, here are some steps to follow:

    1. Ensure Headless CMS Support is Enabled

    Make sure that you have enabled the Headless CMS Support option in Rank Math. You can do this by navigating to Rank Math SEO → General Settings → Others in your WordPress admin area. If the Others tab is not visible, ensure that you are using Rank Math in advanced mode.

    Enable Rank Math General Settings - Others section

    2. Make a GET Request to the API Endpoint

    You should send a GET request to the Rank Math REST API endpoint using the following syntax:

    https://your-domain.com/wp-json/rankmath/v1/getHead?url=https://your-domain.com/hello-world?url=https://your-domain.com/hello-world

    Replace your-domain.com with your actual domain and your-page-url with the specific URL you want to retrieve meta tags for.

    3. Whitelist the API Endpoint in Your Firewall

    If you are unable to access the Rank Math REST endpoint, it may be blocked by a security plugin or firewall. Ensure that you whitelist the following endpoint:

    https://your-domain.com/wp-json/rankmath/v1/getHead?url=https://your-domain.com/hello-world

    You can refer to our tutorials on how to whitelist Rank Math’s REST API endpoints if you are using plugins like Wordfence or Cloudflare.

    4. Handle Common Errors

    If you encounter errors while making requests, here are some common issues:

    • No Route Was Found Matching the URL and Request Method: This indicates that Headless CMS support is not enabled.
    • Invalid Parameter(s) URL: Ensure that you are using a complete URL and not just a slug.
    • Missing Parameter(s) URL: Make sure that your request includes the URL parameter as described above.

    Example of Fetching Meta Tags in React

    Here’s a simple example of how you might implement this in your React frontend:

    import React, { useEffect, useState } from 'react';
    
    const MetaTagsFetcher = ({ pageUrl }) => {
        const [metaTags, setMetaTags] = useState(null);
    
        useEffect(() => {
            const fetchMetaTags = async () => {
                const response = await fetch(`https://your-domain.com/wp-json/rankmath/v1/getHead?url=https://your-domain.com/hello-world?url=${pageUrl}`);
                const data = await response.json();
                if (data.success) {
                    setMetaTags(data.head);
                } else {
                    console.error('Error fetching meta tags:', data);
                }
            };
            fetchMetaTags();
        }, [pageUrl]);
    
        return ;
    };

    This component fetches the meta tags for a given pageUrl and sets them in the component state.

    If you continue to experience issues, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further. We’re here to help!

    Ok thanks for the reply. what we need to do in wordpress site? is there any code to implement there?

    We have already implemented the code on frontend for Fetching Meta Tags in React , given in documentation, but we still not getting the meta tags, do we need to implement anything on wordpress side(backend)? Any detailed explanation of the correct way to set this up would be highly appreciated.

    Hello,

    Looking at your website it seems like you don’t have the REST API from WordPress enabled as accessing the wp-json path returns a 404 page instead of the expected JSON of the page.

    For our implementation to work you need to make sure the REST API is not disabled from WordPress.

    Thank you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    All the content on frontend is fetched from wordpress with the help of REST API. REST API is enables from backend (wordpress).

    Bakend wordpress link is
    https://cggprelive.co.uk/competition/wp-login.php

    Only the meta tag api is not working please review it and help accordingly

    Hello,

    Even if we try to access the JSON data from that URL where you mention the WordPress is installed we get a 404 error, so there’s something wrong with the API calls on this website.

    When you can access the JSON data with the path we mentioned above, you will also be able to fetch the data from our plugin using the methods mentioned here: https://rankmath.com/kb/headless-cms-support/

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

    Can you please share the exact path you are trying from your end to get the jason data

    when we hit the below link

    https://cggprelive.co.uk/competition/index.php/wp-json/rankmath/v1/getHead

    it is giving error
    {“code”:”rest_missing_callback_param”,”message”:”Missing parameter(s): url”,”data”:{“status”:400,”params”:[“url”]}}.

    below is the screenshot

    https://prnt.sc/TxOxFoez9wrS

    Hello,

    You need to access the path wp-json on the domain you are calling this REST API route and make sure it doesn’t return a 404 page.

    For example, if you access https://rankmath.com/wp-json you’ll notice that it returns some JSON code, which means the REST API for WordPress is enabled on our website.

    The same cannot be said about yours where both domain paths you mentioned return a 404 error for that.

    Thank you.

    Hello,

    That means that your website does not have the proper server rules to handle the removal of the index.php from the URLs.

    You will need to discuss this with your hosting provider and make sure that you have the proper rules to enable the pretty permalinks for this website both in the settings and in the server rules.

    Thank you.

    when hitting this link “https://cggprelive.co.uk/competition/index.php/wp-json” now we are getting below data

    https://prnt.sc/NNAGc5eyY2tH

    Hello,

    Yes, that is supposed to happen but you always need to add the index.php path to the URLs which is a clear sign that your server is not correctly set up for WordPress as we mentioned before.

    There is a redirection in the server configuration file to remove the index.php entry point which on an Apache server looks like this:

    
    # BEGIN WordPress
    
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # END WordPress
    

    Since it seems like you are Nginx, you would need to adapt this to your server-specific stack but that is something outside the scope of our support.

    Thank you.

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

The ticket ‘Issue retrieving meta tags via Rank Math API in Headless CMS’ is closed to new replies.