admin管理员组文章数量:1130349
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
GETrequest 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 );
});
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-
Widgetize the sidebar and create a custom widget containing the
wp_remote_getcall and parsing to output the HTML. – WebElaine Commented Dec 10, 2018 at 15:29 - How and where would I do that? – bigpotato Commented Dec 10, 2018 at 15:33
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
GETrequest 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 getWhere would I put my call to wpremoteget
版权声明:本文标题:wp remote get - Where would I put my call to wp_remote_get? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749111186a2317417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_remote_getcall and parsing to output the HTML. – WebElaine Commented Dec 10, 2018 at 15:29