-1

I try to extract a certain part from an external WordPress page (namely the post) and then embed it in a simple site.

I tried with .load() but it doesn't seem to work and I don't really know how to proceed.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Consti P
  • 445
  • 5
  • 11
  • Can you share some code what have you done so far? – NewUser Apr 24 '17 at 12:23
  • By _extract certain part_ are you referring to the ability to [scrap a site](https://en.wikipedia.org/wiki/Web_scraping). Or do you want to display the entire external WP page to your site. Would really appreciate the code and detailed explanation of the problem. – Rashmirathi Tiwari Apr 24 '17 at 13:35

1 Answers1

0

Wordpress provides REST API for access to its posts, comments, pages, etc. For posts you can look at https://developer.wordpress.org/rest-api/reference/posts/#list-posts entry.

So, you can use next code to extract list of posts:

$.ajax({
  method: "GET",
  url: "http://your.website/wp-json/wp/v2/posts/",
  dataType: "json",
})
.done(function(response){console.log(response);})
.fail(function(xhr, status){console.log(status);});

Remember, you need to have CORS allowed for such kind of manipulations. See how to enable CORS in Wordpress at https://joshpress.net/access-control-headers-for-the-wordpress-rest-api/.

If it is not your website, read about how CORS affects your AJAX requests and what you can do with it at https://stackoverflow.com/a/17299796/2678487.

For example, you can use something like next to access Wordpress REST API on my website:

$.ajax({
  method: "GET",
  url: "http://cors-anywhere.herokuapp.com/blog.binaryspaceship.com/wp-json/wp/v2/posts/",
  dataType: "json",
})
.done(function(response){
  $('#result').html(
    JSON.stringify(response, null, 2)
  );
})
.fail(function(xhr, status){alert(status);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<pre id="result"></pre>
Community
  • 1
  • 1
metamaker
  • 2,307
  • 2
  • 19
  • 18