Decoding JSON information within WordPress

I'm looking to retrieve Instagram user account information such as followers, following, and account name.

The endpoint I have been using is:

https://www.instagram.com/{username}/?__a=1

By including the specific username in the endpoint URL, I am able to access a JSON page containing various data points like follower count, following count, and the account name.

In my WordPress website, I have implemented the following code snippet in the functions.php file and created a shortcode for easy display on a page:

function insta_shortcode_func() {
  $request = wp_remote_get('https://www.instagram.com/barkobco/?__a=1');

  if (is_wp_error($request)) {
    return false; // Exit early
  }

  $body = wp_remote_retrieve_body($request);

  $data = json_decode($body);

  return $data -> { 'count'};
}
add_shortcode('count_of_followers', 'insta_shortcode_func');

Despite implementing this code, the desired data is not being displayed. I am specifically aiming to show the follower count, following count, and the account name.

Answer №1

Make sure to use this code snippet for retrieving followers:

return $data->graphql->user->edge_followed_by->count;

Below is the complete code:

function insta_shortcode_func()
{
    $request = wp_remote_get('https://www.instagram.com/barkobco/?__a=1');

    if (is_wp_error($request)) {
        return false; // Exit early
    }

    $body = wp_remote_retrieve_body($request);

    $data = json_decode($body);

    return $data->graphql->user->edge_followed_by->count;
}

add_shortcode('count_of_followers', 'insta_shortcode_func');

I believe this solution will be beneficial for you

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why does my ngFor consistently refresh while the array remains unchanged?

Issue at hand: Whenever I launch this component, the ngFor div continuously updates and causes my RAM to deplete. Typically, ngFor is triggered when the array is updated; however, in my case, the array (announcements) only updates once in the constructor. ...

Encountering issues while establishing a connection to SQL Server through Node.js

I've developed a Node.js application to interact with SQL Server. Here's the code snippet: app.get('/SalesStatistics', function (req, res) { var Connection = require('tedious').Connection; // configuration for the databas ...

The dynamic Vue.js transitions and effects are like a magic

I am using a v-for to render multiple child components: <div v-for="(pass) in scoringPass" :key="pass.decision"> <Pass :pass="pass"/> </div> Each of these child components contains a transition tag: &l ...

Transferring JSON information from JavaScript to PHP

I have searched for various solutions, but have not found one yet. I am stuck with the following piece of code and my suspicion is that the jsondata arriving at the PHP script is empty. Unfortunately, I am clueless on how to debug this issue as the script ...

Get two cross-references using Node.js Sequelize

I'm currently trying to set up a route where two primary keys can be sent as foreign keys to the orderline table. In my setup, I have three tables: customer, address, and orderline. Each orderline entry should have a customer ID and an address ID as f ...

What is the reason for the binding event being triggered following the re-render of the setstate?

The Test component has a state of num. This component is designed to increase the value of num by 1 when a button is clicked. The button is linked to a self-increment method, and the keyup event is also connected to this method. When the method is triggere ...

ViewCheck and every

Can IsInView be set to wait for all elements with the same class, instead of calling them individually? For instance, if there are three sections, can they be called like this: $(".section").on('inview', function(event, isInView) { i ...

How to use JQuery UI sortable to automatically scroll to the bottom of the page

Having trouble with a few sortable tables and here is how I initialized the sortable object: var options = { helper: customHelper, handle: ".moveTargetDeliverables", containment: "#fieldset_deliverables_summary", tolerance: 'pointer&a ...

What is the best way to present these values with spaces in between each word?

When I use console.log(JSON.stringify(selected["1"]?.others)), the output is: ["Cars","Books","Necklaces"] On the screen, however, when displaying this data, all the words appear together without spaces. It looks li ...

Dynamically create a button using jQuery and JSON based on specific conditions (either true or false)

Is there a way to generate buttons with specified parameters only if a condition is true? Currently, the buttons are generated without checking conditions. Any help with the correct syntax would be appreciated. Sample JSON code: { "Caption": "Module ...

How to Use Vanilla JavaScript to Fetch a JSON File, Convert the Data into an Array, and Iterate Through Each Object

Imagine having a JSON file called map.json: { "images":{ "background": ["images/mountains.png","images/sea.png"] } } The goal is for JavaScript to retrieve "images/mountains.png" from map.json and us ...

Quickly extracting JSON data from a file

Hi, I'm new to swiftlyJSON and encountering an issue. I have data stored in a local JSON file and I need to extract a single object from it. I tried using swiftlyJSON but I'm unsure how to parse the file directly. My current solution involves con ...

svg-import.js encountered an import error stating "Unexpected type sodipodi:namedview in SVG Import."

Currently, I am utilizing svgjs to load SVG files and perform various manipulations. In addition, I have integrated the svg-import plugin to bring in pre-existing SVG files. However, when attempting to load an SVG file, I encountered the following error: ...

Exploring JSONObjects in Java for retrieving key values

I am a beginner in Java, so please forgive me if my question is basic. I have a JSON data like this: and I need to loop through based on the number of items in JSONObject: public static void main(String[] args, int value) { JSONParser parser = new JS ...

Issues encountered when trying to use default color classes in Tailwind CSS

I'm currently working on a React project that utilizes the Tailwind CSS Framework. To integrate Tailwind into my React app, I used NPM to install it in the following way: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p After i ...

Tips for fetching updated data in React.js using useContext after applying a filter

I have a React.js component that performs various filtering actions when a button is clicked. Here is the code I have written: import React, { useContext } from 'react'; import { ModelsContext } from "../context/ModelsContext"; const F ...

Streaming videos on Xbox One is not supported while in developer mode

Currently, I am facing a challenge with my UWP app that runs a web application made for Xbox One. There is a bug that only appears after approximately 4 hours of streaming video content, causing the app to freeze unexpectedly on the machine. The issue lie ...

What should I do to resolve the issue of ajax failing to update data randomly?

This script is designed to take the value entered into an HTML form and send it to the ../Resources/BugReport.php file. The data is then inserted into a database in that file, and the table in the ../Resources/BugDisplay.php file displays the information f ...

JQuery does not immediately update the input value

I'm working on a jQuery placeholder that mimics the behavior of default placeholders in Chrome and Firefox for browsers that don't support it. However, I'm facing an issue where the placeholder div's HTML doesn't change as quickly ...

Problem converting JSON between PHP and Python

Can you spot the mistake in my Python3 code? >>> import json >>> s = "\"{'key': 'value'}\"" >>> obj = json.loads(s) >>> obj['key'] Traceback (most recent call last): File "<s ...