I have a sidebar is supposed to loop through a bunch of job postings that are fetched from a GET request. The resulting HTML should look like this:
<div>
<h4 class="myapp-sidebar-heaader">Related Positions</h4>
<div>
<a href="#" class="myapp-related-job-link">
<h5 class="myapp-job-title">Job title really really long</h5>
<h5 class="myapp-job-location">Location</h5>
</a>
<a href="#" class="myapp-related-job-link">
<h5 class="myapp-job-title">Job title really really long</h5>
<h5 class="myapp-job-location">Location</h5>
</a>
</div>
</div>
Let's say for now that the API URL is .
I found wp_remote_get
( / ) but I'm not sure where I put it. Should I just put it in the view file?
I'm guessing I should make a function that:
- Calls
wp_remote_get("")
- Returns the array of job posting objects
in functions.php, but I'm not sure how I would export it so it would be available in the view.
I have a sidebar is supposed to loop through a bunch of job postings that are fetched from a GET request. The resulting HTML should look like this:
<div>
<h4 class="myapp-sidebar-heaader">Related Positions</h4>
<div>
<a href="#" class="myapp-related-job-link">
<h5 class="myapp-job-title">Job title really really long</h5>
<h5 class="myapp-job-location">Location</h5>
</a>
<a href="#" class="myapp-related-job-link">
<h5 class="myapp-job-title">Job title really really long</h5>
<h5 class="myapp-job-location">Location</h5>
</a>
</div>
</div>
Let's say for now that the API URL is https://jsonplaceholder.typicode/todos
.
I found wp_remote_get
( https://developer.wordpress/reference/functions/wp_remote_get/ ) but I'm not sure where I put it. Should I just put it in the view file?
I'm guessing I should make a function that:
- Calls
wp_remote_get("https://jsonplaceholder.typicode/todos")
- Returns the array of job posting objects
in functions.php, but I'm not sure how I would export it so it would be available in the view.
Share Improve this question asked Dec 10, 2018 at 15:25 bigpotatobigpotato 3352 gold badges6 silver badges17 bronze badges 2 |1 Answer
Reset to default 2I'm guessing I should make a function that:
- Calls
wp_remote_get("https://jsonplaceholder.typicode/todos")
- Returns the array of job posting objects
Yes, that still leaves the question of where to call it
in functions.php, but I'm not sure how I would export it so it would be available in the view.
Functions in functions.php
are available in templates, making it available in the view isn't an issue if you're using WordPress theme templates correctly
As for where you would call this function? In the place you want to display it. However, there are downsides to your approach:
- Doing a remote request in PHP to another server on the frontvend is one of the worst things you can do for performance and scalability
- If it's just a remote
GET
request with no authentication, you can do that in JS
Doing it in javascript instead would shift the work to the browser, and make page caching better as it wouldn't cache stale results.
So you could do something like this in javascript:
fetch('https://jsonplaceholder.typicode/todos')
.then(function(response) {
return response.json(); // decode the JSON
})
.then(function(json) {
// create a container dom node, and fill it with todo items
const container = jQuery('<div>', { id: "todos"
const todos = json.map( todo =>
jQuery( '<p></p>', text: todo.title)
);
container.append( todos );
// add it to the page inside a div with an ID, we're adding it all at once for performance reasons
jQuery('#sidebartodos').append( container );
});
wp_remote_get
call and parsing to output the HTML. – WebElaine Commented Dec 10, 2018 at 15:29