Use SmartCrawl API in React

I’m wondering if it will be simple to use the API to grab the SmartCrawl data to put into the React site, so we can still use SmartCrawl as a GUI for SEO, thanks in advance.

  • Adam Czajczyk
    • Support Gorilla

    Hi netn8

    I hope you’re well today and thank you for your question!

    I’ve asked our developers for consultation on this as I’m not really familiar with React myself (so I don’t quite know what would be necessary to make them work together). I’m awaiting their response and I’ll update you here as soon as I get to know more.

    Please note: developers’ response time might be a bit longer than ours here on forum so I’d appreciate some patience. I will, however, let you know immediately once I got information from them.

    Best regards,

    Adam

  • netn8
    • WPMU DEV Initiate

    Thanks so much Adam. Basically just looking for API calls or endpoints where we can grab the same data that SmartCrawl inserts into posts and pages on the WordPress site, so we can insert it into a static version of the React site.

    Have a terrific Tuesday,

    -Nathan

  • Adam Czajczyk
    • Support Gorilla

    Hi netn8

    Thanks for your response!

    I already had an “initial” talk to our developers and I got additional questions, if you don’t mind :slight_smile:

    They say that “the answer depends on how the React system is implemented” and are asking – I’m passing the question literally – which API is used, is it the one that comes with WP or something additional/custom?

    Best regards,

    Adam

  • netn8
    • WPMU DEV Initiate

    Hi Adam,

    They are using the standard WordPress REST API to grab post content and pull it into the React site. They’re also using Prerender to serve static HTML to the bots.

    I would have thought that the SmartCrawl data would be part of the meta objects that get returned with the post type endpoint, but the developers on my end are asking how they can grab the SEO data with React. I’m just the SEO person, so hoping you could provide some insight.

    Thank you.

  • Adam Czajczyk
    • Support Gorilla

    Hello netn8

    Thank you for this additional explanation!

    I got confirmation from our developers that you’re actually correct: we store our data as standard post meta so you should be able to retrieve it using standard WP Rest API. Post endpoint response does contain meta fields.

    That basically means that you shouldn’t need to pull any additional data or do it in any non-standard way – a standard retrieval of the post you’re working with should contain necessary data.

    However, if you’re experiencing some troubles with it, could you please ask your developers about the details of these specific issues during retrieval? An example of the code that they are using could also be helpful.

    I’d then pass it on again to our developers and I’m sure we can find the “way out” :slight_smile:

    Best regards,

    Adam

  • netn8
    • WPMU DEV Initiate

    Hello again Adam,

    Still waiting on a response for this. Could you give me an example API call that your developers would use to grab the Smartcrawl meta data? Speciifically we’re looking for title, description, keywords, open graph cards, and twitter cards.

    Thank you,

    -Nathan

  • Adam Czajczyk
    • Support Gorilla

    Hello netn8

    I’m sorry for delayed response on my end!

    I got a feedback from one of our developers who checked that and apparently the response does not indeed contain meta data by default. There’s, however, a simple solution to this that should do the trick.

    If you add this code to your site (e.g. via current theme’s “functions.php” file):

    register_rest_field( 'post', 'metadata', array(
    'get_callback' => function ( $data ) {
    return get_post_meta( $data['id'], '', '' );
    }, ));

    a standard call for a post should now return all the data, including all meta data – so also including all SmartCrawl data related to the post.

    Give it a try, please, and let me know if it worked for you.

    Best regards,

    Adam

  • netn8
    • WPMU DEV Initiate

    Ok thanks. I see how that works, but it’s also showing weird text from what should only appear in the admin dashboard. My concern is now all that info is public.

    Here is an example of some text that is being returned in the API call now:

    “Your SEO title is the most important element because it is what users will see in search engine results. You’ll want to make sure that you have your focus keywords in there, that it’s a nice length, and that people will want to click on it. Best practices suggest keeping your titles between 50 and 70 characters including spaces, though in some cases 60 is the sweetspot. The length is important both for SEO ranking but also how your title will show up in search engines – long titles will be cut off visually and look bad. Unfortunately there isn’t a rule book for SEO titles, just remember to make your title great for SEO but also (most importantly) readable and enticing for potential visitors to click on.

  • netn8
    • WPMU DEV Initiate

    Ok thanks. I see how that works, but it’s also showing weird text from what should only appear in the admin dashboard. My concern is now all that info is public.

    Here is an example of some text that is being returned in the API call now:

    “Your SEO title is the most important element because it is what users will see in search engine results. You’ll want to make sure that you have your focus keywords in there, that it’s a nice length, and that people will want to click on it. Best practices suggest keeping your titles between 50 and 70 characters including spaces, though in some cases 60 is the sweetspot. The length is important both for SEO ranking but also how your title will show up in search engines – long titles will be cut off visually and look bad. Unfortunately there isn’t a rule book for SEO titles, just remember to make your title great for SEO but also (most importantly) readable and enticing for potential visitors to click on.

  • netn8
    • WPMU DEV Initiate

    Ok thanks. I see how that works, but it’s also showing weird text from what should only appear in the admin dashboard. My concern is now all that info is public.

    Here is an example of some text that is being returned in the API call now:

    “Your SEO title is the most important element because it is what users will see in search engine results. You’ll want to make sure that you have your focus keywords in there, that it’s a nice length, and that people will want to click on it. Best practices suggest keeping your titles between 50 and 70 characters including spaces, though in some cases 60 is the sweetspot. The length is important both for SEO ranking but also how your title will show up in search engines – long titles will be cut off visually and look bad. Unfortunately there isn’t a rule book for SEO titles, just remember to make your title great for SEO but also (most importantly) readable and enticing for potential visitors to click on.

  • Adam Czajczyk
    • Support Gorilla

    Hi netn8

    Yes, it does because it’s supposed to return “all metadata”, as it’s just a simple code snippet. What you get there is a content of SEO Analysis. You can modify it slightly to return only the data that you want to fetch – regardless whether that’d be SmartCrawl data, data added (the same way) but other plugins or just your own custom fields.

    If you look at the output of your curl request (in fact you can simply open the same URL directly in the browser) you’ll be able to see the names of the meta fields – you can also see them in the database by looking into the “_postmeta” table.

    Let’s say that you only want to fetch SmartCrawl’s keywords and news keywords for the article. By looking at meta data you can see that relevant fields would be (accordingly) “_wds_keywords” and “_wds_news_keywords”. Knowing that you can modify the previous code slightly:

    register_rest_field( 'post', 'metadata', array(
    'get_callback' => function ( $data ) {

    $my_rest_meta = array(); // initialize empty array to avoid warnings

    // fetch only kewyords data and add to array
    $my_rest_meta['_wds_keywords'] = get_post_meta( $data['id'], '_wds_keywords', '' );
    // fetch only news keywords and add to array
    $my_rest_meta['_wds_news_keywords'] = get_post_meta( $data['id'], '_wds_news_keywords', '' );

    return $my_rest_meta;

    }, ));

    The difference in comparison to the previous version of this code is that in this case instead of fetching all available meta fields at once, you’re fetching only selected ones that you want to (may it be from SmartCrawl or any other meta data that’s stored in “_postmeta” table).

    Best regards,

    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.