I am building my site base on the product data provided by the vendor. All product related pages are sitting on the vendor’s site.
The vendor allows me to create a “product summary” link to use it as a post in WordPress. Basically the post contains a list of products with brief infomation.
If I click on one of the products, it will take me to the “product detail” page which again it sits on the vendor’s server.
At this point all I can do is show the “product summary” post as a teaser on my front page.
What I really want to use as a teaser on the front page is a few products instead of product summary.
I used file_get_contents() to echo the product detail on my “detail” post:
function demo_file_get_contents(){
if(is_page('detail')){
echo file_get_contents('http://vendorsite.com/product.html');
}
}
add_action('wp_head','demo_file_get_contents');
Then I tried to capture the “green color” attribute, so I can assign this product to a “green” category and show the product as a group of green colored products teasers on the front page.
function demo_retriveAttr(){
if(is_page('detail')){
?>
<script type="text/javascript">
jQuery(document).ready(function() {
$('#color').load('http://mysite.com/detail.html #product-color');
});
</script>
<?php
}
}
add_action('wp_footer','demo_retriveAttr');
My questions are:
- Is file_get_contents the best practice to capture external data?
- the .load() function didn’t work, what did I do wrong?
Quick Answers
file_get_contentsis a good one, if your server has access to the website..load()function didn’t work, you might have to enable CORS. Cross Domain requests are generally blocked by the browser for safety.