Simple Function Using wp_remote_get()

There isn’t a good amount of information on wp_remote_get() in the codex. Take a look to see what I mean:

serverpoint hosting banner

https://codex.wordpress.org/Function_Reference/wp_remote_get

After you’ve used the function, the information it returns also has to be formatted in a certain way or you won’t be able to do anything with it.

wp_remote_get() Basics

Let’s start with a simple function:

With this function, we’re using wp_remote_get() to retrieve our URL. If there’s an error, we display the first sentence. If it worked, we display the second sentence.

Not much value in that so we need a few more lines of code.

What Comes Next?

Once we retrieve data from the URL, we need to format it correctly.

After we retrieve our data, we use wp_remote_retrieve_body() and json_decode() to process it and get what we need.

We can even go one step further and display our data.

This we’ll display the array of data that was returned.

Conclussion

When you need to hook into an API or retrieve data from a URL, wp_remote_get() is the function you need. Just remember that you’ll also have to take a few more steps in order to get to the actual data you want.

If you have any questions or feedback on this post, please use the comments below to start a discussion.

The post Simple Function Using wp_remote_get() appeared first on bavotasan.com.

You might also like More from author

Leave A Reply

Your email address will not be published.