Entries Tagged 'WordPress' ↓

Do Not Let Your Sidebar Slow Down Your Site

Post written by: Mike

Content is king on the internet, right? Yet we keep throwing more and more junk into our sidebars.

I’m in the process of cleaning up the sidebars on another of my sites because it was starting to scare me.

Gayla at Mom Gadget was getting pretty frustrated because of how slowly her site was loading. I frequently joke with her about all of the gadgets and gizmos on her sidebar so I knew she should try to fix it before considering moving it since moving it probably wouldn’t help the pageload time much.

So I volunteered to find a way to get it to load faster and started hitting the refresh button to figure out what was going on.

The left sidebar would pop right up, the right sidebar would hesitantly follow and then up to 10 seconds later the content in the middle would decide to arrive. Not good.

It looked like the images from the MyBlogLog and BlogCatalog widgets were causing the delay.

But how could I fix that without getting rid of them.

I dug into the php files and noticed that the call to the sidebar was happening in the index.php, single.php and page.php files before the content. (There is not archive.php file in the theme).

Without even bothering to dig through the style.css file, I knew that there was something that slammed the left sidebar all the way left and the right sidebar all the right and then the content just fills in between.

So, I moved that one line call to the sidebar down to the bottom of those two php files and magic began. The page loads faster.

Technically the page doesn’t actually load any faster. The page just appears to load faster because the content shows up first. The right sidebar still trudges along but nobody notices because they are busy reading through the posts.

As a side benefit, in the html file, the content shows up before any of the sidebar information. So search engines will find the content earlier in the file. I’m pretty sure that I have heard that search engines like to focus on the text toward the top of the html file.

Anyway, if your site makes you go find a snack while waiting for the page to load, you might want to see if you can simply move the sidebar call to later in the process.

If you think you might have a similar problem and want somebody to take a look, let me know.

One Way To Handle AdSense Ads on a Multi Author WordPress Blog

Post written by: Mike

Paul and I decided from the start that we wanted to monetize the site but didn’t want to spend a ton of time optimizing and tweaking and monitoring. Just something simple that could be a little carrot to keep us entertained and maybe pay the hosting costs.

We currently just have the AdSense text links just under the header of all pages and the AdSense medium rectangle before the comments on the single posts.

The tricky part was trying to figure out how to get ads to rotate the way we wanted. We decided that some kind of random 50/50 impressions split would be fine for now and I couldn’t find a WordPress plugin that would do it the way I wanted without needing to put some php directly in the AdSense code to figure out which publisher ID to use which made me a little squeamish.

In the end, I added a bit of code and figured I should share the info in case some other blog authors want to do something similar.

First off, because I don’t know how to get the variables to pass from one php file to another and since Google does not allow two publisher IDs on one page, all of the code that gets displayed for any given pageview is in one php file.

For the main page and the archives, we are just using the text ad under the header. So, in the archive.php and index.php we have this after the get_header and before the content divs start:

