Post comments not allowed if comment metabox removed

Allow Comments in edit post

When building a site for clients it’s nice to clean out the Admin Dashboard screen of some of the unnecessary (and possibly dangerous) sections and checkboxes to avoid distractions when creating or editing their content. WordPress is flexible enough to allow sections, or meta boxes, to be easily removed through the use of the remove_meta_box function. The code can be added to the functions.php file.

One of the meta boxes that can be removed is the Comments or Discussion meta box. This box lives in the lower half of the Edit post screen. It allows you to set the post, or page, to accept or deny comments and trackbacks/pings. I’ve been working on a couple of sites for some clients and it was originally decided not to have comments at the launch. I removed the comments options from the post screen and removed the call to the comment template in the sites theme files.

Then the client changed their mind and would like comment functionality restored. Now, it’s some time into the life of the site and the previously created posts are showing that comments are closed and the client want to add the ability for all posts to allow comments. It is easy to edit the theme files and add back in the “comment_template()” code and that would be that. However, the posts still show “Comments are closed”.

In Settings, Discussion there are no restrictions for commenting so it’s a little confusing that comments are not showing.

After working a little further on this I found the trick, which can be a little time consuming if you have lots of posts. Either edit, or quick edit, each affected post and tick the box that says “Allow Comments”.

Allow Comments in edit post
Allow Comments in edit post

After doing that for each post you should find that normal comment functionality is restored for all posts and for new projects I always try and identify the comment requirements at the outset.

Slow WordPress tag queries

While working on a real estate project I encountered very slow response from WordPress on certain queries. These queries are multiple tag queries in the form:

http://www.site.com/tag/TAG_ONE+TAG_TWO

My application is using WordPress tags to annotate the content and I am providing links to those multiple tag queries throughout the site. Over time and while I was developing the site I noticed the site response getting slower and slower when I clicked on these links and yet there are times when the response was within the acceptable range. My hosting service was also affected and I was seeing high CPU usage when one of these queries was triggered. I decided to investigate.

I bounced between my Apache configuration, my MySQL configuration and the external database I was also querying. I had used the external database in a previous project and I was fairly confident I had not introduced any issues there. The queries I was performing were responding well within acceptable times and using mtop on my hosting console I was seeing WordPress queries hogging the resources.MySQL query

I changed the settings in my MySQL configuration file to increase buffers, heaps, table sizes and more. Improvements were minimal where there were any and in other cases no change. I didn’t think I was doing anything beyond the scope of WordPress (although I always like to learn) so I started searching to see if anyone else was experiencing the same or similar issues.

I found the usual and variable guides on how to speed up WordPress and more about taxonomy queries. I found there were reports of issues after a recent WordPress upgrade – support questions were posted here and here.

Digging deeper I did come across a WordPress Trac ticket http://core.trac.wordpress.org/ticket/16706 and that seemed to be very similar to my issue.  I have been keeping my installations up to date so I could see that I became affected at that same upgrade point. Reading the Trac ticket it seems a resolution to this issue is due in the next release of WordPress (3.2) but I wanted to find out if it really did fix my issue. I upgraded my site to 3.2 beta and applied the patch mentioned in the ticket. The difference is huge and the site response time is much closer to where it should be.

Reading through the Trac ticket the explanation of the issue and the fix is purely down to optimization of the query built when WordPress is required to display information based on multiple categories or tags.

**UPDATE**: It seems that these multi tag queries are operating efficiently after upgrading to WordPress 3.2+ and this hint/tip might no longer be valid or required.

Adding Child Theme filters and Twenty Ten

Working on a child theme project for a client I found that my filter modifications for both ‘excerpt_length’ and ‘excerpt_more” were not being processed. My child theme is based on Twenty Ten and the filters I was using had already been filtered by the parent theme. So, I had added my custom filters and the Twenty Ten filter removal code to my child themes functions.php.

However, these custom filters were not being actioned. To help me debug what was happening I even added PHP error logging to check the return values from the remove_filter functions and that proved the filters were not being removed prior to mine being added but I still couldn’t work out why.

