{"id":14,"date":"2011-03-02T16:44:26","date_gmt":"2011-03-02T15:44:26","guid":{"rendered":"http:\/\/www.retibus.com\/en\/blog\/?p=14"},"modified":"2011-03-07T19:27:38","modified_gmt":"2011-03-07T18:27:38","slug":"embedding-wordpress-into-an-existing-website","status":"publish","type":"post","link":"http:\/\/www.nubaria.com\/en\/blog\/?p=14","title":{"rendered":"Embedding WordPress into an existing website"},"content":{"rendered":"<p>WordPress is the content management system that is used as the underlying software platform for a lot of blogs, including this one. When I decided to start the Retibus Blog, I briefly considered rolling out my own PHP-based solution in order to add a blog section to the website. I quickly dismissed that idea, though. It would have been <em>a lot<\/em> of work to get most of the features that people expect from blogs, like enabling readers to register as users, add comments, make permalinks available for the individual posts and so on. I then decided that it would be much more convenient not to re-invent the wheel and leave all the dirty work in the hands of WordPress. That was a wise decision. The WordPress software is very powerful and elegant, and makes it very easy to administer a blog. However, while it is quite easy to download the latest version, install it and set up a website wholly powered by WordPress, things are not that simple if you want to integrate a WordPress blog within an existing website, keeping a uniform look and feel across the blog and non-blog parts of the site. In this post I&#8217;m going to explain a technique to seamlessly integrate a blog powered by WordPress into an existing website.<\/p>\n<div style=\"text-align:center;padding:20px 0 20px 0;\">\n<img decoding=\"async\" src=\"..\/..\/..\/..\/..\/images\/blog\/wordpress_0.jpg\" width=\"560\" alt=\"A WordPress default installation integrated within a custom website\" \/>\n<\/div>\n<p><!--more--><\/p>\n<h2>1. Alternative approaches<\/h2>\n<p>There are actually three possible approaches if you want to add a WordPress-powered blog to an existing site:<\/p>\n<ol>\n<li>Rebuild the whole site using WordPress. WordPress lets you add custom pages where you can replicate the contents of the original site.<\/li>\n<li>Write your own PHP code in order to set up a <a href=\"http:\/\/codex.wordpress.org\/The_Loop\">WordPress loop<\/a> within the existing site.<\/li>\n<li>Tweak a WordPress theme so that the blog page mimics the look and feel of the rest of the site.<\/li>\n<\/ol>\n<p>The problem with the first approach is that it affects the whole site. If you&#8217;re building a new site from scratch, it may make sense to make it fully managed by WordPress, but if you just want to add a \u2018Blog\u2019 or \u2018News\u2019 section while leaving the rest unchanged, porting everything to WordPress pages may be a high price to pay. My decision was not to go that way, and use WordPress only for the \u2018Blog\u2019 section. This leaves us with the two other options.<\/p>\n<p>The second option consists in doing everything by hand. The WordPress documentation has <a href=\"http:\/\/codex.wordpress.org\/Integrating_WordPress_with_Your_Website\">an article<\/a> which explains the basic facts about how the WordPress loop works. This approach gives you a lot of flexibility. By calling functions such as <code>get_posts<\/code>, <code>query_posts<\/code> and <code>have_posts<\/code> you can retrieve the posts as plain text and use your HTML, CSS and PHP skills to format them as you wish. But it should go without saying that this approach is not for the faint-hearted. After you manage to display some posts, you will find that you need to provide a \u2018Comments\u2019 section, and then the permalinks, and then a way to navigate through the posts, and then a 404 error page&#8230; After a lot of time and effort, you&#8217;ll be the PHP programmer of the month, but you&#8217;ll probably still be struggling with the odd glitch here and there.<\/p>\n<p>The third option is more of a top-down approach, where we begin by setting up an ordinary WordPress blog and then modify its active theme to match the rest of the site. This is in my opinion the easiest way of integrating a WordPress blog within an existing website, and it is how the Retibus Blog itself has been implemented. The remainder of this post explains how to do this.<\/p>\n<h2>2. The basic steps<\/h2>\n<p>Let us suppose that we have a site <em>www.example.com<\/em>. We now want to add a new \u2018Blog\u2019 section powered by WordPress. We will need to follow the next steps.<\/p>\n<p>1. Create a <em>dummyblog<\/em> subdirectory and a <em>dummyblog.html<\/em> file that matches the general layout of the site. In this explanation I will assume that the file is HTML and that there is a separate <em>dummyblog.css<\/em> file for the styles, but everything should be the same if the skeleton file is a PHP file or if the styles are inlined.<\/p>\n<p>2. In the <em>dummyblog.html<\/em> file add a div element where you would like the blog to appear, like this:<\/p>\n<p><code>&lt;div class=&#34;my-website-blog-area&#34;&gt;Blog goes here.&lt;\/div&gt;<\/code><\/p>\n<p>Add a new CSS class to the <em>dummyblog.css<\/em> file, something like this:<br \/>\n<code><br \/>\n.my-website-blog-area<br \/>\n{<br \/>\nbackground-color:red;<br \/>\n}<\/code><\/p>\n<p>Now if you save this and check the <em>www.example.com\/dummyblog\/dummyblog.html<\/em> file, you should have a red area where the blog will be embedded, as shown in the picture below.<\/p>\n<div style=\"text-align:center;padding:20px 0 20px 0;\">\n<img decoding=\"async\" src=\"..\/..\/..\/..\/..\/images\/blog\/wordpress_1.jpg\" width=\"560\" alt=\"A custom website with a placeholder area for the blog contents\" \/>\n<\/div>\n<p>3. The next step consists in installing WordPress in a <em>blog<\/em> subdirectory of the site. I assume you know how to do this. If in doubt, refer to <a href=\"http:\/\/codex.wordpress.org\/Installing_WordPress\">the official documentation<\/a>. Once you&#8217;ve installed WordPress, the <em>blog<\/em> subdirectory will contain all the WordPress files including <em>index.php<\/em> whereas the sibling <em>dummyblog<\/em> subdirectory contains our non-WordPress <em>dummyblog.html<\/em> and <em>dummyblog.css<\/em> files. If you now access <em>www.example.com\/blog\/index.php<\/em>, you will see the WordPress blog with the default Twenty Ten theme:<\/p>\n<div style=\"text-align:center;padding:20px 0 20px 0;\">\n<img decoding=\"async\" src=\"..\/..\/..\/..\/..\/images\/blog\/wordpress_2.jpg\" width=\"560\" alt=\"A WordPress default installation displaying the TwentyTen theme\" \/>\n<\/div>\n<p>Now the problem we are faced with is that we have a fully functional WordPress blog with the Twenty Ten look and feel (<em>blog\/index.php<\/em>) and a dummy web page with a placeholder empty blog area (<em>dummyblog\/dummyblog.html<\/em>). The task ahead consists in tweaking the WordPress theme in order to incorporate the HTML elements of <em>dummyblog.html<\/em> into the web page that the WordPress file <em>index.php<\/em> generates.<\/p>\n<p>4. We now have to copy the WordPress default Twenty Ten theme. In order to do this, simply go to the <em>example.com\/blog\/wp-content\/themes\/<\/em> directory and make a copy of the <em>twentyten<\/em> directory, renaming it as <em>mywebsite<\/em>.<\/p>\n<p>5. In the new <em>example.com\/blog\/wp-content\/themes\/mywebsite\/<\/em> directory you will find a file <em>style.css<\/em>. Open it in a text editor.<\/p>\n<p>6. Edit the comment at the top so as to replace the Twenty Ten name and description with your own:<br \/>\n<span style=\"font-size: medium; font-family: monospace; color: #447700;\">\/*<br \/>\nTheme Name: My Website Theme<br \/>\nTheme URI: http:\/\/www.example.com\/<br \/>\nDescription: A custom modification of the WordPress Twenty Ten theme.<br \/>\n(&#8230;)<\/span><\/p>\n<p>7. Open the WordPress dashboard (<em>www.example.com\/blog\/wp-admin\/<\/em>) and check that there is now a second theme in the Themes menu:<\/p>\n<div style=\"text-align:center;padding:20px 0 20px 0;\">\n<img decoding=\"async\" src=\"..\/..\/..\/..\/..\/images\/blog\/wordpress_3.jpg\" width=\"560\" alt=\"WordPress dashboard showing the newly created theme\" \/>\n<\/div>\n<p>Note that WordPress uses the comment at the top of <em>style.css<\/em> to identify the name and description of the theme. If you want to have a different thumbnail image, you&#8217;ll just need to edit the <em>screenshot.png<\/em> file in the same directory.<\/p>\n<p>8. Activate the new theme. The blog still looks the same since the new theme is just a copy of Twenty Ten. Now comes the fun part.<\/p>\n<p>9. In the <em>themes\/mywebsite<\/em> directory find the file <em>header.php<\/em> and open it in a text editor. This file contains the HTML head and the first part of the body.<\/p>\n<p>10. In the head element, we need to merge those parts of the <em>dummyblog.html<\/em> file which are required for our site, such as links to JavaScript files and the favicon. In my test I added the highlighted text below:<\/p>\n<p><span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">&lt;!DOCTYPE html&gt;<br \/>\n&lt;html &lt;?php language_attributes(); ?&gt;&gt;<br \/>\n&lt;head&gt;<br \/>\n(&#8230;)<br \/>\nwp_head();<br \/>\n?&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace;\">&lt;script src=&#34;..\/..\/..\/..\/..\/js\/main.js&#34;&gt;&lt;\/script&gt;<br \/>\n&lt;link rel=&#34;icon&#34; type=&#34;image\/png&#34; href=&#34;..\/..\/..\/..\/..\/images\/favicon.png&#34;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">&lt;\/head&gt;<\/span><\/p>\n<p>You might want to change the way the title is processed too, but the Twenty Ten code does some nifty things like adding page numbers, so it is probably better to leave it as it is.<\/p>\n<p>11. We now continue editing the <em>header.php<\/em> file in order to modify the body part. You will find that the Twenty Ten theme has a div element of class <code>wrapper<\/code> inside the body element. That&#8217;s where we have to insert the HTML code. So, open <em>dummyblog.html<\/em> and copy all the HTML markup between <code>&lt;body&gt;<\/code> and <code>&lt;div class=&#34;my-website-blog-area&#34;&gt;<\/code>.<\/p>\n<p><span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">&lt;body &lt;?php body_class(); ?&gt;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace;\">(&#8230;)<\/span><span style=\"font-size: medium; font-family: monospace; color: #447700;\">&lt;&#45;&#45; Here goes all the HTML code between body and the my-website-blog-area div &#45;&#45;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace;\">&lt;div class=&#34;my-website-blog-area&#34;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">&lt;div id=&#8221;wrapper&#8221;&gt;<\/span><\/p>\n<p>12. Save the <em>header.php<\/em> file.<\/p>\n<p>13. Now open the file <em>footer.php<\/em> in the text editor. This is the file that generates the final part of the HTML document.<\/p>\n<p>14. In the <em>dummyblog.html<\/em> file copy the part from the closing <code>&lt;\/div&gt;<\/code> tag for the <code>my-website-blog-area<\/code> element up to the final closing <code>&lt;\/body&gt;<\/code> tag. In <em>footer.php<\/em>, paste the code just before the closing <code>&lt;\/body&gt;<\/code> tag:<\/p>\n<p><span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">wp_footer();<br \/>\n?&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace;\">&lt;\/div&gt;<\/span> <span style=\"font-size: medium; font-family: monospace; color: #447700;\">&lt;!&#45;&#45; my-website-blog-area &#45;&#45;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace;\">(&#8230;)<\/span><span style=\"font-size: medium; font-family: monospace; color: #447700;\">&lt;&#45;&#45; Here goes all the HTML code between the closing tag for the my-website-blog-area div and the closing \/body  tag &#45;&#45;&gt;<\/span><br \/>\n<span style=\"font-size: medium; font-family: monospace; color: #aaaaaa;\">&lt;\/body&gt;<\/span><\/p>\n<p>15. Save <em>footer.php<\/em>.<\/p>\n<p>16. Open the file <em>style.css<\/em> in a text editor together with <em>dummyblog.css<\/em>. Copy all the styles for your id and class selectors from <em>dummyblog.css<\/em>, and paste them at the end of the <em>style.css<\/em> file.<\/p>\n<p>17. Save these changes and load your site <em>www.example.com\/blog\/index.php<\/em>. There will probably be some glitches with the styles. The things to watch out for are:<\/p>\n<ul>\n<li>Be careful with name clashes. If <em>style.css<\/em> uses a name (like <code>footer<\/code>, for example) that your styling file also uses, you will have to use different names for those elements.<\/li>\n<li>The other problem is that the WordPress file <em>style.css<\/em> and your <em>dummyblog.css<\/em> may specify different styles for HTML elements like <em>body<\/em> or <em>p<\/em>. In such cases you will have to apply those styles to the div elements that surround the non-WordPress part, and reset those styles in the <code>my-website-blog-area<\/code> class. This may involve a bit of trial and error, but it should be relatively easy for anyone with a sound understanding of CSS.<\/li>\n<li>If your placeholder blog area has a width similar to the one assumed by the Twenty Ten theme (980 pixels) there should be no problem with the width of the layout elements. In the test I wrote for the screenshots I had a go at a trickier case where the blog area is narrower than usual. In such cases, it is necessary to look for the various hard-coded width values in the <em>style.css<\/em> file that affect the central column (basically all those that are larger than 600), and subtract from them the difference between 980 and the actual number of available pixels. If we choose to keep the Twenty Ten header, we will also need to edit the <em>functions.php<\/em> file to reduce the hard-coded width of the header image, and crop the image file itself.<\/li>\n<\/ul>\n<p>18. Finally you may want to get rid of the header and the footer of the default theme since your design probably has its own header and footer. In the Retibus Blog I have decided to delete the header part, but I have kept the default footer, which simply adds the name of the blog and the  \u2018Proudly powered by WordPress\u2019 text. <\/p>\n<h2>3. References and further reading<\/h2>\n<p>When I first tried to do this I found surprisingly few relevant results when googling for expressions such as \u2018embed WordPress into existing site\u2019. Aside from the official documentation pages at wordpress.org, there are four interesting articles I found on the web that helped me to figure out how to do this:<\/p>\n<ol>\n<li><em><a href=\"http:\/\/embedwordpress.com\/\">Free Tutorial: Embedding WordPress into an existing site<\/a><\/em>.<\/li>\n<li><em><a href=\"http:\/\/jonathanwold.com\/tutorials\/wordpress_integration\/\">Tutorial: How to Integrate WordPress into an Existing Website<\/a><\/em>.<\/li>\n<li><em><a href=\"http:\/\/idratherbewriting.com\/2008\/10\/23\/seamlessly-integrating-a-blog-into-your-non-blog-website\/\">Seamlessly Integrating a Blog into Your Non-blog Website<\/a><\/em><\/li>\n<li><em><a href=\"http:\/\/moshublog.com\/2005\/07\/05\/integrate\/\">Integrating WP in an existing site<\/a><\/em><\/li>\n<\/ol>\n<div style=\"border-style:solid; border-color:rgb(231,231,231); border-width:1px 0 1px 0; padding:8px 0 2px 0; margin:20px 0 20px 0;\"><div style=\"float:left\"><a href=\"http:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-count=\"horizontal\" data-via=\"retibus\">Tweet<\/a><script type=\"text\/javascript\" src=\"http:\/\/platform.twitter.com\/widgets.js\"><\/script><g:plusone size=\"medium\"><\/g:plusone><\/div><iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font&amp;height=24\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:400px; height:24px;\" allowTransparency=\"true\"><\/iframe><\/div><script type=\"text\/javascript\">(function() { var po = document.createElement('script'); po.type = 'text\/javascript'; po.async = true; po.src = 'https:\/\/apis.google.com\/js\/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); <\/script>","protected":false},"excerpt":{"rendered":"<p>WordPress is the content management system that is used as the underlying software platform for a lot of blogs, including this one. When I decided to start the Retibus Blog, I briefly considered rolling out my own PHP-based solution in &hellip; <a href=\"http:\/\/www.nubaria.com\/en\/blog\/?p=14\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7,5],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-php","category-web-design","category-wordpress"],"_links":{"self":[{"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14"}],"version-history":[{"count":44,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":59,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions\/59"}],"wp:attachment":[{"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nubaria.com\/en\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}