1

I have created some reports for some web pages that use JavaScript HTTP Requests to retrieve some data from an API. I would like to use the reports on some WordPress pages. I have done a lot of searching and reading. I am a complete beginner at customizing a WordPress page. While I don't mind experimenting, I would rather get some guidance for how to proceed.

The options appear to be:

  • Use a plugin to allow JavaScript on a WordPress page.

  • Use PHP to call the API, and then use PHP to create the HTML to display the data returned returned from the API.

I think the second one would be better because learning to use PHP on a WordPress page would allow for more options in the future.

I have worked in software development. I have done some programming on my own in JavaScript and Python3. I am currently going through some courses at Lynda.com to learn PHP. I am confident that I can pick it up or find what I need online to create the code I need.

I have found this plug-in: https://wordpress.org/plugins/php-everywhere/ It appears to allow me to use PHP on a WordPress page.

I also found this: WordPress has the HTTP API. https://codex.wordpress.org/HTTP_API

There are linked pages that explain how to make a request and get a response code and data: https://codex.wordpress.org/Function_Reference/wp_remote_retrieve_response_code https://codex.wordpress.org/Function_Reference/wp_remote_get

I have no problem understanding this because I did the web pages using JavaScript to get API data.

I just don't understand how to start using it. Is this a plugin? I don't think so. I think it is built-in code that I can just use.

So, can I just use the GET and Response Code functions in this reference in my PHP code once I have installed that first plug-in? (Did I mention that I am a complete beginner with customizing a WordPress page?)

I have found a number of websites explaining how to install a plug-in. I have found some websites explaining how to use the WP HTTP API. But, I need an even more basic explanation as to how to start with the WP HTTP API, and how to combine all of this. I haven't found that anywhere.

My next steps appear to be:

  1. Install that plug-in (php-everywhere). Write some simple PHP code to create some HTML to test it on a test page.

  2. Write a simple API call using the GET and Response Code functions from the Codex pages. Once I get some data successfully returned, I can then write more PHP code to incorporate the returned data. (I can use the web pages written in JavaScript as a guide to write the PHP to display the data.)

1 Answer 1

0

@Gina, and welcome!

I'm fairly new to the craft too, but I would love to share what I wish I knew from the very beginning.

Install that plug-in (php-everywhere). Write some simple PHP code to create some HTML to test it on a test page

When it comes to that I think it's better to just dig into the themes that produce the front-end. So here's some resources for you to read.

  1. Understanding the WordPress themes. Here's a link to the Theme Handbook. This will help you get around any theme, including the ones that come with every installation of WordPress;

  2. making and editing a child theme. I think editing a child theme is easier than creating one from scratch because one can just go back to the parent theme and copy an original file if one made a mess in his child theme' code;

  3. another thing that I think will be very helpful and will eliminate the need of using the php-everywhere plugin is the knowledge of how to make a page template. This will allow you to make a blank page that you can fill with whatever you want;

  4. of Course, if you get through the entire handbook you might want to start your own theme and there's a nice tool for that one too — _s gives you a barebones theme that you can upgrade.

Write a simple API call using the GET and Response Code functions from the Codex pages. Once I get some data successfully returned, I can then write more PHP code to incorporate the returned data. (I can use the web pages written in JavaScript as a guide to write the PHP to display the data.)

Once you have a child theme with your own page template you can write in it whatever you fancy. Some functions are better suited for the function.php file, but that, I think, will come with the practice and the knowledge.

Hope I helped, good luck!

5
  • So, if I learn themes/child themes, I won't need that php-everywhere plug-in. Is that correct? I can use PHP on a WP page. But, will that require me to install/use cURL for an API request? Or, can I just use the WordPress HTTP API ?
    – Gina
    Oct 12, 2018 at 17:11
  • Hmm...OK...I found this. It appears that the WordPress HTTP API uses cURL, and could make things simpler: wordpress.stackexchange.com/questions/185251/…
    – Gina
    Oct 12, 2018 at 17:24
  • Oh, wow. Thanks to your information, I did some reading and searching, and found this video about how to add PHP to a WP theme. So, now I am going to read the pages you linked more thoroughly to learn about Themes, Child Themes and page templates. I want a good understanding of what I am doing. youtube.com/watch?v=6-ivr_ECyXY
    – Gina
    Oct 12, 2018 at 17:28
  • @Gina I'm very glad to hear that! :) I'm not very familiar with the HTTP requests since I do mostly theme based front-end and I use pretty much only WordPress' functions, but I think one can hook some JavaScript and do some AJAX requests too. There's also jQuery that comes with the WP installation. Oct 12, 2018 at 18:21
  • 1
    You gave me a clear path to find out what I needed to do! Here is one page that helped me understand JavaScript API requests. The code examples are very good: taniarascia.com/how-to-connect-to-an-api-with-javascript
    – Gina
    Oct 12, 2018 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.