David Nash

Wordpress Guru Sydney

WordPress upgrade “Briefly unavailable for scheduled maintenance. Check back in a minute.”

Posted on December 15, 2011

I just updated my WordPress installation from 3.2 to 3.3. The upgrade was running but then the page directed to this message:

Briefly unavailable for scheduled maintenance. Check back in a minute.

The front page and wp-admin both showed this unstyled message. After a quick search I found that simply deleting the .maintenance file via FTP will remove this. Perhaps it’s a bug in this version, perhaps something went wrong. But after deleting that file everything seems to be working well. And I must say I like the design tweaks in WordPress 3.3.

WordPress Theme Development Quick Start

Posted on December 9, 2011

I’ve created plenty of WordPress themes based on designs from clients. This is the process I usually follow.

1. Create a static HTML and CSS template. I create index.html and style.css.

2. Install WordPress

3. In the WordPress folder wp-content/themes/ I create a folder with the name of my new theme.

4. Copy my static HTML to the new theme folder.

5. Modify style.css, add the WordPress stylesheet header:

/*
Theme Name: THEME_NAME
Theme URI: YOUR_WEBSITE
Description: THEME_DESC
Version: 0.1
Author: YOUR_NAME
*/

6. Rename index.html to index.php. Open index.php and replace the reference to style.css with < ?php bloginfo( 'stylesheet_url' ); ?>

7. In wp-admin, go to Appearance -> Themes and select my new theme. It will still be completely static but this is the starting point for the real WordPress theme development.

8. Create the file header.php ; move the header stuff from index.php into this file. For me this is generally from first line (the DOCTYPE) down to the start of the main content area. In index.php I insert <?php get_header(); ?> to load the contents of header.php.

9. Similarly I create footer.php and sidebar.php and then move the lines from index.php into those files, and replace with <?php get_footer(); ?> and <?php get_sidebar(); ?> respectively.

10. Edit header.php, so that it looks something like this (this is HTML5):

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo('charset'); ?>" />
	<title><?php wp_title(); ?> <?php bloginfo( 'name' ); ?></title>
	<link rel="profile" href="http://gmpg.org/xfn/11" />
	<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen" />
	<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
	<?php wp_enqueue_script('jquery'); //only use if you need jquery?>
	<?php wp_head(); //this does a lot of stuff ?>

	<!--[if IE]>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style-ie.css" type="text/css"></link>
	<![endif]-->
</head>
<body <?php body_class(); ?>>
	<div id="Main">
		<header>
			<div id="Holder">
				<h1><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h1>
				<h2><?php bloginfo('description'); ?></h2>
			</div>
			<nav>
				<?php wp_page_menu(); ?>
			</nav>
			<div class="clear"></div>
		</header>

11. Edit index.php, remove the static content and insert The Loop code, which looks like this:

<?php get_header(); ?>
<div id="Content">
<?php
	//The Loop
	if( have_posts() ) : while( have_posts() ) : the_post();
		echo '<h1>'.get_the_title().'</h1>';
		the_content();
	endwhile; endif; //end the loop
?>
</div><!--Content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

If there are other files in your theme directory, use

<?php bloginfo('template_url'); ?>/

… eg:

<link href="<?php bloginfo("template_url"); ?>/webfonts/bauhaus.css" rel="stylesheet" type="text/css" />

12. In footer.php I might have something like:

	<footer>
		&copy; <?php echo date('Y').' '; bloginfo('name'); ?>
	</footer>
	</div><!--Wrap-->
	<?php wp_footer(); ?>
</body>
</html>

At this point we should see our theme loading content dynamically from WordPress. But this is just the start…

See the WordPress Template Checklist for a more thorough guide to everything you might need.

Delete links across multiple HTML files in bash

Posted on December 5, 2011

Or any type of file, really. I needed to quickly remove links from an old website that was using flat HTML files. In my linux command line, I found I could do:

perl -pi -e 's/SEARCH/REPLACE/g' *.html

To replace all instances of SEARCH with REPLACE in *.html.

Except I needed to do a fair bit of escaping, because HTML is full of characters that mean something else on the command line.

So let’s say the string I needed to remove was:

<a title="Search Engine Optimisation" href="http://superspammyseocompany.com/" target="_self"><span>Search Engine Optimisation</span></a> by <a title="Super Spammy SEO Company" href="http://superspammyseocompany.com/" target="_self">Super Spammy SEO Company</a>

I copy + pasted this into vim, and then every time these characters occur:

< , >, / and ”

I put a \ in front of each of these, which gave me:

\<a title=\"Search Engine Optimisation\" href=\"http:\/\/superspammyseocompany.com\/\" target=\"_self\"\>\<span\>Search Engine Optimisation\<\/span\>\<\/a\> by \<a title=\"Super Spammy SEO Company\" href=\"http:\/\/superspammyseocompany.com\/\" target=\"_self\">Super Spammy SEO Company\<\/a\>

Which was a bit of work, but still much more fun than manually removing the link from each file.

Note that these characters do not need to be escaped with a backslash:

= (equals), . (dot), and  _ (underscore)

So my final command was:

perl -pi -e 's\\<a title=\"Search Engine Optimisation\" href=\"http:\/\/superspammyseocompany.com\/\" target=\"_self\"\>\<span\>Search Engine Optimisation\<\/span\>\<\/a\> by \<a title=\"Super Spammy SEO Company\" href=\"http:\/\/superspammyseocompany.com\/\" target=\"_self\">Super Spammy SEO Company\<\/a\>//' *.html

I’d already initialised a git repository and committed the files so I could easily restore the files in case of a mistake. A quick look through the links showed it all worked perfectly, and it saved me so much time I thought I’d write this post about it.

Bonus: I outputted all the changed files to list.html, which had one filename per line, like:

./file1.html
./file2.html
./file3.html

Here’s the vim command to turn them all into links, for easy human checking:

:%s/^\(.*\)$/<a href="\1">\1\<\/a\>\<\/br\>

WordPress: Limit Archives to Single Category

Posted on December 3, 2011

Late yesterday afternoon a client asked if I could look at a business’s WordPress installation. They had posts in several categories but only wanted to show the “Latest News” posts in the archives.

WordPress is designed around blog posts, whereas I find that many business or company sites are designed around pages (eg About Us, Contact Us) – and don’t use WordPress’ powerful blogging tools on the front page. My site is an example – the content on the home page doesn’t change that much.

This means that many of the solutions are also geared around blog-post design.

While searching I found a few examples of how to exclude a specific category from the archives, when what I needed was to include only one category in the WordPress archives – generated in the theme by wp_get_archives().

The solution is to add a filter that modifies the SQL used by wp_get_achives() to the theme’s function.php:

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}

function customarchives_where( $x ) {
global $wpdb;
$include = '1'; // category id to include
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}

Replace ’1′ with the category ID (wp-admin > posts > categories > edit, then in the address bar it is directly after ‘&tag_ID’) and that’s it. wp_get_archives() will now only display posts from that category.

I found this in http://wordpress.org/support/topic/wp_get_archives-and-conditional-tags?replies=6#post-1578041 (thanks Paul!).