<? srand(time());
$random = (rand()%2);
if($random == 0)
{
?>
[My AdSense code]
<? }
elseif($random == 1)
{
?>
[Paul's AdSense code]
<?}
?>

The first two lines just get a random number that is either 0 or 1.  Then if it is 0, my ad shows and if it is 1, Paul’s does.

Nice and simple.

For the single.php, it starts out with the same code after the get_header. Then after the content and before the comments, there is another if/elseif.  This second time on the page, we don’t get the random number because we need to make sure that both ads on the single post get assigned to the same publisher ID.

There might be WordPress plugins out there that handle this with fancier reporting in WordPress or greater control in where the ads show up, but so far this has been working pretty well for us.  If we find out that the random zeros show up more than the random ones, we can just flip who gets displayed for each number until it starts to even out.

Adding WordPress Authors In Your Template

Post written by: Mike

Since this blog has two authors, we thought it might be nice to have an easy way for people to see which posts Paul wrote and which ones I wrote.

WordPress makes all of this pretty easy but you have to know where to look. The best resource is the WordPress Codex page for the Author Templates.

I thought some real world code examples might be useful, so here are the changes that I made to our template to get the author links to show up under the title of the posts and in the sidebar.

Link to author’s posts under the post title

I started with the while loop of the index.php file. We are using the Copyblogger theme as our base template so this is what it looks like now. The code to add the authors is in bold.

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p>Post written by: <?php the_author_posts_link(); ?></p>
<p class=”post_date”><?php the_time(’F jS, Y’) ?> — <?php the_category(’, ‘) ?></p>
<div class=”entry”>
<?php the_content(”Continue reading →”); ?>
</div>
<p class=”post_meta”><span class=”add_comment”><?php comments_popup_link(’No Comments’, ‘1 Comment’, ‘% Comments’); ?></span></p>
<?php endwhile; ?>

The code above adds the author’s name and a link to all of their posts below the title and before the date and category line.  I did almost exactly the same thing in the archive.php and single.php files.

Add links to each author’s posts in the sidebar

<li class="widget">
<h2>Authors:</h2>
<ul>
<?php wp_list_authors(); ?>
</ul>
</li>

The important part is in bold. The rest just blends it in as its own section in the sidebar so you should modify that to match the way your own template handles the individual blocks in the sidebar.

What happens when you click on one of the authors?

When you click on the Mike or Paul links, WordPress looks for an author.php file. If it finds one, it uses that to display the page. If not, it uses the archive.php. If there is no archive.php, it uses the index.php. No matter what, it will only display the posts of the author you selected.

WordPress Is An Ever Changing Beast

Post written by: Mike

I was working on hardcoding some things in the theme to one of my other WordPress blogs to reduce the number of database queries. I decided that I rarely change my Blogroll so I should just hardcode in the values.

While in the area, I noticed that the theme listed the number of posts next to each category name. I decided to get rid of the count but was unfamiliar with how that function worked. The theme used the function wp_list_cats() so I fired up the old WordPress Codex and started searching.

To my shock, I discovered that this function has been deprecated starting with WP 2.1 and we should all be using wp_list_categories() instead. Who new?

I made the change to the new function and got rid of the argument that displays the post counts for each category (show_count=1 if you decide that you want to turn that on).

I didn’t notice any blazing performance gains or really any changes at all but I guess at least I am safe if they decide to really get rid of wp_list_cats() for good in a future upgrade.  If you have been upgrading WP for a while without really taking a close look at your theme, you might want to make sure that you are not still using the wp_list_cats() function to display your category names.

How Not To Upgrade WordPress

Post written by: Mike

I decided to upgrade one of my blogs from WordPress 2.1.3 to 2.2.1 during lunch yesterday. I’ve done several upgrades in the past without any issues using the detailed upgrade instructions instead of the 5 step upgrade instructions.

Yesterday, I was not paying enough attention and messed things up badly. As a result, I could not access the blog after the upgrade. It appears that I deleted the wp-config.php file that the instructions clearly state not to delete.

Then, while FTP’ing the wp-config.php file back to the server I put it in the wrong directory. It ended up in the directory of a different blog that unfortunately happened to share the same database server.

At this point, I had my parenting blog that could not be accessed and my Iowa Hawkeye blog that was showing all of the posts from the parenting blog and no idea what was causing any of it. In fact, I didn’t even know that the Hawkeye blog had a problem yet.

I deleted everything from the parenting blogs directories and restored all of the backup files which included the wp-config.php. Everything was happy on the parenting blog.

Then, I started getting trackbacks in various places from the Talk Hawkeye blog and finally realized that it had issues. I looked at the wp-config.php file there and noticed the timestamp was really recent. So, I copied the file from an old backup of the directories and everything was back to normal.

That is how not to do a WordPress upgrade.

Fortunately, I did backup before the upgrade. Always backup the directories and database before any major changes.

Also, I recommend always using the detailed upgrade instructions paying careful attention to what needs to be deleted and what needs to be kept.

And never upgrade during lunch when you are not really paying attention.