Finally by searching the WordPress forums I came across this post and read the reply from lordarkad. It made sense. In my child theme I had to wait for the parent themes setup to finish before declaring my filter changes and this made more sense because the child themes functions.php is loaded prior to the parent themes functions.php. So whilst these filters showed in the parent theme code I was removing them before they had been applied and, of course, then the parent themes functions.php was applying it’s filters in the theme.

To fix it I followed lordarkad‘s comments and created my filter additions in a child theme setup function. Then I added that function to the “after_setup_theme” action so my function is called after the parent theme has complete setup and then my filters are applied correctly.

This is a snippet of what I ended up with:

add_action( 'after_setup_theme', 'mytheme_theme_setup' );

function mytheme_theme_setup() {
	if (!function_exists('mytheme_new_excerpt_length') ) {
		function mytheme_new_excerpt_length( $length ) {
			return 20;
		}
	}
	remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
	add_filter( 'excerpt_length', 'mytheme_new_excerpt_length' );
etc...
} // end of mytheme_theme_setup

What is the best way to speed up my blog?

Blog performance break the speed limit

There are times when you see your blog fly and other times it seems like it’s taking to an age to load. In your email you’ve got mail from users saying that the blog performance is degrading and not as good as it used to be. You keep checking that your blog cache program is enabled and wonder what else you can do. Well, you can wonder no more as I’ve collected the best tips on how to review your site configuration and restore the performance you and your users are used to.

Let’s take a look at the options you can explore to better improve the performance of your blog.

Blog performance break the speed limit
Blog performance break the speed limit

Blog Theme

Review your theme options, widgets and settings. Use firebug to see if all your theme images are loading correctly especially if you’ve experienced issues during a theme upgrade. Review your themes CSS files to see if there are references to images or other files that are no longer required or perhaps are missing. Disable any unnecessary widgets in your blogs dashboard. You may also check out your theme is generating valid code using the W3C Markup Validation Service or check with your theme developer or the support forum for your theme.

Blog Plugins

Simply put – do you need all the plugins activated on your site? Perform a quick plugin audit, comparing what you see on your home page and blog pages with your active plugins. If there are plugins that are active but not used then deactivate them one by one and refresh your blog page to see that it’s still all working. Sometimes going back to basics and deactivating all plugins is the only way to go. Also make sure you have the latest versions of the plugins you decide on keeping.

Blog Hosting

This can play a big part in the performance of your blog. With the big shift from shared (cheap) hosting to still cost effective and high performance VPS offerings like Slicehost (the host I use) and many others, there are more opportunities to gain improved performance by picking the right host. Look at your hosting package and compare the next one up with your existing host.

Blog Optimization

This can offer up some simple and not so simple tips to help you speed up the responsiveness of your blog. There are many tools you can use – Firebug add on in Firefox, Pingdom, YSlow another addon for Firefox and even Google has a performance tool for Firefox. You can even try Is My Blog Working which, along with Pingdom can give you an idea of the time it takes to load your site. You can compare the load time before and after any of the changes you make.

View the server log

This is probably the most technical tip in this post and varies wildly from host to host. I have found that sometimes it can lead you straight to a bottleneck because you see lots of errors being reported and at other times you are swimming through the log struggling to find what’s occurring. Perhaps with this one work with your hosting tech support or a WordPress consultant if you feel you are still struggling with blog speed.

Blog Caching

This is typically performed by a plugin but there are lower level server options available if you’re running on a VPS or non-shared server. Such options include APC – Alternative PHP Cache – and these can improve the server performance and that blends through to your WordPress performance.

Within WordPress there are a number of caching plugins that you can choose to use – WP-Super-Cache and W3-Total Cache. I’ve used both and both can benefit from tuning of their options so be sure to review the options carefully. The basis of these WordPress caching tools is that they generate a single html page of your posts / home page that the web server can deliver to a browser much more efficiently than processing the page on each request. Most of the caching plugins have multiple options for tuning how it should work on your blog – try changing the available options to see if the performance improves.

Blog Security

Blog Security
Blog Security

Securing your blog is important and there are many options that will help you. Now, there’s no substitute for good security but you might not want to put too many locks on the blog such that it takes too much time to do something simple. And each security layer can add a level of performance sapping complexity. Review your blogs security options and pare them down to the minimum necessary or within your security comfort level.

Blog Advertising

