WordPress tip: Get rid of unused post revisions
10+ life saving SQL queries
Some useful WordPress database queries
J2EE Developer & Photography enthusiast
WordPress tip: Get rid of unused post revisions
10+ life saving SQL queries
Some useful WordPress database queries
I’ve just update the Selective Javascript Loader plugin with some bugfixes and improvements:
Read more or download it.
I’ve just released Selective Javascript Loader my first WordPress plugin. It’s a very simple plugin that automatically loads different Javascript files based on the blog section that is being viewed (index, category, single post, page).
It can be really useful if you make extensive use of Javascript in your theme and want to split the code and load functions only when you need them.
In my previous post I talked about a Lorelle blog post on creating single post templates for different categories.
After using the code snippets “has is” I tryed to build a more flexible solution so I’ve come up with this little function:
function getSingleTemplate(){ $category = get_the_category(); $templateName = TEMPLATEPATH . '/single_' . $category[0]->category_nicename .'.php'; if (!file_exists($templateName)){ $templateName = TEMPLATEPATH . '/single_default.php'; } return $templateName; }
This function must be called inside the single.php template: it gets the first post’s category, searches for a template named like “single_post_first_category_slug.php” and returns its path; if the file doesn’t exists it will return the single_default.php template path.
My single.php template:
<?php $post = $wp_query->post; include( getSingleTemplate() ); ?>
Today, while trying to customize the single.php template for a specific category I found a pretty old article written by Lorelle:
Creating Multiple Single Posts for Different Categories.
Her tip is really simple and really useful: just rename your single.php to single1.php and create a new single2.php with your category specific layout; after that, create a new single.php file with this code:
<?php $post = $wp_query->post; if ( in_category('1') ) { include(TEMPLATEPATH . '/single2.php'); } else { include(TEMPLATEPATH . '/single1.php'); } ?>
So, if the post is in category 1 WordPress will use the single2.php otherwise it will use single1.php.
I wrote a more flexible solution: check it out.
WordPress has a simple function to build a search form for your blog and in this tutorial I’ll show you how to add a category filter to it.
< ?php get_search_form(); ?>
This function will look for a file called searchform.php inside your template folder: if it doesn’t exist it will output the standard search form. So, if it isn’t already in place, create your custom searchform.php and copy into it the default search form output. It should look similar to this:
<form role="search" method="get" id="searchform" action="<?php bloginfo('siteurl'); ?>"> <div> <label class="screen-reader-text" for="s">Search for:</label> <input type="text" value="" name="s" id="s" /> <input type="submit" id="searchsubmit" value="Search" /> </div> </form>
Take a look at this post for a list of useful “hidden” configuration settings for your WordPress installation.
I found some nice tricks that I’ve applied to my wp-config.php:
// Number of post revision to keep define('WP_POST_REVISIONS', 3); // Installation path and template path to reduce database queries define('WP_HOME', 'http://your_site_url.com'); define('WP_SITEURL', 'http://your_wordpress_directory_url.com'); define('TEMPLATEPATH', '/absolute/path/to/wp-content/themes/active-theme'); define('STYLESHEETPATH', '/absolute/path/to/wp-content/themes/active-theme'); // Security keys to improve cookies encription define('AUTH_KEY', 'randomKey'); define('SECURE_AUTH_KEY', 'randomKey'); define('LOGGED_IN_KEY', 'randomKey'); define('NONCE_KEY', 'randomKey');
A really good tutorial on building custom taxonomies in WordPress 2.8.
Dougal Campbell wrote some interesting notes on permalinks structure and best practices in WordPress. Worth reading:
Efficient permalink strategies for WordPress – Dougal Campbell’s geek ramblings.