How to integrate wordpress posts inside .net website
Have you ever encountered a situation where all of a sudden demand arises for having a wordpress blog posts inside another fully functional website?
Is your website on any other different technology than wordpress?
Do you want to have wordpress blog inside .net web application?
Does your users wants to write blogs on wordpress and you have already built a website on any other technology?
I have faced similiar situation where I had a website built on .net, and all of a sudden client wanted to have blogs posted by him(wordpress) in the website(.net).
If you are facing similar challenge, then you have landed on the right post.
It is not that difficult if you are comfortable with making web requests and reading data from JSON response.
WordPress has its own api to serve such requests made to a web domain.
It returns almost everything you need to create a blog(on different website with different technology) from existing wordpress website.
For learning more about the api goto
WordPress APIBelow is the example of creating a web request and reading response in a datatable:
try {
DataTable wordpressposts = new DataTable();
wordpressposts.Columns.Add("title");
wordpressposts.Columns.Add("postcontent");
wordpressposts.Columns.Add("postid");
wordpressposts.Columns.Add("postdate");
wordpressposts.Columns.Add("poststatus");
wordpressposts.Columns.Add("posttype");
wordpressposts.Columns.Add("postexcerpt");
wordpressposts.Columns.Add("featuredimage");
Net.WebRequest request = WebRequest.CreateHttp("http://anywordpresssite.com/wp-json/wp/v2/posts?_embed");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
JArray blogposts = JsonConvert.DeserializeObject(responseFromServer);
foreach (blogpost in blogposts) {
string featuredImageLink = "";
string title = blogpost.Item["title"].Item["rendered"].ToString;
string postcontent = blogpost.Item["content"].Item["rendered"].ToString;
string postid = blogpost.Item["id"].ToString;
string postdate = blogpost.Item["date_gmt"].ToString;
string poststatus = blogpost.Item["status"].ToString;
string posttype = blogpost.Item["type"].ToString;
string postexcerpt = blogpost.Item["excerpt"].Item["rendered"].ToString;
if (((posttype == "post")
&& (poststatus == "publish"))) {
string iDate = postdate;
DateTime oDate = DateTime.Parse(iDate);
postdate = (oDate.ToString("MMMM", new CultureInfo("en-US")) + (" "
+ (oDate.Day + (", " + oDate.Year))));
// Get cover image
try {
featuredImageLink = blogpost.Item["_embedded"].Item["wp:featuredmedia"].Item[0].Item["media_details"].Item["sizes"].Item["full"].Item["source_url"].ToString;
}
catch (Exception ex) {
}
if ((featuredImageLink == "")) {
featuredImageLink = "URL to any default featured image";
}
DataRow R = wordpressposts.NewRow;
R("title") = title;
R("postcontent") = postcontent;
R("postid") = postid;
R("postdate") = postdate;
R("poststatus") = poststatus;
R("posttype") = posttype;
R("postexcerpt") = postexcerpt;
R("featuredimage") = featuredImageLink;
wordpressposts.Rows.Add(R);
}
}
// Now you have all the posts in the wordpressposts datatable
}
catch (Exception ex) {
}
Also, don’t forget to include newtonsoft library, you can add it from nuget directly and can easily use it inside your web application.
Notes:
If you make request to
http://anywordpresssite.com/wp-json/wp/v2/posts
Then, it is going to return only posts data (Without any link to featured image).
Though it has a ‘media details id’ to create another request for getting featured image.
But, for better performance and minimal requests(faster loading time), you can simply add “?_embed” in the end of request.
By doing it you will have everything you need in response of a single request.
If you are getting wordpress posts from a website which has lots of posts, it is better to make request by using filters(latest 10 posts etc) in requests.
Otherwise you will have a issue loading speed of you webpage.
and you can make another web request when user scrolls through all the posts which has been already loaded into the webpage.
For achieving such functionality, move your code into the web method and and call the method by AJAX when user scrolls towards bottom of webpage.
Another thing, making request to /posts return an array of data, but while making a request to a single post returns data of one single post.
So while getting data of one single read the response into a JObject instead of JArray.
The following concept should be good enough to help you out whether you are using it with the .net web application or any other technology.
This post will give you enough inspiration/solution for solving challenge of having wordpress posts inside your .net website