WordPress Comments outside the WordPress Site (External Page)
I had been using Google Friend Connect for more than 2 years on my site various product pages for getting the feedback. It has worked great for me, but then all of sudden, Google decided to drop this service in favor of new Google+. While I do created new product support pages on the Google+, I find it a bit inconvenient for my visitors to leave my site and post the comments on some other website.
I searched for various feedback widgets and APIs, but nothing seem to integrate into my website well. Now I have been using WordPress for a long time on my site for my blogs, but most of my main site pages are static XHTML files, and I would like to keep them so because of extra control I get this way and due to some other technical issues. All that said, I have been a big fan of WordPress comments as well. These are nice and simple, plus when used with Akismet, you almost get rid of all the spams automatically (which is even more good than it sound at first).
So, I decided to find some way to use the WordPress page comments on my existing simple HTML pages without having to implement all existing pages in new or exiting WordPress site. After a bit of testing and tweaks, I was able to make it all work. So, I thought to share this with community in hope that it will help someone else, or I may even get input from users on how to best do this.
Now coming to the solution, the first thing I did was create a new Page Template under the following path:
/wp-content/themes/[theme-name]/comment.template.php
Here is how my template page looks like:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php /* Template Name: Product Comments */ ?> <link rel="stylesheet" href="http://blog.syedgakbar.com/wp-content/themes/personal/style.css" type="text/css" media="screen" /> <div style="padding-top:10px"> <?php comments_template(); ?> </div> |
<?php /* Template Name: Product Comments */ ?> <link rel="stylesheet" href="http://blog.syedgakbar.com/wp-content/themes/personal/style.css" type="text/css" media="screen" /> <div style="padding-top:10px"> <?php comments_template(); ?> </div>
As you can see above, I have only placed the comment_template call in the page, so this shows only the page comments and don’t show title or page content.
Now save this file, and now try to create a new page in WordPress. When you do this, you should see this newly created template in the Template dropdown in Page Attributes group-box (key for this is “Template Name: Product Comments” in comment of new template php file).
Okay, once this is done. Save the page and publish it. You can also preview it for testing. When you preview, note down the page-id. You are going to use it next. Next go to the HTML (or any other page of external site), and you can show these WordPress page comments using the iFrame on that page; something like this:
1 2 3 4 5 6 | <div> <script type="text/javascript" src="/js/productComment.js"></script> <iframe id='productComment' src="http://www.syedgakbar.com/blog/?page_id=229" width="100%" style="border:none" onload="OniFramLoaded('productComment', document.body.scrollHeight)"> </iframe> </div> |
<div> <script type="text/javascript" src="/js/productComment.js"></script> <iframe id='productComment' src="http://www.syedgakbar.com/blog/?page_id=229" width="100%" style="border:none" onload="OniFramLoaded('productComment', document.body.scrollHeight)"> </iframe> </div>
The Scrtipt tag above and the JavaScript function call in the above code is optional, and it’s used to re-size the parent page iFrame dynamically (based on comments page height) to avoid showing the scroll-bars in iFrame. For those of you who are curious about the content of productComment.js, here it’s:
1 2 3 4 5 | function OniFramLoaded(iframeID) { var frameWin = document.getElementById(iframeID) frameWin.style.height = frameWin.contentWindow.document.body.scrollHeight + "px"; } |
function OniFramLoaded(iframeID) { var frameWin = document.getElementById(iframeID) frameWin.style.height = frameWin.contentWindow.document.body.scrollHeight + "px"; }
That’s all. You can now create new Product page in the WordPress and show their comments anywhere on your external site too. I know it’s not very elegant solution, but it does look elegant when in action. See your self at:
http://www.syedgakbar.com/products/usb/
Tags: Comments, Page, WordPress
This entry was posted
on Sunday, July 1st, 2012 at 11:54 pm and is filed under WordPress.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Akbar,
OK so now is works! Especially when I use the same protocol (SSL throughout): https://gpowerdemo.org/test1111.html
Thank you!
Is it beyond the scope of this functionality to work cross domain? Not knowing where I should draw the javascript from, I tried both: https://gpowerhost.com/test2222.html and https://gpowerhost.com/test1111.html and I’m getting “[Error] Blocked a frame with origin “https://gpowerhost.com” from accessing a frame with origin “https://gpowerdemo.org”. Protocols, domains, and ports must match.”
Any ideas for how to get beyond that?
Hope I’m not pestering you too much, but this is very interesting to me and could be useful using WordPress as a “Comments Engine” on a few pages where I would not want to have WordPress on the same domain (for reasons such as security or complexity, or bloat).
Have a very nice day!
Edie
Edie,
You are correct that all URL parts i.e. protocol, domain and ports must match. As you may know this is done to avoid cross site scripting which when used by malicious scripts can do great harm.
I don’t know of any legal workaround for this. Some hacks may work, but they will break sooner or later.
Regards, Akbar
Hello Syed Akbar,
Thank you for the script and instructions.
I’m having a hard time getting it to work. I’ve posted a demo here: http://gpowerdemo.org/test1111.html and thought is you had a moment, you might take a look and see what I’m doing wrong.
getting the error: “ReferenceError: Can’t find variable: OniFramLoaded”
And of course the iframe doesn’t resize vertically. 🙂
Have a very nice day!
Edie
Hello Edie,
I think the issue is one small syntax error in your HTML. For the JavaScript comment include, the code on your sample page is like this:
<script type=”text/javascript” src=””https://gpowerdemo.org/permanent_js/gPowerComment.js”> </script>
But, here the two doublequores in the src attribute are wrong. It should be:
<script type=”text/javascript” src=”https://gpowerdemo.org/permanent_js/gPowerComment.js”> </script>
Try this and hopefully it should work fine.
Syed,
Thank you. I corrected the double, double quote and unfortunately is didn’t make a difference. Frustrating to me, as I really need this to work and all other things I tried didn’t work either. I briefly even tried calling your comments and your js (just to see if it made a difference) and the page displayed exactly the same. Of course maybe there is another reason for that. If not I suppose it rules out the js and the wordpress comments themselves.
I though it could be some kind of firewall or Selinux issue (blocking js data?), but I can confirm that neither is at fault.
It works so beautifully for you and I’m still at a loss.
Edie
Edie,
May be you fixed that, but on your test site, the problem is still the same:
http://gpowerdemo.org/test1111.html
Can you fix that too? I can then look at the problem, and help you from there.
Regards, Akbar
Akbar,
OK so now is works! Especially when I use the same protocol (SSL throughout): https://gpowerdemo.org/test1111.html
Thank you!
Is it beyond the scope of this functionality to work cross domain? Not knowing where I should draw the javascript from, I tried both: https://gpowerhost.com/test2222.html and https://gpowerhost.com/test1111.html and I’m getting “[Error] Blocked a frame with origin “https://gpowerhost.com” from accessing a frame with origin “https://gpowerdemo.org”. Protocols, domains, and ports must match.”
Any ideas for how to get beyond that?
Hope I’m not pestering you too much, but this is very interesting to me and could be useful using WordPress as a “Comments Engine” on a few pages where I would not want to have WordPress on the same domain (for reasons such as security or complexity, or bloat).
Have a very nice day!
Edie
ps sorry for the duplicate post, I accidentally forgot to “reply” to the thread (hopefully you can take care of that!) 🙂
Hello,
I’m creating a site in Dreamweaver and hope to use WordPress comments inside the Dreamweaver pages. I have WordPress uploaded, but realized I can’t modify the templates for the layout I want; hence Dreamweaver. I tried the code above, but must have done something wrong. Would you – for a fee – be up to modifying my page (once it’s finished, which should be by tomorrow) so that I can place WordPress comments in it? I can keep dabbling with the code you so nicely presented, but I went over everything and still can’t figure out what I did wrong.
Thanks either way.
Weller,
From you comments, I can’t tell what could be the wrong there. May be once you have this setup on somewhere, you can share the website URL and then I can look into this as I have free time. I will not take any kind of fee for the help. I do this blogging for fun, and would like to keep it this way. Thanks.
Regards, Akbar
Thanks for the post it helped me out alot. 🙂