Posts Tagged ‘wordpress’

Loading a webpage in stages Last updated:1 March 2010

I reworked my homepage this weekend and wanted to display the latest Wordpress blog post on it. No problem there. However, the initial response times for my blog are sometimes quite slow, and this then meant that the whole of my homepage was slow to display, because the page was waiting for the blog to respond before displaying anything.

So what I wanted to do was display the home page immediately, and then load the blog post when it’s available. The solution was to use AJAX, which turned out to be very straightforward. More information on AJAX here, but essentially it allows a page to communicate with a server without refreshing the whole page. First things first – here’s the php code to retrieve the latest post from Wordpress as follows (I originally had this on the homepage itself):

<?php require($_SERVER['DOCUMENT_ROOT']."/blog/wp-blog-header.php");
query_posts('showposts=1');
while (have_posts()): the_post();?> 
<h2><a href="<?php the_permalink(); ?>" title="Blog">
Latest blog: <?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile;?>

I saved this code separately into a file called latest_post.php (stored in my “includes” folder).

In my homepage file I added some “placeholder” code where I want the blog post to appear. This will be displayed until the data comes back from Wordpress:

<div id="latest_post">
<h2>Latest blog: loading...</h2>
</div>

Note the id of latest_post assigned to the div. Then I added the following javascript code to the <head> section of the homepage

<script type="text/javascript">
window.onload = new Function("loadXMLDoc('/includes/latest_post.php');");
function loadXMLDoc(url)
  {
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET",url,false);
  xmlhttp.send(null);
  document.getElementById('latest_post').innerHTML=xmlhttp.responseText;
  }
</script>

The function loadXMLDoc retrieves the php file I’ve created above (latest_post.php) and then the last line replaces the text within the element with id “latest_post” with whatever is retrieved. This is standard ajax code which can be used for any purpose. Only the last line is specific to my page.

The code window.onload... calls this function when the page is loaded.

So, when the page is requested the php and html that’s on the page display the page with the placeholder text, but without the blog post, so the page loads nice and quickly. Once the page loads, the javascript is fired which calls the AJAX function to retrieve the blog post, and this loads on the page when it’s available.

This was the first time I’ve used AJAX, and it seemed quite a neat little solution, so I thought I’d post it here. This is the basic solution which could be further enhanced with some error handling.

Stop Wordpress adding <br/> tags Last updated:6 February 2010

Been swearing at Wordpress this afternoon…

I’ve been trying to transfer an html form, which I had working perfectly on a static html page, onto a Wordpress page. Wordpress insisted on adding multiple <br/> tags where I didn’t want them. Mutter.

Eventually, after much searching, I identified a parameter within wp-includes/formatting.php, which allows you to disable this irritating feature. If you open formatting.php and find the function wpautop you’ll see this:

function wpautop($pee, $br = 1)

Change this to:

function wpautop($pee, $br = 0)

Save the file and publish it back to your server and that’s it. Works fine in WP2.8.

Note: WP 2.8.3 overwrites formatting.php so this change needs redoing.

Wordpress and Joomla as CMS Last updated:20 October 2009

I had a quick look this week at the possibility of using either Wordpress or Joomla as a content management system, to enable me potentially to build a website but for someone else to keep it updated. Talking about a smallish site, maybe 20 pages max, mostly static content, nothing fancy…

I started with Joomla, easy to download, easy to install, didn’t find the user interface very intuitive (so neither would a less web-savvy user). The standard installation, however, came with about 3800 files….! Don’t know which ones I didn’t need so had to FTP the whole lot onto a server to test, not ideal. Created a couple of pages very easily using the standard template, but I thought the resulting html was excessively long, and used tables throughout. It was also invalid (a <p> tag within a <span> tag, if you’re interested).  And this was for two paras of lorem ipsum. Not a great first impression.

Then I tried Wordpress, much better, much smaller initial install (the logic being that you then add on the bits you want), better user interface, valid code, no tables.

No contest, then.

Joomla as CMS – round two Last updated:20 October 2009

Had another attempt at Joomla this week as I thought perhaps I hadn’t been quite fair last time

Slightly more success, and this time I seemed to get valid code, using the JA-purity template. From my point of view, I find it considerably less intuitive from a developers perspective, and this carries through to a non-technical user perspective.

I found adding an image rather clunky (it’s not great in Wordpress either), and was surprised to find the image form allowing me to enter hspace and vspace attributes (now deprecated). Having said that, when I entered values it had no perceptible effect. So why’s it there?

The other odd thing I thought was the menu. In Wordpress if you add a page, it adds it to the menu automatically, but in Joomla you have to manually add the menu item. I guess this is a double-edged sword, but for a non-technical user adding the menu seemed a bit clunky. What I did like about the Joomla menu is that it comes with a built in “suckerfish” style dropdown.

The code generated, however, still seemed to have an advanced case of div-itis, and an unnecessary table.

So generally a bit more positive, but from a simplicity perspective, solely from a non-techy user’s point of view, I still prefer Wordpress.

Body classes on Wordpress pages Last updated:8 September 2009

I spent hours this weekend trying to apply a different body class to a Wordpress page. Specifically, I wanted to apply a different class to Wordpress pages as opposed to Wordpress posts. Why? Because I wanted to be allow comments to be left on a number of pages which were previously outside Wordpress, but I wanted the menu, in line with the rest of my site, to display the submenu for either web pages or photo pages, depending on the actual page displayed. My css controls this by using the body class.

Once I’d worked it out it was straightforward, inevitably. First off, I used the “Custom Fields” functionality to define a custom field called bodyclass, then set this to “web” or “photo” as my css requires. Easy so far.

I then amended header.php using Wordpress conditional comments to use the bodyclass value for pages, and to use “blog” as standard for posts. The following statement did the trick here:

<body class="
<?php if (is_page()) echo get_post_meta($post->ID, 'bodyclass', true); 
else echo "blog";?>
">