Advertising makes the blog world go around (along with B2B opportunities of course) and I’ve worked on one or two sites that have multiple advertising sections and there have been times where the site won’t completely load because it’s waiting for a piece of advertising to load. Review with your advertising contacts/services to see that you have the most optimal code for your advertising sections on your blog.

So, you can see there are many ways you can speed up your blog or at least tweak it for performance in some areas. If there’s a tip I’ve missed but you’ve used and benefited from then please leave a comment!

Integrating WordPress and Google-hosted jQuery 1.4

WordPress & jQuery
WordPress & jQuery

[Update] The generally accepted best practice is to no longer do this or use a plugin. More information in the WordPress codex

The release of jQuery 1.4 brings many javascript improvements in performance, better handling of attributes, manipulation of DOM events and many more. Head over to the jQuery site for more details and a breakdown of the new features and improvements.

That’s great and we all like speed and flexibility improvements. Now, how would you like these in your blog from today? Well a very quick and easy modification to your theme can have you utilizing the latest version of jQuery from Google’s CDN servers. I took the code block from the source site below, inserted it in my functions.php but replaced the jQuery version number. Check out the adjusted code block below:

if( !is_admin()){
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
   wp_enqueue_script('jquery');
}

See where I just put “..jquery/1/jquery…” – by referencing the “1” instead of “1.3.2” we will get the latest version of jQuery loaded on the site. A quick look at the source code and we can see that the inserted javascript source is indeed via Google’s CDN and by clicking on the source code we see it’s version 1.4. To use simple terms in this code we are telling WordPress to “forget” it’s usual jQuery source / script setting, then preparing WordPress for the new settings (please load jQuery from Google’s CDN) and then serving up the new information for WordPress to use.

A quick and easy way to give your blog / site the benefits of the recent jQuery updates and improvements. If you encounter an issue with your site or  jQuery after this change you can easily revert to the previous version (1.3.2) by either removing the code block you added or, to keep jQuery hosted by Google’s CDN, adjusting the version number from “1” to “1.3.2”. For those more familiar with jQuery coding there’s a list of “backwards incompatible” changes settings on the jQuery site.

If you’ve changed your site over to jQuery 1.4 how did it work out? As smoothly as mine did? Leave me a comment.

Source: http://digwp.com/2009/06/use-google-hosted-javascript-libraries-still-the-right-way/

WordPress 2.9 – adding image thumbnails to your blog

One of the new features of WordPress 2.9 and above is having a thumbnail associated with a post. It’s common to have themes that retrieve the first image found in a post and use that as the post thumbnail. But that may not be what you need. The inclusion of images in any post makes for a more interesting read but having to pick the first image to match the post content and have the responsibility of being the post thumbnail is now a thing of the past.

Adding a thumbnail is easy and is done

seanhayesbiz-thumbnail
Thumbnail button in media dialog

when editing your post. There is a new sidebar / widget called “Post Thumbnail” with a clicky called “Set Thumbnail”. At any point during the post writing process click on Set Thumbnail and the familiar media dialog will appear. Pick your image from the available images in your library or upload a new image. Select the image by choosing “Show” and then at the bottom of the dialog there’s a clicky labelled “Use as thumbnail”. When you click that the image you were viewing becomes the thumbnail associated with that post.

Now, if your theme doesn’t support the thumbnail feature in WordPress 2.9 you can make some quick modifications to your themes functions.php and then to any or all of the individual theme files you want the thumbnail to be active such as index.php, single.php, archive.php.

Thumbnail support is added to a theme by simply adding the following line to the end of your functions.php file (before the last line and “?>”).

add_theme_support( 'post-thumbnails' );

Then you add the modification for the index.php (and subsequently the others):

					<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
				<?php
					if ( has_post_thumbnail() ) {
						?><div class="wp-caption alignright"><?php
						the_post_thumbnail();
						?></div><?php
					} else {
						// the current post lacks a thumbnail
					}
				?>
					<?php the_content(''); ?>

For my theme, a great free theme from woothemes, I placed the thumbnail in it’s own wp-caption styled div and aligned it right in my posts.

Sources:

http://codex.wordpress.org/Post_Thumbnails
New in WordPress 2.9: Post Thumbnail Images